如何用js监听滚动条滚动事件

如何用js监听滚动条滚动事件,第1张

js监视滚动事件的函数是onscroll

js语法:elementonscroll = functionReference

html语法:<element onscroll="myScript">

<!DOCTYPE html>

<html lang="en">

  <head>

  <meta charset="UTF-8" />

  <style>

  #container {

    position: absolute;

    height: auto;

    top: 0;

    bottom: 0;

    width: auto;

    left: 0;

    right: 0;

    overflow: auto;

  }

 

  #foo {

    height:1000px;

    width:1000px;

    background-color: #777;

    display: block;

  }

 

  </style>

  </head>

  <body>

    <div id="container">

      <div id="foo"></div>

    </div>

 

    <script type="text/javascript">

      // js绑定你需要监控滚动事件的dom,也可以绑定documentbody监控整个网页滚动

      // 也可以监控具体的dom滚动,像下面的container Id对象

      documentgetElementById('container')onscroll = function() {

        consolelog("scrolling");

      };

    </script>

  </body>

</html>

可以这么试一下

在你的页面上某个位置放一个div或者就用你页面上已有的某个元素也行,当用户滚动的时候这个div相对于浏览器的顶部或底部的距离肯定会变化,然后你通过这个距离来判断用户浏览到当前页面的什么位置了。

参考一下,我没试过

创建一个id为menu_zdd的盒子

documentgetElementById("menu_zdd")styleposition="absolute"; documentgetElementById("menu_zdd")styletop=(parseInt(documentdocumentElementclientHeight,10)/2)+parseInt(documentdocumentElementscrollTop,10)-50+"px";

documentgetElementById("menu_zdd")styleleft=((parseInt(documentdocumentElementclientWidth,10)/2)-190)+"px";

大致上就是这个思路,动态获取位置

Jquery可以用height(),来获取DOM元素的高。示例如下:

<script src=">

以上就是关于如何用js监听滚动条滚动事件全部的内容,包括:如何用js监听滚动条滚动事件、JS如何判断浏览器滚动条的位置、如何使用js保持div随滚动条移动跟着移动 比如一直在窗口的上端等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9532566.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存