简体   繁体   English

CSS和活动状态导航元素

[英]CSS and active state nav elements

Making an active nav element for a menu isn't too difficult, here is an example. 为菜单创建活动的nav元素不是很困难,这是一个示例。 http://jsfiddle.net/6nEB6/38/ http://jsfiddle.net/6nEB6/38/

<ul>
    <li><a href="" title="Home">Home</a></li>
    <li class="active"><a href="" title="Deals">Deals</a></li>
    <li><a href="" title="Support">Support</a></li>
    <li><a href="" title="Contact">Contact</a></li>
</ul>

CSS: CSS:

html {
  filter: expression(document.execCommand("BackgroundImageCache", false, true));
} 

ul {
    background: url(http://shared.web2works.co.uk/tmp/tab-bg-top.png) no-repeat;
    height: 51px;
    font-family: arial;
    font-size: 14px;
}

ul li {
    float: left;
    height: 51px;
}

ul li a {
    display:block;
    background: url(http://shared.web2works.co.uk/tmp/nav-seperator.gif) no-repeat top right;
    padding: 17px 20px 17px 21px;
    text-decoration: none;
    color: #263e60;
}

ul li:last-child a{
    /*background-image: none;*/
}

ul li:first-child a{
    padding-right: 75px;
}

li.active a, li:hover a {
    background: #02284c; 
    color: #FFF;
    margin-left: -2px;
    padding-left: 23px;
}

What I need is a bit different. 我需要的有点不同。 In most active states people tend to style their menu buttons so you can use the same style for every button. 在大多数活动状态下,人们倾向于为其菜单按钮设置样式,以便您可以对每个按钮使用相同的样式。 I need it so the buttons activate a different image for every state. 我需要它,以便按钮为每种状态激活不同的图像。

This is an image of what I'm talking about: 这是我正在谈论的图像:

在此处输入图片说明

Those buttons have different glow effects which are all different images. 这些按钮具有不同的发光效果,它们都是不同的图像。 When you select a different button the glow should stay active. 当您选择其他按钮时,发光应保持活动状态。 So if I do it this way I'm not able to use the same style for every button. 因此,如果以这种方式进行操作,则无法为每个按钮使用相同的样式。

The buttons change pages and the hovers work correctly, I'm just having trouble with setting the states for each button to active when it reaches it's destination page. 这些按钮会更改页面,并且悬停窗口可以正常工作,我只是在将每个按钮到达其目标页面时将状态设置为活动时遇到了麻烦。 The only state that works is the first button, the home page. 唯一有效的状态是第一个按钮,即主页。

Here is my code(important bits): 这是我的代码(重要位):

<div id="wrapper">
</div>

<div class="menu" id="menunav">
    <ul class="menuul">
        <li><a id="home-link">Home</a>
        </li>

        <li><a id="work-link">Work</a>
        </li>

        <li><a id="about-link">About</a>
        </li>

        <li><a id="contact-link">Contact</a>
        </li>

        <li><a id="resources-link">Resources</a>
        </li>
    </ul>
</div>

CSS: CSS:

.menu {
    height: 50px;
    margin: auto;
    width: 650px;
    text-align:center;
    padding:10px;


}

.menu ul li {
    display: inline-block;
    margin: 0 10px;
}

.menu ul li a {
    display: block;
    text-indent: -99999px;
    cursor: pointer;
    color: #00000;

}

#home-link {
    background: transparent url() no-repeat;
    width: 90px;
}

#home-link:hover, #home-link.current-item {
    background:url() no-repeat;
}

#work-link {
    background: transparent url() no-repeat ;
    width: 90px;
}

#work-link:hover, #about-link.current-item {
    background:url() no-repeat;
}

#about-link {
    background:url() no-repeat;
    width: 90px;
}


#about-link:hover, #services-link.current-item {
    background:url() no-repeat;

}

#contact-link {
    background: transparent url() no-repeat;
    width: 90px;
}

#contact-link:hover, #work-link.current-item {
    background:url() no-repeat;
}

#resources-link {
    background:url() no-repeat;
    width: 100px;
}

#resources-link:hover, #contact-link.current-item {
    background:url() no-repeat;
}

.current-item {
}

JS: JS:

function switchscrollscroll()
{
    var scrolloffset = $("#wrapper").scrollLeft();

    if(scrolloffset == 0 && scrolloffset <= 1999)
    {
        $('#menu ul li a').removeClass('current-item');
        $('#home-link').addClass("current-item");
    }
    else if(scrolloffset >= 2000 && scrolloffset <= 3999)
    {
        $('#menu ul li a').removeClass('current-item');
        $('#work-link').addClass("current-item");
    }
    else if(scrolloffset >= 4000 && scrolloffset <= 5999)
    {
        $('#menu ul li a').removeClass('current-item');
        $('#about-link').addClass("current-item");
    }
    else if(scrolloffset >= 6000 && scrolloffset <= 7999)
    {
        $('#menu ul li a').removeClass('current-item');
        $('#contact-link').addClass("current-item");
    }
    else if(scrolloffset >= 8000 && scrolloffset <= 10000)// Contact
    {
        $('#menu ul li a').removeClass('current-item');
        $('#resources-link').addClass("current-item");
    }
}

switchscroll();
 $("#wrapper").scroll(function(){
    switchscrollcroll();
});
});

Images were taken out on purpose. 图像是故意取出的。 If anyone has done something like this before, I'd appreciate the help. 如果有人以前做过这样的事情,我将非常感谢您的帮助。

You can do this by adding a class to the body. 您可以通过在正文中添加一个类来做到这一点。 For example if you have class work on the body of the work page then your css can look something like 例如,如果你有类work的工作页面的主体,然后你的CSS可以看起来像

.work #work-link {
    background: transparent url(different image) no-repeat ;
}
#work-link {
    background: transparent url(default image) no-repeat ;
    width: 90px;
}

If you don't want to go around editing every page then you can use jquery to figure out which page you are on and add the appropriate class 如果您不想四处编辑每个页面,则可以使用jquery找出您所在的页面并添加适当的类

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

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