JavaScript Preload images during scroll

I am using Jquery to alter the source of an image as the page scrolls. However, currently they are loading as they are displayed and i would like to have them preload several images prior to being shown.

HTML

<img src="/img/1.jpg" />

JQuery

$(window).load(function(){
// Array of images to swap between
var images = [/img/1.jpg, /img/2.jpg, /img/3.jpg, /img/4.jpg];

var totalImages = images.length; 

var documentHeight = $(document).height();

// Work out how often we should change image (i.e. how far we scroll between changes)
var scrollInterval = Math.floor(documentHeight / totalImages);

$(document).scroll(function () {
// Which one should we show at this scroll point?
i = Math.floor($(this).scrollTop() / scrollInterval);
// Show the corresponding image from the array
$('img').attr('src', images[i]);
});
});//]]>  

css

img {
position: fixed;
top: 0;
left: 0;
height: 100%;
}
body {
height: 5000px;
}

Attempt. I would like to add something similar to this to,

$(document).scroll(function () {    
    function preload(arrayOfImages) {
        $(arrayOfImages).each(function(){
            (new Image()).src = this;
        });
    }

    i = Math.floor([i]+'1');

    preload([[i]]);
}

But cannot figure how to code it... (New to JS)

Answer:1

I've built a test client page and I'm trying to post to a Web API that I've built, but I get the following error: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'val'...

I've built a test client page and I'm trying to post to a Web API that I've built, but I get the following error: 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'val'...

  1. object doesn't support property or method
  2. object doesn't support this action
  3. object doesn't support this action ie11
  4. object doesn't fit print volume

I'm getting this error when the optimizer is launched by grunt (I'm using yeoman backbone-generator ) Running "requirejs:dist" (requirejs) task Error: ReferenceError: window is not ...

I'm getting this error when the optimizer is launched by grunt (I'm using yeoman backbone-generator ) Running "requirejs:dist" (requirejs) task Error: ReferenceError: window is not ...

  1. requirejs window.define
  2. requirejs window is not defined

My objective is to design a small site with auto-completion of a field which would take suggestions from server's database (Node.js + MySQL). What methods I could use to achieve this client-server ...

My objective is to design a small site with auto-completion of a field which would take suggestions from server's database (Node.js + MySQL). What methods I could use to achieve this client-server ...

I got a variable using document.getSelection() This variable is well displayed if I use alert() but not if I use html(). How can I make it visible with html()? $(document).ready(function(){ $("....

I got a variable using document.getSelection() This variable is well displayed if I use alert() but not if I use html(). How can I make it visible with html()? $(document).ready(function(){ $("....

  1. text selection not visible in illustrator
  2. adobe illustrator text selection not visible
  3. text selection not showing in illustrator