beliy333
beliy333

Reputation: 479

If scrollTop is greater than value, then do this ONLY ONCE

When user scrolls past 10px from the top, I'd like to hijack their scroll and scroll them down to certain div. After this happens once, I'd like to release the scroll to be free to scroll wherever. How do I do this?

My current code works but it won't let user to scroll freely after that initial scroll:

$(window).scroll(function() {

    //if I scroll more than 1000px...
    if($(window).scrollTop() > 10){
         $('html,body').animate({scrollTop : $('#main').offset().top}, 900, function(){
            h = 2; 
        });
    }
});

Upvotes: 3

Views: 7941

Answers (1)

edonbajrami
edonbajrami

Reputation: 2206

Try:

var scrolled = false;

$(window).scroll(function() {

    //if I scroll more than 1000px...
    if($(window).scrollTop() > 10 && scrolled == false){
         $('html,body').animate({scrollTop : $('#main').offset().top}, 900, function(){
            h = 2; 
        });
      scrolled = true;
    } else if($(window).scrollTop() == 0) {
      scrolled = false;
    }
});

Upvotes: 3

Related Questions