简体   繁体   English

禁用和启用滚动

[英]Disable and Enable scrolling

this javascript code is work fine: 这个JavaScript代码可以正常工作:

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

put it disable scrolling for all elements, and I want to disable scrolling for "BODY" element only and keep the scolling for all child elements 把它禁用所有元素的滚动,我只想禁用“ BODY”元素的滚动,并保持所有子元素的倾斜

This should work: 这应该工作:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM