简体   繁体   English

更改css中的活动导航栏颜色

[英]Change the active navbar color in css

I am using a simple top css navbar(just a css and html, without bootstrap/other framework) and i would like to change the active page. 我正在使用一个简单的顶级css导航栏(只是一个CSS和HTML,没有bootstrap /其他框架),我想更改活动页面。 So when i go to the home page, the button color in navbar changes into red/whatever, likewise when i go to the other page... 因此,当我转到主页时,导航栏中的按钮颜色变为红色/无论如何,当我转到另一页时...

here the code: 这里的代码:

 body { margin: 0; } .logo { margin: 0; padding: 0; overflow: hidden; background-color: #f2f2f2; float: left; width: 25%; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 40px; text-decoration: none; font-size: 18px; } li a:hover { background-color: #111; } 
 <ul> <li><a href="#">Home</a></li> <li><a href="#">News</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li><a href="#">Division</a></li> <li><a href="#">Career</a></li> <li><a href="#">MChoice's</a></li> </ul> 

do you have an idea? 你有想法吗? it's ok to add javascript 可以添加javascript

Thanks a lot! 非常感谢!

What I did here is when $(document).ready(function() {..} get the path using var url = window.location.pathname; so you know which link the user coming from therefore you know which menu item they clicked. 我在这里做的是当$(document).ready(function() {..}使用var url = window.location.pathname;获取路径var url = window.location.pathname;因此你知道用户来自哪个链接,因此你知道他们点击了哪个菜单项。

Then $('ul li a').each(function() {...} will check each menu item, try to match the url path with the menu's href attributes, if a match found, make that menu item active (with css active class added), if not match remove the active class if any. That should do the trick. 然后$('ul li a').each(function() {...}将检查每个菜单项,尝试将url路径与菜单的href属性match ,如果找到match项,则使该菜单项处于活动状态(与css活动类已添加),如果not match删除活动类(如果有)。这应该可以解决问题。

(note: assume your app is not single page app) (注意:假设您的应用不是单页应用)

for Single page app it is much easier, deactive all menu item then active the one you clicked. 对于单页面应用程序,它更容易,所有菜单项都取消激活,然后激活您单击的那个。

 $(document).ready(function() { //var url = window.location.pathname; var url = 'http://stacksnippets.net/js#division'; console.log('url-->', url); $('ul li a').each(function() { var href = $(this).attr('href'); if (!!url.match(href)) { $(this).addClass('active'); } else { $(this).removeClass('active'); } }); }); 
 body { margin: 0; } .logo { margin: 0; padding: 0; overflow: hidden; background-color: #f2f2f2; float: left; width: 25%; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 40px; text-decoration: none; font-size: 18px; } li a:hover { background-color: #111; } .active { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> <li><a href="#division">Division</a></li> <li><a href="#career">Career</a></li> <li><a href="#mchoices">MChoice's</a></li> </ul> 

try below code for active menu 尝试下面的活动菜单代码

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
   $('li a').on('click', function(){
      $('li a').removeClass('active');
      $(this).addClass('active');
    }); 
});
</script>

<style type="text/css">
body {
  margin: 0;
}

.logo {
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #f2f2f2;
  float: left;
  width: 25%;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 40px;
  text-decoration: none;
  font-size: 18px;
}

li a:hover, li a.active {
  background-color: #111;
}

</style>
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">News</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Division</a></li>
  <li><a href="#">Career</a></li>
  <li><a href="#">MChoice's</a></li>
</ul>

The simplest solution would be to add an active class to the link of the page you're on: 最简单的解决方案是将活动类添加到您所在页面的链接:

<ul>
  <li><a href="#" class="active">Home</a></li>
  <li><a href="#">News</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Division</a></li>
  <li><a href="#">Career</a></li>
  <li><a href="#">MChoice's</a></li>
</ul>

Then style those that class accordingly: 然后相应地为那些类设置样式:

li a.active {
    background: #F00;
}

If you're using a CMS (Wordpress, etc), adding some sort of active class on the active link is usually done for you. 如果您使用的是CMS(Wordpress等),通常会在活动链接上添加某种活动类。 If you're doing your own static HTML, you would have to do it manually. 如果您正在使用自己的静态HTML,则必须手动执行此操作。

To change the color of active link in your navigation you need to do the following things: 要更改导航中活动链接的颜色,您需要执行以下操作:

  1. On click of navigation link add css class: 点击导航链接添加css类:
$('ul li a').click(function(){
    $('li a').removeClass("active");
    $(this).addClass("active");
});
  1. Add CSS for active class 为活动类添加CSS
ul li a.active {
    background-color: #f4f4f4;
}

HTML Part HTML部分

 <div class="navbar">
    <div class="navbar-fixed-top">
       <div class="container" style="width: auto;">
          <div class="nav-collapse" id="nav-collapse">
             <ul class="nav" id="nav">
                 <li id="News"><a href="#News">News</a></li>
                 <li id="Contact"><a href="#Contact">Contact</a></li>
                 <li id="About"><a href="#About">About</a></li>
                 <li id="Division"><a href="#Division">Division</a></li>
                 <li id="Career"><a href="#Career">Career</a></li>
                 <li id="skill"><a href="#skill">Skill</a></li>
                 <li id="research"><a href="#research">Research</a></li>
                 <li id="MChoice"><a href="#MChoice">MChoice's</a></li>                     
             </ul>
          </div>
       </div>
    </div>
 </div>

JavaScript part JavaScript部分

$(function() {
        $('#nav li a').click(function() {
           $('#nav li').removeClass();
           $($(this).attr('href')).addClass('active');
        });
     });

CSS Part CSS部分

.navbar #nav > .active > a {
    color: yellow;
}

here is JSFiddle result 这是JSFiddle结果

http://jsfiddle.net/Ag47D/775/ http://jsfiddle.net/Ag47D/775/

 function redButtons() { $(".inclusive-buttons").on("click", "a", function() { $(".inclusive-buttons a").css("background", "#333"); $(this).css("background", "red"); }) } var x = document.getElementsByTagName("li"); x.onclick = redButtons(); 
 body { margin: 0; } .logo { margin: 0; padding: 0; overflow: hidden; background-color: #f2f2f2; float: left; width: 25%; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 40px; text-decoration: none; font-size: 18px; } li a:hover { background-color: #111; } a:active { background-color: red; } 
 <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <ul class="inclusive-buttons"> <li><a href="#">Home</a></li> <li><a href="#">News</a></li> <li><a href="#">Contact</a></li> <li><a href="#">About</a></li> <li><a href="#">Division</a></li> <li><a href="#">Career</a></li> <li><a href="#">MChoice's</a></li> </ul> 

https://jsfiddle.net/m5gm7x7e/2/ https://jsfiddle.net/m5gm7x7e/2/

One possible way is to use the active selector in CSS. 一种可能的方法是在CSS中使用活动选择器。 This selector highlights the active element you are using when its clicked. 此选择器突出显示单击时使用的活动元素。

a:active { 
    background-color: yellow;
}

a:focus { 
    background-color: yellow;
}

You can use some JQuery to turn it on and off too. 您也可以使用一些JQuery打开和关闭它。 Try looking at this post here, I think you may have get your answer. 试着看看这篇文章,我想你可能得到了答案。

(Related to How to keep :active css style after clicking an element ) (与如何保持相关:单击元素后的活动css样式

jQuery('button').click(function(){
   jQuery(this).toggleClass('active');
});

Here's a JSFiddle: https://jsfiddle.net/timhjellum/nw3n7eka/103/ 这是一个JSFiddle: https ://jsfiddle.net/timhjellum/nw3n7eka/103/

This is a jQuery option which looks at the page URL (window.location) and specifically for a string which you define in the .indexOf(" add a unique string here ") and asks if that string is greater than -1, then locate the li element with the class you assigned to it, and add another class called active. 这是一个jQuery选项,它查看页面URL(window.location),特别是你在.indexOf中定义的字符串(“ 在这里添加一个唯一的字符串 ”)并询问该字符串是否大于-1,然后找到带有您分配给它的类的li元素,并添加另一个名为active的类。

In the example I'm using "display" because that the URL for that iFrame that JSFiddle uses so hopefully that's not confusing. 在示例中,我使用“display”,因为JSFiddle使用的iFrame的URL有希望这不会让人困惑。

Here's the navigation: 这是导航:

$(document).ready(function () {
     if(window.location.href.indexOf("home") > -1) {
         $(".home").addClass("active");
     }
     if(window.location.href.indexOf("display") > -1) {
         $(".news").addClass("active");
     }
     //make one for each nav element
});

The HTML needs to be modified like: HTML需要修改如下:

<ul>
  <li class="home"><a href="index">Home</a></li>
  <li class="news"><a href="display">News</a></li>
  <li class="contact"><a href="contact">Contact</a></li>
  <li class="about"><a href="about">About</a></li>
</ul>

And then a simple css addition: 然后是一个简单的css补充:

li.active {
  background-color: white;
}
li.active a {
  color: black;
}

If you can't use jQuery, let me know but this is the easiest solution for you to implement and allow you to easily modify 如果您不能使用jQuery,请告诉我,但这是您实施的最简单的解决方案,并允许您轻松修改

You could try having separate classes in your CSS file, like "ul-home," "ul-news," etc. and define different background colors for each, then simply set the class for your <ul> tag on each page to match the class you want. 您可以尝试在CSS文件中使用单独的类,例如“ul-home”,“ul-news”等,并为每个类定义不同的背景颜色,然后在每个页面上设置<ul>标记的类以匹配你想要的课程。 So: 所以:

.ul-home {
    background-color: red;
}
.ul-news {
    backrgound-color: yellow;
}

And then on your home page: 然后在您的主页上:

<ul class="ul-home>
    <li>Home</li>
    <li>News</li>
</ul>

On your news page: 在您的新闻页面上:

<ul class="ul-news">
    <li>Home</li>
    <li>News</li>
</ul>

Etc. with all the other pages you have. 等等你拥有的所有其他页面。

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

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