简体   繁体   English

更改导航栏背景颜色?

[英]Changing Navigation Bar Background Colour?

I am currently trying to design a navigation bar for one of my websites. 我目前正在尝试为我的一个网站设计一个导航栏。 This navigation bar will be across the top of the webpage. 该导航栏将位于网页顶部。

The problem is that I can get the items to change colour when I hover over them, but cannot get the items to stay in the hover state once the page is active. 问题在于,当我将项目悬停在项目上时,它们可以更改颜色,但是一旦页面处于活动状态,就无法使项目保持在悬停状态。 So for example, if I wanted to go "Home", I would hover over the "Home" item in the navigation bar, and it would turn gray, when I click on it to go back to "index.php" the "Home" in the navigation bar would stay gray. 因此,例如,如果我想转到“主页”,则将鼠标悬停在导航栏中的“主页”项上,并且当我单击它以返回到“ index.php”时,它会变成灰色。导航栏中的“”将保持灰色。 (With the background of the navigation bar being black). (导航栏的背景为黑色)。

I have formatted a table with a single row, and a few pieces of data, which will serve as links. 我已经格式化了一个表格,其中只有一行,并且有一些数据,这些数据将用作链接。 This is what the HTML code kind of looks like: 这是HTML代码的样子:

<div id="header_banner">
<table id="header_banner_table">
    <tr id="header_banner_row">
        <td id="header_banner_a"><a class ="link1" href="">Item 1</a></td>
        <td id="header_banner_b"><a class ="link1" href="">Item 2</a></td>
        <td id="header_banner_c"><a class ="link1" href="">Item 3</a></td>
        <td id="header_banner_d"><a class ="link1" href="">Item 4</a></td>
        <td id="header_banner_e"><a class ="link1" href="">Item 5</a></td>
        <td id="header_banner_f"><a class ="link1" href="">Item 6</a></td>
        <td id="header_banner_g"><a class ="link1" href="">Item 7</a></td>
    </tr>
</table>
</div>

Here is some of the CSS: 以下是一些CSS:

#header_banner {
    display: block;
    position: fixed;
    top: 0px;
    left: 0px;
    right: 0px;
    width: 100%;
    height: 71px;
    background-color: black;
    color: white;
    font-size: 18px;
    text-align:center;
    text-transform: uppercase;
    text-decoration: none;
}

#header_banner_table {
    position: fixed;
    top: 0px;
    width: 60%;
    padding: 0px;
    margin-left: 20%;
    margin-right: 20%;
}

.link1 {
position: relative;
display: block;
color: white;
text-decoration: none;
padding-top: 23px;
padding-bottom: 23px;
padding-left: 20px;
padding-right: 20px;
}

.link1:hover{
position: relative;
display: block;
text-decoration: none;
color: white;
padding-top: 23px;
padding-bottom: 23px;
padding-left: 20px;
padding-right: 20px;
background-color: gray;
}

So currently the background of the navigation bar is black, and when you hover over something, it turns gray. 因此,当前导航栏的背景为黑色,当您将鼠标悬停在某物上时,它会变成灰色。 I would like it to stay gray when that page is active. 我希望该页面处于活动状态时保持灰色。

Any solutions? 有什么办法吗?

Thank you in advance. 先感谢您。

Try 尝试

:focus 

or 要么

:active

But I think it needs to be clicked on. 但是我认为它需要被点击。

Are you familiar with jQuery? 您熟悉jQuery吗? If I'm thinking the same thing as you are you should be able to do it with jQuery. 如果我在想与您相同的事情,则应该可以使用jQuery。

var path = window.location.pathname;

$('#header_banner_row a').each(function(){
    if( $(this).attr('href') === path ){
        $(this).addClass('active');
    }
});

So basically, var path is whatever is after .com in the navigation bar in your browser. 因此,基本上, var path就是浏览器导航栏中.com之后的内容。 And if that path is the same as one of the a in the header, it will get a class of active that you can style to be gray. 而且,如果该路径与标题中的a相同,则它将获得一类活动类别,您可以将其设置为灰色。

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

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