锚悬停每个锚,除了一个悬停的变色

时间:2018-02-23 18:32:14

标签: javascript html css

当用户将锚点悬停在每个锚点上时,除了一个悬停的更改颜色。可以说有link1 link2 link3。如果用户在link1上悬停,我想要更改link2和link3的颜色。和其他人一样。

到目前为止,我对此进行了尝试link on codpen

 a:hover{color:green}
 #link1:hover{color: originalcolor}
 #link2:hover{color: originalcolor}
 #link3:hover{color: originalcolor}

2 个答案:

答案 0 :(得分:1)

这可以使用像这样的jQuery hover()函数来实现 -



$( "a" ).hover(
  function() {
    $("a").not($(this)).css("color","green");
  },function(){
  $("a").css("color","white");
  });

<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
 <nav class="navbar navbar-inverse navbar-default" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
</div><!-- end container -->
&#13;
&#13;
&#13;

P.S 将代码段展开到全屏以查看效果最佳

答案 1 :(得分:1)

非常确定你可以用纯CSS做到这一点。为更改子项的容器添加悬停规则,但也为要使其不同的子项添加悬停状态规则。

ul li {
  transition: background-color 200ms ease;
}

ul:hover li {
  background-color: lightblue;
}

ul:hover li:hover {
  background-color: yellow;
}
<ul>
  <li>Hello</li>
  <li>World</li>
  <li>Foo</li>
  <li>Bar</li>
<ul>