admin管理员组文章数量:1401794
Following on from another SO Question
This script changes an image on page scroll. However this script would be excessively large if it was for example, used for frames of a video with 200+ images. Is it possible to shorten this script for use with large number of files?
Many thanks
Following on from another SO Question
This script changes an image on page scroll. However this script would be excessively large if it was for example, used for frames of a video with 200+ images. Is it possible to shorten this script for use with large number of files?
Many thanks
Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Aug 6, 2013 at 12:54 user1949366user1949366 4652 gold badges6 silver badges17 bronze badges 01 Answer
Reset to default 8Minor code tweaks...
JSFiddle
http://jsfiddle/gvee/ygkWH/6/
HTML
<img src="http://placekitten./100/100" /><b>Frame: 0</b>
[illustrative] 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./' + (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);
});
本文标签: javascriptImage change on scrollStack Overflow
版权声明:本文标题:javascript - Image change on scroll - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744234911a2596512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论