Jquery单个页面上的活动导航

Jquery active nav on single page

本文关键字:活动 导航 单个页 Jquery      更新时间:2023-09-26

嗨,我正在尝试实现jsfiddle在这里证明的活动导航功能

http:jsfiddle.net/SJkmh/15/

我想使用现有的css和divs/ul-li将其实现到我的html中,但我一直失败。

我有一个内部jquery-min.js调用,并为每个部分使用div(secion)ID
使用<section id"about></section>可以实现所需的滚动效果,但导航点未激活。

任何帮助都将不胜感激。

提前谢谢。

潜水导航当前如下:

<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插件。航路点。您必须在加载JQuery之后再加载它。

<script src="http://cdn.jsdelivr.net/jquery.waypoints/2.0.2/waypoints.js"></script>