CSS同时转换边框和背景



我想在悬停时同时更改背景颜色和边框颜色。我使用:

transition: all 0.5s ease;    
-vendors-transition: all 0.5s ease;

但是背景颜色立即改变。

这是一个工作代码,

#yourdiv {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: yellow;
  border: 5px solid black;    
  transition: border 500ms ease-out; 
  -webkit-transition: border 500ms ease-out; 
  -moz-transition: border 500ms ease-out;
  -o-transition: border 500ms ease-out;
}
#yourdiv:hover{
  border: 10px solid red;
  background-color: red;
}

我已经通过单独指定属性来尝试过了,它有效,也许all关键字失败了?

div:hover {
    background-color: blue;
    border-color: black;
    -webkit-transition: background-color 0.5s ease, border-color 0.5s ease;
    -moz-transition: background-color 0.5s ease, border-color 0.5s ease;
    /* please note that transitions are not supported in IE 9 and there is no -ms prefix */
    transition: background-color 0.5s ease, border-color 0.5s ease;
}

jsFiddle

更新:我想我理解你的问题。您希望在解除缠绕时反转过渡。在这种情况下,您还需要非悬停元素(没有:hover的元素)上的转换属性:

div {
    width: 10rem;
    height: 10rem;
    background-color: purple;
    border: thick solid orange;
    -webkit-transition: background-color 0.5s ease, border-color 0.5s ease;
    -moz-transition: background-color 0.5s ease, border-color 0.5s ease;
    /* please note that transitions are not supported in IE 9 and there is no -ms prefix */
    transition: background-color 0.5s ease, border-color 0.5s ease;
}

jsFiddle

相关内容

  • 没有找到相关文章

最新更新