0

所以它是预加载图像

  var headerImages = [
       'images/logoa.png',
       'images/logob.png',
       'images/logoc.png',
       'images/logod.png',
       'images/logoe.png',
       'images/logog.png',
       'images/logoh.png',
       /* 'images/logoi.png', */
       'images/test/logoa.png',
       'images/test/logob.png'];

然后在每个页面滚动上随机选择一个,我如何按顺序制作,意思是从上到下预加载?

....

jQuery.preLoadImages(headerImages);


jQuery(window).scroll(function() {
  jQuery('#sidebar').css('backgroundImage', 'url(' +
headerImages[Math.floor(Math.random()*headerImages.length)] + ')');
});

所以我想这是这部分Math.floor(Math.random()*headerImages.length),但我不知道该怎么做..

非常感谢你的帮助!

4

3 回答 3

1
var x =0;
jQuery(window).scroll(function() {
  jQuery('#sidebar').css('backgroundImage', 'url(' +
headerImages[x] + ')');
x++
if (x == headerImages.length)
{
 x =0;
}
});
于 2012-12-17T12:12:41.133 回答
1

headerImages似乎是一个数组,所以如果你从 0 到 headerImages.lenght 循环并执行以下操作,你应该没问题:

jQuery('#sidebar').css('backgroundImage', 'url(' + headerImages[x] + ')');

在循环内部,x您从 0 增加到最大值的变量在哪里。

于 2012-12-17T12:14:01.907 回答
1

您可以通过将当前图像索引存储在变量中并在每次滚动时添加来实现。然后在到达列表末尾时将其设置为零:

jQuery.preLoadImages(headerImages);

    var index = 0; // set the variable

    jQuery(window).scroll(function() {
    jQuery('#sidebar').css('backgroundImage', 'url(' + headerImages[index] + ')');

    index ++; // add one to point to the next image on the next scroll

    if(index==headerImages.length) index = 0; // If the counter is at the end of the array then set it to zero again

});
于 2012-12-17T12:14:02.603 回答