重新启用touchmove eventlistener。 iPhone应用程序与电话差距 [英] Re-enabling touchmove eventlistener. iPhone app with Phone gap

查看:102
本文介绍了重新启用touchmove eventlistener。 iPhone应用程序与电话差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下权利在顶部的我的js(这是需要的iScroll插件工作)。

I have the following right at the top of my js (which is needed for the iScroll plugin to work).

document.addEventListener('touchmove', function (e) {e.preventDefault();}, false);

在某些页面上,我需要重新启用此功能,所以正常的iPhone滚动操作当页面上使用iscroll时,iphone键盘不会弹出在输入框上)。

On some pages, I need to re-enable this so normal iPhone scrolling takes over (and to fix bugs where the iphone keyboard does not popup on an input box when iscroll is used on the page).

我无法运行语法。

推荐答案

您需要定义一个函数来处理preventDefault行为,如下所示:

You will need to define a function that handles the preventDefault behavior, like so:

document.addEventListener('touchmove', preventDefault, false);
function preventDefault(e) { e.preventDefault(); };

这样,在代码中,你可以删除事件监听器并重新启用默认滚动:

That way later on in your code, you can remove the event listener and re-enable default scrolling:

document.removeEventListener('touchmove', preventDefault, false);

现在,您可以在需要时重新添加和删除它。

Now you are able to re-add and remove it whenever you need to.

你不能使用内联的匿名函数来完成这个工作,因为你可以引用一个匿名函数,首先让它启动,然后删除它的引用,例如:

You cannot accomplish this using an inline, anonymous function like in your original post because the only way you can refer to an anonymous function is to first let it fire and then remove the reference to it, e.g.:

document.addEventListener('touchmove', function(e) {
    e.preventDefault();

    // some more logic...

    this.removeEventListener('touchmove', arguments.callee, false);
}, false);

显然这不是你想要的效果。您还可以创建一个对象并存储对处理事件的方法的引用。如果你想深入一点,我发现此主题很有用:如何removeEventListener on anonymous function?

Obviously this is not the effect you would like to have. You could also create an object and store a reference to a method that handles the event. If you're looking to dive a little deeper I found this thread helpful: How to removeEventListener on anonymous function?

这篇关于重新启用touchmove eventlistener。 iPhone应用程序与电话差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆