gpt4 book ai didi

twitter-bootstrap - Twitter Bootstrap navbar 悬停效果 - 过渡边框从中心展开

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:00 25 4
gpt4 key购买 nike

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

这是我的 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;
}

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

EDIT: The transition works now, but I use the twitter bootstrap navbar... and when I use a small device and the links are now vertically positioned, there is no padding-left in the a element, because it is positioning absolute... hope you understand. (so I have no distance between the left browser side and the a element)

看这里我的工作示例(当你调整移动设备浏览器的大小时,你可以看到不再有距离)

/* 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>

最佳答案

I want that the border expand from the center to left and right.

你不能像那样转换边框。

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

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);
}
<a href="#1">SOME LINK</a>

或者,您可以转换宽度,但仍然需要转换(这次是平移)来保持居中。

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%;
}
<a href="#1">SOME LINK</a>

编辑

我知道当导航在较低的视口(viewport)宽度下垂直时,“边框”可能需要稍微变窄。

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

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

关于twitter-bootstrap - Twitter Bootstrap navbar 悬停效果 - 过渡边框从中心展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com