Get div tag scroll position using JavaScript

后端 未结 2 2045
慢半拍i
慢半拍i 2021-01-03 18:29

How do I get the amount of scroll in a div tag using JavaScript? Please provide me with an example.

I don\'t want to use jQuery, only JavaScript.

相关标签:
2条回答
  • 2021-01-03 19:00
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function scollPos() {
                var div = document.getElementById("myDiv").scrollTop;
                document.getElementById("pos").innerHTML = div;
            }
        </script>
    </head>
    <body>
        <form id="form1">
        <div id="pos">
        </div>
        <div id="myDiv" style="overflow: auto; height: 200px; width: 200px;" onscroll="scollPos();">
            Place some large content here
        </div>
        </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-03 19:17

    you use the scrollTop attribute

    var position = document.getElementById('id').scrollTop;
    
    0 讨论(0)
提交回复
热议问题