简体   繁体   English

在页面上突出显示当前菜单项

[英]Highlighting Current Menu Item On Page Scroll

I am building a menu on a web site, and the thing is that I can't realize changing of a selected menu item. 我正在网站上构建菜单,但事实是我无法实现对所选菜单项的更改。 I know that it has to be made by adding some JS, however as I am new in the coding I did not find any solution. 我知道必须通过添加一些JS来完成,但是由于我是编码新手,所以没有找到任何解决方案。

The menu is ready and works fine then I click on an item it switches to active, but when the page starts there is no any active item and not changing when I scroll it using mouse. 菜单已准备就绪,可以正常工作,然后单击一个项目将其切换为活动状态,但是当页面启动时,没有任何活动项目,并且当我使用鼠标滚动该项目时也不会更改。

CSS 的CSS

#menu li {
display:inline-block;
float: right;
margin-top: 8px;
}

#menu {
position:fixed;
z-index: 70;
height: 60px;
width: 100%;
background-color: #FFFFFF;
}
/**home-button**/
#menu #Home-button a:hover {
background-image:url(images/home_hov.gif);
background-repeat: no-repeat;
}
#menu #Home-button a {
padding: 20px 20px;
display:inline-block;
background-image:url(images/home_st.png);
background-repeat: no-repeat;
}
/**about-button**/
#menu #About-button a:hover  {
background-image:url(images/about_hov.gif);
background-repeat: no-repeat;
}
#menu #About-button a {
padding: 20px 20px;
display:inline-block;
background-image:url(images/about_st.png);
background-repeat: no-repeat;
}
/**works-button**/
#menu #Works-button a:hover  {
background-image:url(images/works_hov.gif);
background-repeat: no-repeat;
}
#menu #Works-button a {
padding: 20px 20px;
display:inline-block;
background-image:url(images/works_st.png);
background-repeat: no-repeat;
}
/**contacts-button**/
#menu #Contacts-button a:hover  {
background-image:url(images/contacts_hov.gif);
background-repeat: no-repeat;
}
#menu #Contacts-button a {
padding: 20px 20px;
display:inline-block;
background-image:url(images/contacts_st.png);
background-repeat: no-repeat;
}

/**selected-items**/
#menu #Home-button.active  a {
background-image:url(images/home_act.png);
background-repeat: no-repeat;
}
#menu #About-button.active  a {
background-image:url(images/about_act.png);
background-repeat: no-repeat;
}
#menu #Works-button.active  a {
background-image:url(images/works_act.png);
background-repeat: no-repeat;
}
#menu #Contacts-button.active  a {
background-image:url(images/contacts_act.png);
background-repeat: no-repeat;
}

HTML 的HTML

<nav id="menu">
    <ul>
        <li id="Home-button" ><a id="home" class="hightlight" href="#"></a></li>
        <li id="About-button"><a id="about" class="hightlight" href="#"></a></li>
        <li id="Works-button" ><a id="works" class="hightlight" href="#"></a></li>
        <li id="Contacts-button"><a id="contacts" class="hightlight" href="#"></a></li>
    </ul>
</nav>

JavaScript 的JavaScript

var menu = menu = $('#menu ul');

$('.hightlight').click(function(event) {

    menu.children().removeClass();
    $(this).parent().addClass('active');

});

Please help to resolve the problem. 请帮助解决问题。

Thanks in advance! 提前致谢!

There's a library called skollr.js that let you decide what happen when an element appear on the screen. 有一个名为skollr.js的库,可让您决定当元素出现在屏幕上时会发生什么。 Check it out. 看看这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM