原生js判断某个区域的滚动条滚动到了底部

原生js判断某个区域的滚动条滚动到了底部

讲解==》

关系公式:element.scrollHeight - element.scrollTop === element.clientHeight
解释:此公式可以用于判断是否滚动到底
你必须知道这个方法 可以判断滚动条滚动到了底部哈!

element.scrollHeight 是获取这个元素区域的实际高度(包含被隐藏起来的高度)
element.scrollTop是获取滚动条距离顶部的实际距离(包含被隐藏起来的高度)
element.clientHeight: 就是元素内部(可见高度) + 自身padding

在生命周期函数中 给滚动区域的元素注册滚动事件
element.addEventListener(‘scroll‘, this.doSomething);

  <div id="app">
         <div class="box" id="box">
            <div class="demo" v-for="(item,index) in condata" :key="index">{{item}}</div>
         </div>
   </div>

  <!-- import Vue before Element -->
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <!-- import JavaScript -->
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://unpkg.com/[email protected]/lib/index.js"></script>
  <script>
    new Vue({
      el: '#app',
        data() {
            return {
                condata:["121212","121212","121212","121212","121212","121212","121212","121212",
               "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212",
               "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","121212",
              "121212","121212","121212","121212","121212","121212","121212","121212","最后一条数据了",
              ],
              boxScrollpositionHeight:0,
              boxElement:"",
              boxscrollPosition:0,//滚动条所在的高度()
            }
        },
        mounted () {
             this.boxElement=document.getElementById("box"); 

            this.boxElement.addEventListener('scroll', this.doSomething);

            // 获取滚动条滚动区域盒子的高度

            this.boxScrollpositionHeight=this.boxElement.offsetHeight;
            console.log("div内容区域的实际高度",this.boxElement.scrollHeight)
        },
        methods: {
            doSomething(){

                //此时滚动条所在的高度
                let scrollcurHeight=this.boxElement.scrollTop;

                console.log("滚动条距离顶部的实际高度",this.boxElement.scrollTop)
                console.log("hah",this.boxElement.clientHeight)

                // 关系公式:element.scrollHeight - element.scrollTop === element.clientHeight
                // 解释:此公式可以用于判断是否滚动到底
  

                if(this.boxElement.scrollHeight-this.boxElement.scrollTop===this.boxElement.clientHeight){
                    console.log("滚动到底部了",)
                }
            }
         },
        beforeDestroy() {
            let boxscrollPosition=document.getElementById("box");
            boxscrollPosition.removeEventListener('scroll', this.doSomething);
        },
    })
  </script>
  <style type="text/css">
      *{
          padding: 0;
          margin: 0;
      }
      body,html,#app{
          height: 100%;
      }
    .box{
         width: 300px;
         height: calc(100% - 279px);
         background: #0366D6;
         overflow: hidden;
         overflow-y: auto;
        }

        .demo{
            height: 30px;
            line-height: 30px;
            border-top:1px solid #ccc;
        }

  </style>

原文地址:https://www.cnblogs.com/IwishIcould/p/12104954.html

时间: 2024-11-06 03:57:58

原生js判断某个区域的滚动条滚动到了底部的相关文章

JS判断页面是否出现滚动条

今天无聊,帮一个网友解决一个很无聊的问题,用JS判断页面是否出现滚动条,在网上看了一些代码,经过验证并不起作用,下面是在网上搜索到的代码: 当可视区域小于页面的实际高度时,判定为出现滚动条,即: if (document.documentElement.clientHeight < document.documentElement.offsetHeight) scroll = true; 要使用 document.documentElement ,必须在页面头部加入声明: <!DOCTYPE

原生js判断css动画结束 css 动画结束的回调函数

原文:原生js判断css动画结束 css 动画结束的回调函数 css3 的时代,css3--动画 一切皆有可能: 传统的js 可以通过回调函数判断动画是否结束:即使是采用CSS技术生成动画效果,JavaScript仍然能捕获动画或变换的结束事件: transitionend事件和animationend事件标准的浏览器事件,但在WebKit浏览器里你仍然需要使用webkit前缀,所以,我们不得不根据各种浏览器分别检测事件 var transitions = { 'transition':'tra

当滚动条滚动到页面底部自动加载增加内容的js代码

这篇文章主要介绍了如何使用javscript实现滚动条滚动到页面底部自动加载增加页面内容,需要的朋友可以参考下..1,注册页面滚动事件,window.onscroll = function(){ }; 2,相关获取页面高度.滚动条位置.文档高度的函数: 复制代码 代码如下://获取滚动条当前的位置 function getScrollTop() { var scrollTop = 0; if (document.documentElement && document.documentEle

JS 原生JS 判断滚动条滑动到底部(兼容苹果safari)

ListenerScoller () { var pageIndex = 1; var startX, startY; document.addEventListener('touchstart',function (ev) { startX = ev.touches[0].pageX; startY = ev.touches[0].pageY; }, false); let _this = this; document.addEventListener('touchend',function

利用原生JS判断组合键

<script type="text/javascript"> var isAlt = 0; var isEnt = 0; document.onkeydown = function(e){ var evn = e||event; var code = evn.keyCode||evn.which||evn.charCode; if(code==13){ isEnt = 1; } if(code==18){ isAlt = 1; } // 判读Alt+Enter组合键 if

原生JS和jQuery实现banner图滚动那些事

  前  言 阿q 作为一个准前端学员,banner图可是很重要的呢.本人,小白一只,给大家分享几个刚刚学习的基础banner图事件.~~~ 1. 小广告图滚动播放 1.1HTML代码 首先,创建基本结构 <div id="outside"> <div id="inside"> <div>1</div> <div>2</div> <div>3</div> <div&

JS 判断鼠标滚轮的上下滚动

<script type="text/javascript"> var scrollFunc = function (e) { e = e || window.event; if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件 if (e.wheelDelta > 0) { //当滑轮向上滚动时 alert("滑轮向上滚动"); } if (e.wheelDelta < 0) { //当滑轮向下滚动时 alert(&quo

js判断类型为数字的方法实现总汇——原生js判断isNumber()

方法一[推荐]: 最容易想到的是用typeof来判断是否是number类型 ,但是如果为NaN会被认为也是number类型,因此我们需要使用isNaN来排除NaN的情况. function isNumber(obj) { return typeof obj === 'number' && !isNaN(obj) } 方法二: isNaN(obj)   等价于isFinite().Infinity在js表示数字的无穷大,判断是否可用做数字的最佳方法则是isFinite(),因为该方法会筛除掉

原生js判断某个元素是否有指定的class名的几种方法

[注意]以下方法只对class只有一个值的情况下操作 ************************************************************* 结构部分: <div>   <p>1</p>   <p class="test">2</p>   <p>3</p> </div> *******************************************