Trigger event when scroll past bottom of element?

前端 未结 2 668
野性不改
野性不改 2020-12-30 04:41

So on my website I have a static header at the very top of the site -- it\'s not fixed to the top of the viewport. However, what I\'d like to do is once the user scrolls pas

2条回答
  •  爱一瞬间的悲伤
    2020-12-30 05:20

    I think that if you add the height of the div to the top offset you'll get the behaviour you want

    $("#header-2").hide(); // hide the fixed navbar initially
    
    var topofDiv = $("#header-container").offset().top; //gets offset of header
    var height = $("#header-container").outerHeight(); //gets height of header
    
    $(window).scroll(function(){
        if($(window).scrollTop() > (topofDiv + height)){
           $("#header-2").show();
        }
        else{
           $("#header-2").hide();
        }
    });
    

提交回复
热议问题