简体   繁体   English

向上或向下滚动JavaScript事件

[英]JavaScript event on scroll up or scroll down

Is it possible to write a JavaScript to make an action when a DIV Layer's scroll bar is manually scrolled up or scrolled down? 当DIV图层的滚动条手动向上滚动或向下滚动时,是否可以编写JavaScript来执行操作?

If so please give me a hint to implement a simple such as alert box saying you scrolled up and you scrolled down. 如果是这样,请给我一个提示,以实现一个简单的警告框,例如说您向上滚动和向下滚动。

You can simple use onscroll event of java-script. 您可以简单地使用Java脚本的onscroll事件。

OnScroll Event Reference : http://www.w3schools.com/jquery/event_scroll.asp OnScroll事件参考http : //www.w3schools.com/jquery/event_scroll.asp

Here is example, 这是例子

<head>
    <script type="text/javascript">
        function OnScrollDiv (div) {
            var info = document.getElementById ("info");
            info.innerHTML = "Horizontal: " + div.scrollLeft
                            + "px<br/>Vertical: " + div.scrollTop + "px";
        }
    </script>
</head>
<body>
    <div style="width:200px;height:200px; overflow:auto;" onscroll="OnScrollDiv (this)">
        Please scroll this field!
        <div style="height:300px; width:2000px; background-color:#a08080;"></div>
        Please scroll this field!
        <div style="height:300px; width:2000px; background-color:#a08080;"></div>
        Please scroll this field!
    </div>
    <br /><br />
    Current scroll amounts:
    <div id="info"></div>
</body>

Working copy here : http://jsbin.com/iledat/2/edit 此处的工作副本: http : //jsbin.com/iledat/2/edit

尝试jQuery滚动事件

$('div').scroll(function(){alert('scrolled!')})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM