[英]How to make JavaScript do something every X amount of pixels scrolled
So this is somewhat of a math problem that I'd like to solve using JavaScript. 所以这是一个我想用JavaScript解决的数学问题。 I'm creating a fixed canvas on a website that outputs a different image based on every X amount of pixels scrolled from a particular .offset().top from the top of the window.
我正在网站上创建一个固定画布,根据从窗口顶部的特定.offset()。顶部滚动的每X个像素数输出不同的图像。 I 'could' explicitly map a new image to a particular position but I've got a lot of images and it would behoove me to create a function that can handle this process multiple times until particular end point.
我'可以'显式地将新图像映射到特定位置但是我有很多图像,我应该创建一个可以多次处理此过程直到特定终点的函数。 I'm sort of stuck on how to express this and was wondering if anyone could steer me in the right direction.
我有点不知道如何表达这一点,并想知道是否有人能引导我朝着正确的方向前进。
EDIT 编辑
After consider @Richard Hamilton answer below I've been able to somewhat successfully implement his solution to my own project. 在考虑@Richard Hamilton回答下面之后,我已经能够成功地将他的解决方案应用到我自己的项目中。 It's a little verbose, but here's what I have...
这有点冗长,但这就是我所拥有的......
// Preload Images
var totalImages = 203
var images = new Array()
for (var i = 1; i <= totalImages; i++) {
var filename = 'img_'
if (i < 10) filename += '00'
if (i > 9 && i < 100) filename += '0'
filename += i + '.jpg'
var img = new Image
img.src = '/images/temp/' + filename
images.push(img)
}
// Set initial frame index
var currentLocation = 0
// Canvas Context
var canv = document.getElementById('canvas')
var context = canv.getContext('2d')
$(canv)
.width(768)
.height(432)
// Frame Starting Location
var currentLocation = 0
// Determin the breakpoint increment to fit inside the context
var contextHeight = $('.about--context').height() - 200
var frameHeight = contextHeight / totalImages
// Set first breakpoint
var breakpoint = 63
// Get top of context in relation to window
var contextPos = $('.about--context').offset().top - $(window).scrollTop()
// Set where to start scrubbing through frames
var scrubStart = 62
// Initial scroll direction
var lastScrollTop = 0,
st,
direction
// Output the scroll direction as up or down
function detectDirection() {
st = window.pageYOffset;
if (st > lastScrollTop) {
direction = "down"
} else {
direction = "up"
}
lastScrollTop = st
return direction
}
window.addEventListener('scroll', function() {
var dir = detectDirection()
var contextPos = $('.about--context').offset().top - $(window).scrollTop()
var contextHeight = $('.about--context').height()
var frameHeight = contextHeight / totalImages
if (contextPos <= breakpoint && dir === 'down') {
breakpoint -= frameHeight
currentLocation++
context.drawImage(images[currentLocation], 0, 0, 768, 432)
console.log('Breakpoint = ' + breakpoint + ', index = ' + currentLocation)
}
if (contextPos > breakpoint && dir === 'up') {
breakpoint += frameHeight
currentLocation--
context.drawImage(images[currentLocation], 0, 0, 768, 432)
console.log('Breakpoint = ' + breakpoint + ', index = ' + currentLocation)
}
})
This mostly works, but there seems to be a discrepancy between how the frames change during scroll between a mouse wheel and a trackpad. 这主要是有效的,但是在鼠标滚轮和触控板之间滚动期间帧的变化似乎存在差异。 The trackpad is much more sensitive and can get the breakpoint increment correctly, but the mouse wheel ends up scrolling through the section much quicker without correctly keeping up with the proper frame rate, so I never end up reach the final frame by the end of the section.
触控板更灵敏,并且可以正确地获得断点增量,但鼠标滚轮最终更快地滚动通过该部分而没有正确地保持正确的帧速率,所以我永远不会到达最后一帧到达最后一帧。部分。 Other than that the frames are moving correctly when scrolling up and down.
除此之外,当向上和向下滚动时帧正确移动。
Let's say you have an image tag. 假设你有一个图片标签。 If you have a lot of different image files, it would be a good idea to store them in array.
如果你有很多不同的图像文件,最好将它们存储在数组中。 This is a hard coded example, but shows the general structure.
这是一个硬编码的例子,但显示了一般结构。
var image = document.getElementById("myImage");
var sources = ["image1.png", "image2.png", "image3.png", "image4.png"];
var i = 0;
var breakpoint = 100; // Change to whatever you like
window.addEventListener("scroll", function() {
var scrollDown = document.body.scrollTop;
if (scrollDown >= breakpoint) {
img.setAttribute(src, sources[i]);
breakpoint += 100; //Change to whatever you like
i++;
}
}
You could also have something like this included 你也可以包含这样的东西
var windowHeight = window.innerHeight;
var scrollBottom = document.body.clientHeight - document.body.scrollTop;
if (scrollBottom === windowHeight) {
// Do something
}
First set a breakpoint variable equal to the number of pixels you want to scroll. 首先设置一个断点变量,该变量等于要滚动的像素数。 For an example, I chose 100 because it's a nice round number.
举个例子,我选择了100,因为它是一个很好的圆数。 You then attach an event listener on the
window
object, to detect if a user is scrolling. 然后,您在
window
对象上附加一个事件侦听window
,以检测用户是否正在滚动。
The scrollTop
function represents how far the top of the screen is from the top of the window. scrollTop
函数表示屏幕顶部距窗口顶部的距离。 If that value is higher than the breakpoint, that's when we call our code. 如果该值高于断点,那就是我们调用代码时的情况。 We then increment this by 100.
然后我们将此增加100。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.