Sosa
Sosa

Reputation: 834

jQuery stop (window).scrollTop function

I have an if else statement where it adds a class if the scroll is over 50px. Is there a way I can cancel out this entire function if I were to click a button?

$(window).scroll(function(){
    if($(window).scrollTop()>50){
        $('.scroller').addClass('fixedposition');
    } else {
        $('.scroller').removeClass('fixedposition');
    }
});

I have that same .scroller class on multiple places on the page so I would like to turn it off in certain events.

Example JSFiddle

Upvotes: 2

Views: 9243

Answers (1)

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61509

This should disable any action that is attached to scrolling (solution independent from library that you are using)

$('body').on({
'mousewheel': function(e) {
    e.preventDefault();
    e.stopPropagation();
    }
})

Other (more appropriate) solution would be to unbind scroll assuming you are using Jquery scroll

$(window).unbind('scroll');

last one would be to

$('button')
    .on('click', function () {
        $('.scroller')
            .toggleClass('scroller-enabled');
})

and based on scroller-enabled class enable or disable functionality.

Upvotes: 3

Related Questions