JQuery Scroll detection fire once

前端 未结 2 1911
孤城傲影
孤城傲影 2021-01-06 01:12

I have a small piece of JQuery designed to collapse my header, change its dimensions, and then stick it to the side of the screen. It all works apart from one part, the heig

2条回答
  •  执念已碎
    2021-01-06 01:39

    use a flag to check it is 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;
       } 
    });
    

提交回复
热议问题