js滚动条练习

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #info {
        width: 200px;
        height: 200px;
        background: red;
        overflow: auto;
      }
    </style>
  </head>
  <body>
    <h3>欢迎注册</h3>
    <p id="info">
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654 1112345654 1112345654
      1112345654 1112345654 1112345654 1112345654
    </p>
    <form action="">
      <input type="checkbox" disabled="disabled" name="12"/>我已阅读
    <input type="submit" disabled="disabled" value="注册"  name="13"/>
    </form>
  </body>
</html>
<script>
 window.onload=function(){
    var info = document.getElementById("info");
  var inps = document.querySelectorAll("input");
  console.log(info, inps);
  info.onscroll = function () {
    console.log("111");
    // 检查滚动条是否滚动到底
    // console.log(info.scrollHeight,info.scrollTop,info.clientHeight);
    // console.log(Math.floor(info.scrollHeight-info.scrollTop)==info.clientHeight);
    if(Math.floor(info.scrollHeight-info.scrollTop)==info.clientHeight){
      // disabled true为禁用,false为使用
      inps[0].disabled = false;
      inps[1].disabled = false;
    }
  };
 }
</script>

为什么我会在事件判断中加入Math.floor()方法,因为有时候scrollTop的值是小数,而在进行if判断的时候我们用scrollHeight去减scrollTop时产生的值也是小数,为了与clientHeight比较,所以采取向下取整Math.floor()的方法,才能实现正常的判断

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值