1

別のSO質問に続く

このスクリプトは、ページのスクロール時に画像を変更します。ただし、このスクリプトは、たとえば 200 以上の画像を含むビデオのフレームに使用すると、非常に大きくなります。多数のファイルで使用するためにこのスクリプトを短くすることはできますか?

どうもありがとう

4

1 に答える 1

8

マイナーコードの微調整...

JSFiddle

http://jsfiddle.net/gvee/ygkWH/6/

HTML

<img src="http://placekitten.com/100/100" /><b>Frame: 0</b>

【実例】CSS

img, b {
    position: fixed;
    top: 0;
    left: 0;
}
body {
    height: 10000px;
}

JQuery

// Array of images to swap between
var images = [];

// Add 200 items to array
for (i = 0; i < 200; i++) {
    images.push('http://placekitten.com/' + (100 + i) + '/' + (100 + i));
}

var totalImages = images.length;

var pageHeight = $(document).height() - $(window).height();

// Work out how often we should change image (i.e. how far we scroll between changes)
var scrollInterval = Math.floor(pageHeight / 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]);
    $('b').text('Frame: ' + i);
});
于 2013-08-06T13:11:53.003 に答える