禁用并启用滚动

时间:2014-08-05 00:10:39

标签: javascript

这个javascript代码工作正常:

function scrolling(bool) {
  if (typeof(bool) == "undefined") {
    document.ontouchstart = function(e) { e.stopPropagation(); }
  } else {
    document.ontouchstart = function(e) { e.preventDefault(); }
  }
}

让它禁用所有元素的滚动,我想禁用滚动#34; BODY"仅元素并保持所有子元素的scolling

1 个答案:

答案 0 :(得分:0)

这应该有效:

function scrolling(bool) {
    var deny = function(e) { if (e.target === e.currentTarget) e.preventDefault(); };
    if (bool) {
        document.body.removeEventListener('touchstart', deny);
        document.body.removeEventListener('touchmove', deny);
    } else {
        document.body.addEventListener('touchstart', deny, false);
        document.body.addEventListener('touchmove', deny, false);
    }
}