2017-04-18 31 views
3

從頂部標題修復滾動不起作用。滾動標題固定jQuery代碼不起作用

代碼段:

$(document).ready(function() { 
 
    $(window).scroll(function() { 
 
    if ($(this).scrollTop() > 350) { 
 
     $('.header-nav').addClass('fixed2'); 
 
    } else { 
 
     $('.header-nav').removeClass('fixed2'); 
 
    } 
 
    }); 
 
});
.fixed2 { 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    background-color: cornflowerblue; 
 
} 
 

 
html { 
 
    min-height: 1000px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="header-nav"> 
 
    <ul id="main-nav"> 
 
    <li><a href="index.html" class="active">Home</a></li> 
 
    <li><a href="about.html">About</a></li> 
 
    <li><a href="#">Register Your Business</a></li> 
 
    </ul> 
 
</div>

jQuery代碼不衝突,我用$ jQuery的。

jQuery不工作。

+0

你真的想與滾動這裏做什麼? –

+4

比'_not working _'更具體,你能提供一個解決問題的小工具嗎? –

+0

您是否檢查了控制檯的錯誤? – Pete

回答

-1

此代碼將幫助您。

$(document).ready(function() { 
 
     $(window).scroll(function() { 
 
      if ($(this).scrollTop() > 50) { 
 
       $('.header-nav').addClass('fixed2'); 
 
       $('.header-nav').css('top', Math.round($(this).scrollTop()) + 'px'); 
 
      } else { 
 
       $('.header-nav').removeClass('fixed2'); 
 
      } 
 
     }); 
 
    });
.fixed2 { 
 
     position: absolute; 
 
     top: 0; 
 
     left: 0; 
 
     width: 100%; 
 
    }

 

 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <div class="header-nav"> 
 
     <ul id="main-nav"> 
 

 
      <li><a href="index.html" class="active">Home</a></li> 
 
      <li><a href="about.html">About</a></li> 
 
      <li><a href="#"> Register Your Busi</a></li> 
 
     </ul> 
 
    </div> 
 

 
    <div style="height:1000px;">&nbsp;</div>

+0

你改變了什麼,它有什麼不同? – Adam

+0

你可以看到我已經改變位置固定爲絕對和設置菜單的頂部位置時滾動 –