JQuery Scroll detection fire once

前端 未结 2 1909
孤城傲影
孤城傲影 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:33

    look at jquery's one() functionality http://api.jquery.com/one/

    It will only fire once and then promptly remove itself..

    0 讨论(0)
  • 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;
       } 
    });
    
    0 讨论(0)
提交回复
热议问题