javascript CSS 过渡淡入原始文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22596028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:26:36  来源:igfitidea点击:

CSS transition fade back to original text color

javascriptjquerycss

提问by user3453103

Well I have successfully made my text transition to a different color with a 1s delay, but upon the mouse no longer hovering over the element, I cannot figure out how to make it do a transition back to the original color without going to it immediately.

好吧,我已经成功地使我的文本以 1 秒的延迟转换为不同的颜色,但是当鼠标不再悬停在元素上时,我无法弄清楚如何在不立即转到原始颜色的情况下使其转换回原始颜色。

I looked around online and cannot find much to help. If anything that I found, I couldn't figure out how to piece it together to make it work properly.

我在网上环顾四周,找不到太多帮助。如果我发现了什么,我无法弄清楚如何将它拼凑在一起以使其正常工作。

HTML:

HTML:

<div id="navbar">
<div id="navlinks">
<nav>
<ul id="navlist">
<li><a href="#">HOME</a></li>
<li><a href="#">ASSIGNMENTS</a></li>
<li><a href="#">DREAM CARS</a></li>
<li><a href="#">PROJECTS</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</nav>
</div>
</div>

CSS:

CSS:

body{
margin:0 auto;
}
#logo{
margin:auto;
width:430px; 
}
#navbar{
width:100%;
height:50px;
display:table;
margin:auto;
}
#navlinks ul {
display:table;
border-collapse:collapse;
width:100%;
margin:0 0 20px;
padding:0;
list-style:none;
}
#navlinks li {
display: table-cell;
vertical-align: middle;
text-align:center;
width:20%;
background: linear-gradient(to right, #111 50%, #444 50%);
background-size: 200% 100%;
background-position:left top;
transition:all 1s ease;
-webkit-transition-delay:all 1s ease;
-moz-transition-delay:all 1s ease;
-ms-transition-delay:all 1s ease;
-o-transition-delay:all 1s ease;
}
#navlinks li:hover{
background-position:right top;
}
#navlinks a {
text-decoration:none;
color: #999;
text-transform: uppercase;
display:block;
padding-top:10px;
padding-bottom:10px;
font: bold 12px/25px Arial, Helvetica;
}

#navlinks a:hover{
color:black;
transition:all 1s ease;
-webkit-transition-delay:all 1s ease;
-moz-transition-delay:all 1s ease;
-ms-transition-delay:all 1s ease;
-o-transition-delay:all 1s ease;
}

I also attempted to follow jquery tutorials on this, and it simply had done nothing to the pages at all.

我也尝试在这方面遵循 jquery 教程,但它根本没有对页面做任何事情。

Demo in Jsfiddle

在 Jsfiddle 中演示

回答by Gildas.Tambo

#navlinks a {
transition:color 1s ease;/*i just moved this from anchor*/
}

#navlinks li:hover a{
color:black;
}

WORKING DEMO

工作演示

body{
  margin:0 auto;
}
#logo{
  margin:auto;
  width:430px; 
}
#navbar{
  width:100%;
  height:50px;
  display:table;
  margin:auto;
}
#navlinks ul {
  display:table;
  border-collapse:collapse;
  width:100%;
  margin:0 0 20px;
  padding:0;
  list-style:none;
}
#navlinks li {
  display: table-cell;
  vertical-align: middle;
  text-align:center;
  width:20%;
  background: linear-gradient(to right, #111 50%, #444 50%);
  background-size: 200% 100%;
  background-position:left top;
  transition:all 1s ease;
}
#navlinks li:hover{
  background-position:right top;
}
#navlinks a {
  text-decoration:none;
  color: #999;
  text-transform: uppercase;
  display:block;
  padding-top:10px;
  padding-bottom:10px;
  font: bold 12px/25px Arial, Helvetica;
  transition:color 1s ease-in-out;
}

#navlinks li:hover a{
  color:black;

}
<div id="navbar">
  <div id="navlinks">
    <nav>
      <ul id="navlist">
        <li><a href="#">HOME</a></li>
        <li><a href="#">ASSIGNMENTS</a></li>
        <li><a href="#">DREAM CARS</a></li>
        <li><a href="#">PROJECTS</a></li>
        <li><a href="#">CONTACT</a></li>
      </ul>
    </nav>
  </div>
</div>

回答by Chirag Bhatia - chirag64

The transitions should not be in the hover selector in CSS, but in the element selector without any modified state.

过渡不应该在 CSS 中的悬停选择器中,而应该在没有任何修改状态的元素选择器中。

#navlinks a {
text-decoration:none;
color: #999;
text-transform: uppercase;
display:block;
padding-top:10px;
padding-bottom:10px;
font: bold 12px/25px Arial, Helvetica;
transition:all 1s ease;
-webkit-transition-delay:all 1s ease;
-moz-transition-delay:all 1s ease;
-ms-transition-delay:all 1s ease;
-o-transition-delay:all 1s ease;
}

JSFiddle.

JSFiddle