使用javascript检测当前节
我在一个单页网站上工作。我有一个纯HTML的固定菜单,下面是代码:使用javascript检测当前节,javascript,jquery,html,css,web,Javascript,Jquery,Html,Css,Web,我在一个单页网站上工作。我有一个纯HTML的固定菜单,下面是代码: <header class="header-menu"> <div class="header-menu-inside"> <h1>WM Flying</h1> <nav class="menu menustandard"> <a class="target-section1 current" hre
<header class="header-menu">
<div class="header-menu-inside">
<h1>WM Flying</h1>
<nav class="menu menustandard">
<a class="target-section1 current" href="#section1">Home</a>
<a class="target-section2" href="#section2">About</a>
<a class="target-section3" href="#section3">Portfolio</a>
<a class="target-section4" href="#section4">The team</a>
<a class="target-section5" href="#section5">Contact</a>
</nav>
</div>
</header>
<div class="main" id="main">
<section id="section1" class="home">
</section>
<section id="section2" class="about">
</section>
<section id="section3" class="portfolio">
</section>
<section id="section4" class="team">
</section>
<section id="section5" class="contact">
</section>
</div>
然后我只应用如下css:
nav a.current, nav a:hover{
background: #F0F0F0;
color: #E46C51;
}
要调用该函数,我使用以下命令:
$("#main section").waypoint( function( direction ) {
if( direction === 'down' ) {
change( $( this ) );
}
}, { offset: '20%' } ).waypoint( function( direction ) {
if( direction === 'up' ) {
change( $( this ) );
}
}, { offset: '-20%' } );
但它似乎不起作用。有人能帮我吗
多谢各位
尼古拉斯
$(function () {
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function (event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a target exist?
if (target.length) {
//remove all class 'current-session before add new class'
$(".menustandard").children().removeClass("current-session");
var id_target = target.attr('id');
var current_section_menu = $(".menustandard " + "a[href='#" + id_target + "']");
//add css current section selector
current_section_menu.addClass('current-session');
// Only prevent default if animation is actually gonna happen
event.preventDefault();
}
}
});
});
CSS是
.current-session{
//your style here
}
不起作用?为什么呢它有什么作用?有错误吗?没有,它什么都没做。。。比如如果没有函数被使用,函数被调用了吗?在里面放一个console.log。嗯。。。你到底是如何在这里调用函数的?我现在有一个错误:TypeError:“undefined”不是一个函数(计算“$”(“#main section”)。航路点(函数(方向){。。。
.current-session{
//your style here
}