【发布时间】:2015-07-08 22:58:15
【问题描述】:
我使用TouchSwipe 创建一个可滑动的图像列表。我将 swipe 事件绑定到图像,同时我还绑定了一个 click 事件,它将打开图像的大版本。
我的问题是,如果我滑动,它也会触发点击事件。我试过tap instead of swipe,但我不能让它工作。在此之后,我尝试了很多地方都建议的event.preventDefault() 和event.stopPropagation(),但没有效果。我的最终解决方案尝试是取消绑定点击事件并在事件之后重新绑定它,但如果我在事件函数的最后绑定事件,它会再次触发点击。
$(".js-header-swipe-image").swipe({
swipe:function(event, direction, distance, duration, fingerCount){
$("#details").unbind('click');//Temporary unbind, otherwise the swipe's click would trigger the gallery opening.
//Handling swipe direction.
$('#details').on('click', '.js-header-swipe-image', function (){//Rebind the temporary unbinded event.
console.log('click');
$('#modal-gallery').modal('show');
});
}
});
有没有办法中止事件本身或在事件完成后调用函数,以便我可以在滑动完成后重新绑定点击,这样它就不会触发重新绑定的点击?我也愿意接受任何其他解决问题的方法。
【问题讨论】:
-
你找到解决办法了吗?
-
@Flakerim 不,但发现在移动平台上它的实现方式不同,所以这个问题不存在。 PC 上还是有问题。
-
我修复了我的问题,它使用了旋转滑块滑动,我将 $.fn.swipe 重命名为 $.fn.swipeing 并调用了 .swipeing({}),检查这是否对您有帮助。您可能有其他插件可以覆盖滑动。