JQuery scroll scroll once

I have a small part of jQuery designed to smooth my title, resize it and then snap to the side of the screen. Everything works separately from one part, the function of "switching" the height is activated every time the user scrolls, which causes irritation.

Is there a way to detect scrolling only once or switch only once?

$(window).scroll(function () { var width = $(document).width() - 205; var height = $(document).height(); $('#me').animate({ marginLeft: width, width: '22px', height: 'toggle', borderLeftWidth: "10px", borderRightWidth: "10px" }, 1000, function() { $("#me:first").css("margin-top","-90px"); var div = $('#me'); $('#me h1').css("font-size","30px"); var start = $(div).offset().top; $(div).css("height", height); $.event.add(window, "scroll", function() { var p = $(window).scrollTop(); $(div).css('position',((p)>start) ? 'fixed' : 'static'); $(div).css('top',((p)>start) ? '0px' : ''); }); }); }); 
+4
source share
2 answers

Take a look at the jquery one () function http://api.jquery.com/one/

It fires only once, and then quickly removes itself.

+10
source

use the flag to check it for the first time or not

 var doHeightToggle=true; $.event.add(window, "scroll", function() { if(doHeightToggle) { var p = $(window).scrollTop(); $(div).css('position',((p)>start) ? 'fixed' : 'static'); $(div).css('top',((p)>start) ? '0px' : ''); doHeightToggle = false; } }); 
+4
source

Source: https://habr.com/ru/post/1437842/


All Articles