您好,我正在尝试实现这里从 jsfiddle 证明的事件导航功能
http:jsfiddle.net/SJkmh/15/
我想将它实现到我的 html,使用现有的 css 和 div/ul li,但我一直失败。
我有一个内部 jquery-min.js 调用并使用每个部分的 div(部分)ID。
使用<section id"about></section>
给我想要的滚动效果,但导航点没有激活。
如有任何帮助,我们将不胜感激。
提前致谢。
Div Nav 目前看起来像这样:
<div class="wrapper">
<div class="navi">
<ul>
<li class="about"><a href="#about">About</a></li>
<li class="speakers"><a href="#speakers">Speakers</a></li>
<li class="testimonials"><a href="#event">Testimonials</a></li>
<li class="event"><a href="#event">Event details</a></li>
<li><a href="contact.html" id="pop-contact">Contact</a></li>
</ul>
</div>
<div class="buttons">
<a href="#" class="bt1">Book Now <i class="fa fa-caret-square-o-right"></i></a>
</div>
</div>
这些是我的 CSS 类:
.navi {float:left; padding:17px 0;}
.navi ul {float:left;}
.navi li {font-size:20px; line-height:2.4; font-weight:600; text-shadow:1px 1px 1px rgba(0,0,0,0.2); margin:0 0 0 35px;float: left;}
.navi li {opacity:0.6;}
.navi li:hover {opacity:1;}
.navi li.active {opacity:1;}
这是来自 jfiddle 的 Jquery 代码:
$(document).ready(function(){
$('section').waypoint(function(direction) {
var activeSection = $(this);
if(direction === 'down'){
activeSection = $(this).next();
}
//activeSection = $(this);
var sectionId = activeSection.attr('id');
$('ul li').removeClass('active');
$('ul li.' + sectionId).addClass('active');
console.log(activeSection);
});
});
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - (target.height() / 5)
}, 500);
return false;
}
}
});
最佳答案
您的脚本需要一个外部 Jquery 插件。 Waypoints . 您必须在加载 JQuery 后加载它。
<script src="http://cdn.jsdelivr.net/jquery.waypoints/2.0.2/waypoints.js"></script>
关于javascript - 单页上的 Jquery 事件导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229845/