向上滚动或向下滚动时的 JavaScript 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8445945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript event on scroll up or scroll down
提问by Mad coder.
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.
如果是这样,请给我一个提示来实现一个简单的提示框,比如你向上滚动和向下滚动。
回答by Sreekumar P
You can simple use onscroll
event of java-script.
您可以简单地使用onscroll
java 脚本的事件。
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