0

Trying to trigger an animation once on scroll, but it triggers multiple times.

$(window).scroll(function(){
  var y = $(window).scrollTop();
  var flagscroll=true;
  if( y < 30 && y > 20 && flagscroll==true ) {
        flagscroll=false;
       $('[data-label="SearchPanel"]').animate({ 
            top: "-=34px",
        }, 200 );
  }
});

What am I missing here? Thanks for your ideas!

2 Answers 2

3

Your flag is always set back to true for every scroll event. You must place the initialization outside of the event function declaration:

var flagscroll=true;

$(window).scroll(function(){
    var y = $(window).scrollTop();
    if( y < 30 && y > 20 && flagscroll==true ) {
        flagscroll=false;
        $('[data-label="SearchPanel"]').animate({ 
            top: "-=34px",
        }, 200 );
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

set var flagscroll=true; before $(window).scroll

like

var flagscroll=true;
$(window).scroll(function(){
var y = $(window).scrollTop();
if( y < 30 && y > 20 && flagscroll==true ) {
    flagscroll=false;
   $('[data-label="SearchPanel"]').animate({ 
        top: "-=34px",
    }, 200 );
}
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.