2016-06-17 3 views
0

この設定では、ページが400ピクセルスクロールされたときに#headermenuが固定されるようになっています。しかし、この上のdivは画面の大きさに応じて可変の高さを持ちます。上記のDivが過去にスクロールされたときの固定ヘッダー

#headermenuを上にあるdivの下端(#mixedheightheader)がウィンドウの上部に達したときに#headermenuを固定にするには、JSが必要です。

JSFIDDLE

あなたのための事前のおかげで、私はフィドルあなたを更新した

<div id="mixedheightheader"></div> 

$(function() {     
 
    $('#headermenu'); 
 
}); 
 

 
$(window).scroll(function() {     
 
    if ($(document).scrollTop() < 400)     {         
 
    if ($('#headermenu'))         {             
 
     $('#headermenu');             
 
     $('#headermenu').stop().css({ 
 
     top: '0', 
 
     position: 'relative' 
 
     });         
 
    }     
 
    }     
 
    else     {         
 
    if ($('#headermenu'))         {             
 
     $('#headermenu');             
 
     $('#headermenu').stop().css({ 
 
     top: '0', 
 
     position: 'fixed' 
 
     });         
 
    }       
 
    } 
 
});
body { 
 
    height: 3000px 
 
} 
 

 
#headermenu { 
 
    width: 100%; 
 
    background: black; 
 
    min-height: 100px; 
 
} 
 

 
#mixedheightheader { 
 
    top: 0; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 100vh; 
 
    min-height: 200px; 
 
    overflow: hidden; 
 
    background: grey; 
 
    clear: both; 
 
} 
 

 
#below { 
 
    width: 100%; 
 
    background: darkgrey; 
 
    height: 100px; 
 
    position: relative; 
 
    z-index: -1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="mixedheightheader"></div> 
 
<div id="headermenu"></div> 
 
<div id="below"></div>

答えて

0

を助ける:

https://jsfiddle.net/yLon2oj3/11/

$(function() {     
    var isFixed = false; //This is to not fix if already fixed and reverse 

    $(window).scroll(function() { 
    var mixedHeightHeader = $('#mixedheightheader'); 
    //This uses the offset top position and the height to calculate the bottom of your variable header 
    var mixedHeightHeaderBottom = mixedHeightHeader.offset().top + mixedHeightHeader.height(); 
    var headerMenu = $('#headermenu'); 


    if ($(document).scrollTop() < mixedHeightHeaderBottom && isFixed)     {         
     if (headerMenu) {                   
     headerMenu.css({ 
      top: '0', 
      position: 'relative' 
     });   
     isFixed = false; 
     //this is to remove the placeholder space of the fixed top nav, when its not fixed to top 
     mixedHeightHeader.css('margin-bottom', '0'); 
     }     
    }     
    else  if ($(document).scrollTop() > mixedHeightHeaderBottom && !isFixed)   {         
     if (headerMenu) {                       
     headerMenu.css({ 
      top: '0', 
      position: 'fixed' 
     }); 
     //This holds the position that was occupied by the fixed top nav when it was a relative element, because its now taken out of the flow. 
     mixedHeightHeader.css('margin-bottom', headerMenu.height() + 'px'); 
     } 
     isFixed = true; 
    } 
    }); 

}); 
+0

私はあなたにキスでした!ありがとうございました! – RyanReece

+0

昨年、トップからトップへ向けて多くの作業を行った。 :) お力になれて、嬉しいです。 –

+0

ファインダーを画面から離すまでは、携帯端末では遅れています。この周りに道がありますか? – RyanReece

関連する問題