[英]CSS hover off transition not working
I am making navigation buttons for a portfolio website, hovering works perfectly but once the mouse hovers off, the button pops back to the original position instead of transitioning back slowly. 我正在为一个投资组合网站制作导航按钮,悬停工作完美但是一旦鼠标悬停,按钮就会弹回到原始位置而不是慢慢转换回来。
Here is the code: 这是代码:
li { list-style-type: none; margin: 20px 5px 0px 5px; } nav ul li a { display: block; margin: 30px 10px; text-decoration: none !important; text-align: center; width: 90px; padding: 6px 0; border-radius: 4px; -o-transition: all 300ms ease-in-out; -ms-transition: all 300ms ease-in-out; -webkit-transition: all 300ms ease-in-out; -moz-transition: all 300ms ease-in-out; transition: all 300ms ease-in-out; background: #E6E5CC; float: right; top: 0px; left: 0px; } nav ul li a:hover { color: inherit; position: relative; top: 0px; right: 0; bottom: 0; left: 0px; top: -5px; left: -5px; box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a; background: #f7f6ee; } nav ul li a:active { color: inherit; top: 5px; left: 5px; box-shadow: 0px 0px 5px white; }
<nav> <ul> <li> <a href="#">Contact</a> </li> <li> <a href="#">Projects</a> </li> <li> <a href="#">Blog</a> </li> <li> <a href="#">About</a> </li> </ul> </nav>
Just set your nav ul li a
to position: relative;
只需将你的
nav ul li a
设置为position: relative;
and it will work as expected. 它将按预期工作。
CSS CSS
nav ul li a {
position: relative;
}
li { list-style-type: none; margin: 20px 5px 0px 5px; } nav ul li a { display: block; margin: 30px 10px; text-decoration: none !important; text-align: center; width: 90px; padding: 6px 0; border-radius: 4px; -o-transition: all 300ms ease-in-out; -ms-transition: all 300ms ease-in-out; -webkit-transition: all 300ms ease-in-out; -moz-transition: all 300ms ease-in-out; transition: all 300ms ease-in-out; background: #E6E5CC; float: right; top: 0px; left: 0px; position: relative; } nav ul li a:hover { color: inherit; position: relative; top: 0px; right: 0; bottom: 0; left: 0px; top: -5px; left: -5px; box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a; background: #f7f6ee; } nav ul li a:active { color: inherit; top: 5px; left: 5px; box-shadow: 0px 0px 5px white; }
<nav> <ul> <li> <a href="#">Contact</a> </li> <li> <a href="#">Projects</a> </li> <li> <a href="#">Blog</a> </li> <li> <a href="#">About</a> </li> </ul> </nav>
You had the position:relative
statement in the :hover
rule whereas it should have been in the original state rule. 您在
:hover
规则中具有position:relative
语句,而它应该处于原始状态规则中。 Also, you had some repeated overlapping positional values. 此外,您有一些重复的重叠位置值。
li { list-style-type: none; margin: 20px 5px 0px 5px; } nav ul li a { display: block; margin: 30px 10px; text-decoration: none !important; text-align: center; width: 90px; padding: 6px 0; border-radius: 4px; -o-transition: all 300ms ease-in-out; -ms-transition: all 300ms ease-in-out; -webkit-transition: all 300ms ease-in-out; -moz-transition: all 300ms ease-in-out; transition: all 300ms ease-in-out; background: #E6E5CC; float: right; position: relative; top: 0px; left: 0px; } nav ul li a:hover { color: inherit; top: -5px; left: -5px; box-shadow: 1px 1px 0px #e3e2c5, 2px 2px 0px #dfdebe, 3px 3px 0px #cbc995, 4px 4px 0px #b4b165, 5px 5px 0px #56542a; background: #f7f6ee; } nav ul li a:active { color: inherit; top: 5px; left: 5px; box-shadow: 0px 0px 5px white; }
<nav> <ul> <li> <a href="#">Contact</a> </li> <li> <a href="#">Projects</a> </li> <li> <a href="#">Blog</a> </li> <li> <a href="#">About</a> </li> </ul> </nav>
set nav ul li a
to position: relative;
将
nav ul li a
设置为position: relative;
like this: 像这样:
nav ul li a {
display: block;
margin: 30px 10px;
text-decoration: none !important;
text-align: center;
width: 90px;
padding: 6px 0;
border-radius: 4px;
-o-transition: all 300ms ease-in-out;
-ms-transition: all 300ms ease-in-out;
-webkit-transition: all 300ms ease-in-out;
-moz-transition: all 300ms ease-in-out;
transition: all 300ms ease-in-out;
background: #E6E5CC;
float: right;
top: 0px;
left: 0px;
position: relative;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.