本文介绍了CSS:悬停时设置动画边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建带有边框的菜单(请参见 http://multijuice.org/),并且我需要将悬停时的边框从活动元素移到 hover ed.我该怎么做?也许有人知道一些教程吗?

I'm trying to create menu with border (see http://multijuice.org/), and I need to move border on hover from active element to hovered. How I can do this? Maybe, anybody know some tutorials?

UPD:完成!请参见此js文件以获取答案.谢谢大家!

UPD: Done! See this js file for answer. Thanks all!

推荐答案

演示- http://jsfiddle.net/victor_007/gujrud4y/

您可以使用类似的方法,但是您需要更改每个页面上带下划线的位置,或者可以使用jQuery检查哪个< li> 具有活动类并进行相应设置

You can use something like this but you need to change the position of underlined on every page or you can use jQuery to check which <li> has active class and set accordingly

nav {
  text-align: center;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: inline-block;
  position: relative;
  font-size: 0px;
}
ul li {
  display: inline-block;
  padding: 8px 0;
  width: 80px;
  text-align: center;
  font-size: 15px;
  cursor: pointer;
}
span.border {
  height: 2px;
  background: red;
  width: 80px;
  position: absolute;
  left: 80px;
  bottom: 0;
  transition: .5s linear;
}
li:nth-child(1):hover ~ span.border {
  left: 0px;
}
li:nth-child(2):hover ~ span.border {
  left: 80px;
}
li:nth-child(3):hover ~ span.border {
  left: 160px;
}
li:nth-child(4):hover ~ span.border {
  left: 240px;
}
<nav>
  <ul>
    <li>one</li>
    <li class="active">two</li>
    <li>three</li>
    <li>four</li>
    <span class="border"></span>
  </ul>
</nav>

这篇关于CSS:悬停时设置动画边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:24