请看一下这段代码(我正在使用 Zepto http://zeptojs.com/ BTW)...
var timer = false;
$(window).bind('touchstart touchmove scroll', function (e) {
if (timer === false) {
timer = setInterval(function () {
$('footer').css('top', (this.pageYOffset + this.innerHeight - 40) + 'px');
console.log('Adjusted...');
}, 100);
}
}).bind('touchend', function () {
clearInterval(timer);
timer = false;
console.log('Cleaned it up...');
});
如您所见,我试图将一个页脚元素固定在 iPhone 屏幕的底部。我知道有一些库可以帮助我们轻松实现这一点,例如 iScroll 4 http://cubiq.org/iscroll-4 ,但我想看看是否可以让它变得更简单。
事实证明,上面的代码不能正常工作。当我实际滚动页面时,由于某种原因 setInterval 不执行,而是似乎堆积在后台以同时运行每个调用。
最后它没有做我想要它做的事情,即为页脚“设置动画”并在滚动期间将其固定到位,而不仅仅是在滚动之后。有谁知道如何以类似的方式实现这种效果?
谢谢!
最佳答案
When you pass a method to setInterval() (or any other function, for that matter), it will be invoked with a wrong this value. This problem is explained in detail in the JavaScript reference.
关于javascript - iPhone 上滚动事件内的 SetInterval - 为什么此代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6887377/