CSS导航栏滑块高亮显示动画?

时间:2016-11-03 23:44:13

标签: html css animation css-animations navbar

我正在尝试创建一个带有下划线的顶部导航栏,该下划线在悬停时在每个链接/按钮下方滑动。我猜测过渡或关键帧都可行,但我似乎无法使其发挥作用。

这是我最好的拍摄,但我只能让父母导航或div触发器而不是实际链接本身,而且我也无法让条形滑动到相对于该位置的位置链接(现在它只是将500px翻译到右边)。

非常感谢任何帮助!



#header {
	position: fixed;
	background-color: rgba(255, 255, 255, 0.7);
	width: 100%;
	top: 0px;
	left: 0px;
  z-index: 1;
  text-align: center;
  padding: 15px;
}

.nav ul li {
  font-family: 'Exo', sans-serif;
  text-transform: uppercase;
  list-style-type: none;
  display: inline;
  padding: 11px;
}

.slider {
	position: absolute;
	bottom: 0px;
	left: -50px;
	width: 50px;
	height: 5px;
	background-color: rgba(52, 152, 219, 0.3);
	transition-property: transform;
	transition-duration: 300ms;
	transition-timing-function: ease-in-out;
	transition-delay: 0s;
}

.nav:hover .slider {
	  transform: translate(500px);
}

.nav ul li a {
  text-decoration: none;
  margin: 0px 30px 0px 30px;
  color: rgba(0, 0, 0, 0.5);

<header id="header">
    			<nav class="nav">
    				<ul class="list">
              <div class="slider"></div>
    					<li class="btn"><a href="#home">Home</a></li>
    					<li class="btn"><a href="#about">About</a></li>
    					<li class="btn"><a href="#portfolio">Portfolio</a></li>
    					<li class="btn"><a href="#contact">Contact</a></li>
    				</ul>
    			</nav>
  	    </header>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

你可以选择这样的东西吗?

&#13;
&#13;
$('li').on('click', function() {
  $('.current').removeClass('current');
  $(this).addClass('current');
}).has("a[href*='" + location.pathname + "']").addClass("current");
&#13;
  .nav {
    width: 50%;
    margin: 0 auto;
    font-family: 'Exo', sans-serif;
    text-transform: uppercase;
    list-style-type: none;
    display: inline;
    padding: 11px;
  }

ul li {
    display: inline;
    text-align: center;
  }

  a {
  display: inline-block;
  width: 25%;
  margin: 0;
  text-decoration: none;
  color: black;
 }

 .home.current ~ hr,
   ul li.home:hover ~ hr {
   margin-left: 0%;
 }
   
  .about.current ~ hr,
   li.about:hover ~ hr {
      margin-left: 25%;
   }
  
  .portfolio.current ~ hr,
   li.portfolio:hover ~ hr {
    margin-left: 50%;
   }

  .contact.current ~ hr,
   li.contact:hover ~ hr {
   margin-left: 75%;
   }

hr {
 height: 3px;
 width: 25%;
 margin: 0;
 background-color: rgba(52, 152, 219, 0.3);
 transition-property: transform;
 transition: .3s ease-in-out;
   }
&#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header id="header">
  <div class="nav">
    <ul>
      <li class="home"><a href="#home">Home</a></li><!--
   --><li class="about"><a href="#about">About</a></li><!--
   --><li class="portfolio"><a href="#portfolio">Portfolio</a></li><!--
   --><li class="contact"><a href="#contact">Contact</a></li>
     <hr />
    </ul>
 </div>
</header>
&#13;
&#13;
&#13;

或者您是否希望下划线出现在屏幕旁边?

编辑:添加了js / jQuery,点击菜单项。