cactimurray
8/21/2018 - 1:37 AM

Throttled Scroll Event

JQuery Throttled Scroll Event

var scrollTimeout;
var throttle = 50;

scrollEvent = function() {
    if ($(window).scrollTop() > 500) {
        $(".navbar").addClass("stuck");
    } else {
        $(".navbar").removeClass("stuck");
    }
};

$(window).on('scroll', function() {
    if (!scrollTimeout) {
        scrollTimeout = setTimeout(function() {
            scrollEvent();
            scrollTimeout = null;
        }, throttle);
    }
    // console.log('native scroll');
});