Twitter Bootstrap导航栏悬停效果 - 过渡边界从中心扩展

时间:2016-08-02 10:21:07

标签: css twitter-bootstrap css3 css-transitions

如何让这个过渡开始从元素的中心移动?

这是我的CSS:

/* Navbar */
.navbar-fixed-top{
    top: 80px;
}
.navbar-default{ /* assigning the top bar to the entire navbar div element */
    background-color: #4F4F4F;
}
nav.navbar {  box-shadow: 0 0 2px 0 #ccc; }
nav.navbar a { color: #FF7D36; }
nav.navbar ul.navbar-nav a { color: #FF7D36; border-style: solid; border-width: 0 0 2px 0; border-color: #000; }
nav.navbar ul.navbar-nav a:hover,
nav.navbar ul.navbar-nav a:visited,
nav.navbar ul.navbar-nav a:focus,
nav.navbar ul.navbar-nav a:active { background: #000; }
nav.navbar ul.navbar-nav a:hover
{
    border-color: #FF7D36;
    transition: border 0.5s ease-in-out;
}

过渡工作正常,但我希望边界从中心向左右扩展...... 谢谢!

  编辑:现在转换工作正常,但我使用twitter bootstrap   navbar ...当我使用小型设备时,现在是链接   垂直定位,a元素中没有填充,   因为它定位绝对...希望你理解。 (所以我有   左侧浏览器侧与a元素之间没有距离)

请参阅此处我的工作示例(当您调整移动设备的浏览器大小时,您可以看到不再有距离)

/* Navbar */
.navbar-fixed-top{
    top: 80px;
}
.navbar-default{ /* assigning the top bar to the entire navbar div element */
    background-color: #4F4F4F;
}
nav.navbar {
    box-shadow: 0 0 2px 0 #ccc;
}
nav.navbar a {
    color: #FF7D36;
}
nav.navbar ul.navbar-nav a {
    color: #FF7D36;
    border-style: solid;
    border-width: 0 0 2px 0;
    border-color: #000;
}
nav.navbar ul.navbar-nav a:hover,
nav.navbar ul.navbar-nav a:visited,
nav.navbar ul.navbar-nav a:focus,
nav.navbar ul.navbar-nav a:active {
    background: #000;
}

nav.navbar ul.navbar-nav a {
    text-decoration: none;
    position: relative;
    display: inline-block;
}
nav.navbar ul.navbar-nav a::after {
    content: '';
    width: 100%;
    position: absolute;
    top: 100%;
    left: 0;
    height: 2px;
    background-color: #FF7D36;
    transform: scaleX(0);
    transition: transform .35s ease;
}
nav.navbar ul.navbar-nav a:hover::after {
    transform: scaleX(1);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic Bootstrap Template</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional Bootstrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Navigation ein-/ausblenden</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Projekt-Titel</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Start</a></li>
            <li><a href="#ueber">Über</a></li>
            <li><a href="#kontakt">Kontakt</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>                                		

1 个答案:

答案 0 :(得分:1)

  

我希望边框从中心向左右扩展。

你无法过渡这样的边界。

我建议使用伪元素和转换变换比例。

&#13;
&#13;
a {
  text-decoration: none;
  position: relative;
  display: inline-block;
}
a::after {
  content: '';
  width: 100%;
  position: absolute;
  top: 100%;
  left: 0;
  height: 2px;
  background: red;
  transform: scaleX(0);
  transition: transform .35s ease;
}
a:hover::after {
  transform: scaleX(1);
}
&#13;
<a href="#1">SOME LINK</a>
&#13;
&#13;
&#13;

或者,可以转换 width ,但仍需要转换(这次是转换)以保持居中。

&#13;
&#13;
a {
  text-decoration: none;
  position: relative;
  display: inline-block;
}
a::after {
  content: '';
  width: 0%;
  position: absolute;
  top: 100%;
  left: 50%;
  height: 2px;
  background: red;
  transform: translateX(-50%);
  transition: width .5s ease;
}
a:hover::after {
  width: 100%;
}
&#13;
<a href="#1">SOME LINK</a>
&#13;
&#13;
&#13;

修改

据我所知,当导航在较低的视口宽度处垂直时,边界&#34;可能需要稍微宽一点。

这可以通过改变宽度和调整定位来完成......例如

nav.navbar ul.navbar-nav a::after {
    content: '';
    width: 80%;
    position: absolute;
    top: 100%;
    left: 10%; /* (100% - 80%) / 2 */
    height: 2px;
    background-color: #FF7D36;
    transform: scaleX(0);
    transition: transform .35s ease;
}

Codepen Demo