禁止屏幕滚动

<!DOCTYPE> 
<html> 
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0" />
  <style>
    *{
      margin: 0;
      padding:0;
  }
  button{
      margin: 10px;
  }
  .test{
      position: relative;
      width: 300px;
      height: 1000px;
      background: #ccc;
  }
  .test .head,
  .test .center,
  .test .foot{
      position: absolute;
      left:0;
  }
  .test .head{
      top:0;
  }
  .test .center{
      top: 500px;
  }
  .test .foot{
      bottom: 0;
  }
</style>
</head> 
<body> 
  <button type="button" id="no_scroll">点我屏幕不能滚动</button>
  <button type="button" id="scroll">点我屏幕允许滚动</button>
  <div class="test">
    <p class="head">这里是头部</p>
    <p class="center">这里是中部</p>
    <p class="foot">这里是底部</p>
</div>
<script type="text/javascript">
    var handler = function () {
        event.preventDefault();
        event.stopPropagation();
    }; 
    document.getElementById("no_scroll").addEventListener('click',function(){
        document.body.style.cssText="overflow:hidden";
        document.body.addEventListener('touchmove',handler,false);
        document.body.addEventListener('wheel',handler,false);
    });
    document.getElementById("scroll").addEventListener('click',function(){
        document.body.style.cssText="overflow:auto";
        document.body.removeEventListener('touchmove',handler,false);
        document.body.removeEventListener('wheel',handler,false);
    });
</script>
</body> 
</html> 

 

转载于:https://www.cnblogs.com/theWayToAce/p/7079201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值