Feel The Noise
Feel The Noise

Reputation: 644

Make scrollTop to always check

I'm doing a scrollTop jQuery function, and it is working very nice, see it below:

$(function() {
    $(window).scroll(function() {
        var top = $(document).scrollTop();
        if (top >= 1225) {
            // Do something
        }
    });
});

However, if the user have his scrollTop, in that case, higher than 1225, and the script isn't fully loaded, the function will not happen, just if he scroll the page again. Isn't there a way to make the scrollTop to always check, not just if the user Scrolls the page?

Thanks a lot in Advance.

Upvotes: 2

Views: 521

Answers (1)

Kevin B
Kevin B

Reputation: 95063

Yes, trigger the event yourself.

$(function() {
    $(window).scroll(function() {
        var top = $(document).scrollTop();
        if (top >= 1225) {
            // Do something
        }
    }).triggerHandler("scroll");
});

Upvotes: 4

Related Questions