背景色的过渡

jea*_*guy 272 css3

我正在尝试使用background-color悬停菜单项时的过渡效果,但它不起作用.这是我的CSS代码:

#content #nav a:hover {
    color: black;
    background-color: #AD310B;
    /* Firefox */
    -moz-transition: all 1s ease-in;
    /* WebKit */
    -webkit-transition: all 1s ease-in;
    /* Opera */
    -o-transition: all 1s ease-in;
    /* Standard */
    transition: all 1s ease-in;
}
Run Code Online (Sandbox Code Playgroud)

#nav div是一个菜单ul项目列表.

Ili*_*ium 508

据我所知,转换目前适用于Safari,Chrome,Firefox,Opera和Internet Explorer 10+.

这应该会在这些浏览器中产生淡入淡出效果:

a {
    background-color: #FF0;
}

a:hover {
    background-color: #AD310B;
    -webkit-transition: background-color 1000ms linear;
    -ms-transition: background-color 1000ms linear;
    transition: background-color 1000ms linear;
}
Run Code Online (Sandbox Code Playgroud)
<a>Navigation Link</a>
Run Code Online (Sandbox Code Playgroud)

注意:正如Gerald在评论中指出的那样,如果你将过渡放在上面a,a:hover当你的鼠标离开链接时,它将会淡化回原来的颜色.

这也可能派上用场:CSS基础:CSS 3过渡

  • 当用户将鼠标从链接移开时,您还可以将过渡转换为`content #nav a`以淡出回原始内容. (37认同)
  • 将"转换:"置于非悬停状态不是更好吗?我认为每次用户盘旋时,都会编译转换. (3认同)
  • 摆弄**悬停**和**点击**转换:[jsfiddle.net/K5Qyx](http://jsfiddle.net/K5Qyx/) (2认同)
  • CSS`transition`似乎无法处理“ linear-gradient”类型的颜色,可以通过[此处](http://codepen.io/SitePoint/pen/uIemr)进行测试。顺便说一句,@ Ilium的答案应该被标记为解决方案。 (2认同)

Rez*_*mun 81

对我来说,最好将转换代码与原始/最小选择器放在一起,而不是使用:hover或任何其他附加选择器:

#content #nav a {
    background-color: #FF0;
    
    -webkit-transition: background-color 1000ms linear;
    -moz-transition: background-color 1000ms linear;
    -o-transition: background-color 1000ms linear;
    -ms-transition: background-color 1000ms linear;
    transition: background-color 1000ms linear;
}

#content #nav a:hover {
    background-color: #AD310B;
}
Run Code Online (Sandbox Code Playgroud)
<div id="content">
    <div id="nav">
        <a href="#link1">Link 1</a>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这不是更好或更糟.只是如果你在lement本身上指定转换,它将在元素悬停时以及当它"无悬停"时被动画化.如果将它应用于:hover,则鼠标进入时会有动画,但是当它离开时则不会. (8认同)
  • 这个解决方案整体上更好.过渡效应将会并且应该预期在悬停时消失,并在模糊时淡出. (6认同)

Gab*_*son 12

实现此目的的另一种方法是使用animation提供更多控制的方法。

/* declaring the states of the animation to transition through */
/* optionally add other properties that will change here, or new states (50% etc) */
@keyframes onHoverAnimation {
    0% {
        background-color: #FF0;  
    }
    100% {
        background-color: #AD310B;
    }
}

#content #nav a {
    background-color: #FF0;
    
    /* only animation-duration here is required, rest are optional (also animation-name but it will be set on hover)*/
    animation-duration: 1s; /* same as transition duration */
    animation-timing-function: linear; /* kind of same as transition timing */
    animation-delay: 0ms; /* same as transition delay */
    animation-iteration-count: 1; /* set to 2 to make it run twice, or Infinite to run forever!*/
    animation-direction: normal; /* can be set to "alternate" to run animation, then run it backwards.*/
    animation-fill-mode: none; /* can be used to retain keyframe styling after animation, with "forwards" */
    animation-play-state: running; /* can be set dynamically to pause mid animation*/
    
    
}

#content #nav a:hover {
    /* animation wont run unless the element is given the name of the animation. This is set on hover */
    animation-name: onHoverAnimation;
}
Run Code Online (Sandbox Code Playgroud)