首页 > 解决方案 > 使用ajax调用在移动设备上滚动页面时防止touchstart

问题描述

通常这将是代码:

var touchmoved;
$('button').on('touchend', function(e){
    if(touchmoved != true){
        // you're on button click action
    }
}).on('touchmove', function(e){
    touchmoved = true;
}).on('touchstart', function(){
    touchmoved = false;
});

但是,此代码似乎不适用于动态生成的 ajax 元素。

为此,我有以下代码:

var touchmoved;
$(document).on('click touchend', '.menu', function(){
    if(touchmoved != true){
        // you're on button click action
    }
}).on('touchmove', '.menu', function(e){
    touchmoved = true;
}).on('touchstart', '.menu', function(){
    touchmoved = false;
});

现在它适用于网络界面,但似乎不适用于移动界面。

是否可以制作一个工作区,以便我可以动态地进行 ajax 调用并使其正常工作?

标签: jqueryajaxtouchstart

解决方案


原来我最初发布的代码是正确的答案。

我正在使用一个名为 nicescroll 的第 3 方脚本,它在我下载的版本中有一些错误。

因此,对于后代,(目前)防止动态生成元素的 touchstart 的方法是:

var touchmoved;
$(document).on('click touchend', '.menu', function(){
    if(touchmoved != true){
        // your click action here!!!
    }
}).on('touchmove', '.menu', function(e){
    touchmoved = true;
}).on('touchstart', '.menu', function(){
    touchmoved = false;
});

推荐阅读