Jay*_*Jay 17 javascript jquery
用户滚动100像素后是否可以触发警报.
这是我到目前为止所得到的,但我知道我错过了一些东西;
$(window).scroll(function() {
if (document.documentElement.clientHeight +
$(document).scrollTop() == "100px")
{
alert("You've scrolled 100 pixels.");
}
});
Run Code Online (Sandbox Code Playgroud)
voi*_*tan 49
查看窗口.scrollTop(返回一个整数):
$(window).scroll(function() {
if ($(this).scrollTop() === 100) { // this refers to window
alert("You've scrolled 100 pixels.");
}
});
Run Code Online (Sandbox Code Playgroud)
但如果您滚动了102px它就不会触发警报框.
如果你只是想要触发警报一旦有一个全局变量,如果它被触发就设置为true:
$(function(){
var hasBeenTrigged = false;
$(window).scroll(function() {
if ($(this).scrollTop() >= 100 && !hasBeenTrigged) { // if scroll is greater/equal then 100 and hasBeenTrigged is set to false.
alert("You've scrolled 100 pixels.");
hasBeenTrigged = true;
}
});
});
Run Code Online (Sandbox Code Playgroud)
或者只是在触发警报框后取消绑定滚动事件:
$(function(){
$(window).bind("scroll.alert", function() {
var $this = $(this);
if ($this.scrollTop() >= 100) {
alert("You've scrolled 100 pixels.");
$this.unbind("scroll.alert");
}
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
48219 次 |
最近记录: |