仅允许在移动设备上滚动一个<div>

时间:2016-04-12 12:01:36

标签: javascript jquery ios mobile touchmove

在网络应用上工作,我想阻止屏幕移动,所以我从这开始:

 $(document).on("touchmove", false);

...然后我意识到我有一个div需要能够滚动,所以我添加了:

$(document).on('touchmove', function(e) {
  if (!$(e.target).parents().hasClass( 'touch-moveable' )) {
    e.preventDefault();
  }
});

工作正常,但现在在该div中滚动移动可能会导致整个屏幕再次移动。不是世界的尽头,但是...是不是有某种方法可以让一切都保持不变,只让一个div移动?

修改

好的,这可行,虽然我敢打赌它可以巩固并变得更优雅?:

$(document).on('touchmove', function(e) {
  if (!$(e.target).parents().hasClass( 'touch-moveable' )) {
    e.preventDefault();
  }
});

$('.touch-moveable').on('touchmove',function(e){
  e.stopPropagation();
});

0 个答案:

没有答案