1
我有一個網站,它有一個粘性導航放置在<header>
元素的底部,只要我滾動到一個部分它使用data-attributes
活躍一個類,這裏的錯誤是:當我滾動類active
甚至上滾動並根據部分不滾動部分的一半補充道。導航錨不正確應用活動類,而滾動jQuery
我想要的是打開active
類,只要我得到每個部分的錨,我留下我的代碼下面跟着一個jsfiddle你可以看到什麼問題,我希望你們可以幫助我。
的HTML:
<header class="testheader">
<nav id="cloud_nav" class="absolute">
<ul>
<li><a href="#" data-scroll="overview">Section 1</a></li>
<li><a href="#" data-scroll="readiness">Section 2</a></li>
<li><a href="#" data-scroll="collaboration">Section 3</a></li>
</ul>
</nav>
</header>
<section data-anchor="overview" style="background: red; font-size: 25px;">
</section>
<section data-anchor="readiness" style="background: green; font-size: 25px;">
</section>
<section data-anchor="collaboration" style="background: #ccc; font-size: 25px;">
</section>
</div>
的Javascript:
<script type="text/javascript">
// Sticky Nav
$('#cloud_nav a').on('click', function() {
var scrollAnchor = $(this).attr('data-scroll'),
scrollPoint = $('section[data-anchor="' + scrollAnchor + '"]').offset().top;
$('body,html').animate({
scrollTop: scrollPoint
}, 500);
return false;
})
var navOffset = $('#cloud_nav').offset().top;
$(window).scroll(function(){
var scrollPos = $(window).scrollTop();
if (scrollPos >= navOffset){
$('#cloud_nav').removeClass('absolute');
$('#cloud_nav').addClass('fixed_cloud_nav');
$('section').each(function(i) {
if ($(this).position().top <= scrollPos - 50) {
$('#cloud_nav a.active').removeClass('active');
$('#cloud_nav a').eq(i).addClass('active');
}
});
} else {
$('#cloud_nav').removeClass('fixed_cloud_nav');
$('#cloud_nav').addClass('absolute');
$('#cloud_nav a.active').removeClass('active');
$('#cloud_nav a:first').addClass('active');
}
});
</script>
小提琴:提前 http://jsfiddle.net/qfaeqo2w/
感謝,問候。
驚人!,謝謝你的評論,我已經花了這太多的時間,所以再次感謝您! – Alex 2014-10-17 21:02:47