滚动100个像素后的jQuery警报 [英] jQuery alert after 100 pixels scrolled

查看:113
本文介绍了滚动100个像素后的jQuery警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户滚动100像素后是否可以触发警报。

Is it possible to fire an alert after a user scrolls 100 pixels.

这是我到目前为止所得到的,但我知道我遗失了一些东西;

Here's what I have so far but I know I'm missing something;

$(window).scroll(function() {
    if (document.documentElement.clientHeight + 
        $(document).scrollTop() == "100px")
    { 
        alert("You've scrolled 100 pixels.");
    }
});


推荐答案

查看窗口.scrollTop(返回一个整数) :

Look at the window .scrollTop (returns an integer):

$(window).scroll(function() {
    if ($(this).scrollTop() === 100) { // this refers to window
        alert("You've scrolled 100 pixels.");
    }
});

但如果您滚动了102px则不会触发警报框。

but if you have scrolled 102px it wont trigger the alert box.

如果您只想触发警报,一旦有一个全局变量设置为true,如果它被触发:

if you just want to trigger the alert once have a global variable that sets to true if it has been trigged:

$(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;
        }
    });
});

或者仅在触发警报框后取消绑定滚动事件:

or just unbind the scroll event once the alert box has been trigged:

$(function(){
    $(window).bind("scroll.alert", function() {
        var $this = $(this);
        if ($this.scrollTop() >= 100) {
            alert("You've scrolled 100 pixels.");
            $this.unbind("scroll.alert");
        }
    });
});

这篇关于滚动100个像素后的jQuery警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆