javascript - iPhone 上滚动事件内的 SetInterval - 为什么此代码不起作用?

标签 javascript iphone events scroll setinterval

请看一下这段代码(我正在使用 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.

MDC docs

关于javascript - iPhone 上滚动事件内的 SetInterval - 为什么此代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6887377/

相关文章:

javascript - 获取当前选中的文本

c# - 隐藏扫描仪进度条窗口

java - 带有 ComponentListener 的 JPanel

javascript - 不使用 ajax 发送自定义 header

javascript - 有什么方法可以从 Javascript 触发 VB.NET 按钮?

javascript - jQuery : hide rows in table based on column data

iphone - KVC 与快速枚举

iphone - 如何通过蓝牙将文件发送到其他 iPhone

iphone - 如何通过 Facebook iOS SDK 和 Graph API 邀请 friend 加入我的应用程序

c# - 在 tabitems 中加载列表框 - 使用哪个事件?