CSS 通过上滑动画过渡背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20751937/
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
Transition background-color via slide up animation
提问by David Jones
I am trying to change the background-color of an element on hover by using CSS transitions. I want to do this by having it scroll up from the bottom. I can fade the background in using this, but I would like it to slide up:
我试图通过使用 CSS 转换来更改悬停时元素的背景颜色。我想通过让它从底部向上滚动来做到这一点。我可以使用它淡化背景,但我希望它向上滑动:
-webkit-transition: background-color 0.5s linear;
-moz-transition: background-color 0.5s linear;
-o-transition: background-color 0.5s linear;
transition: background-color 0.5s linear;
One other thought, would it be better to scroll up a separate element with the background applied to it?
另一个想法是,向上滚动一个单独的元素并应用背景会更好吗?
回答by Sampson
In order to slide the background color upyou would need to use a background image, or a gradient of some sort, while gradually adjusting the background-position
:
为了向上滑动背景颜色,您需要使用背景图像或某种渐变,同时逐渐调整background-position
:
.box {
width: 200px; height: 100px;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
transition: background-position 1s;
}
.box:hover {
background-position: 0 -100%;
}
You can see this demo here: http://jsfiddle.net/as5ZU/
你可以在这里看到这个演示:http: //jsfiddle.net/as5ZU/
回答by Temani Afif
An extension to @Sampson answer where I will consider more friendlyvalues easier to understand:
@Sampson 答案的扩展,我将考虑更容易理解的更友好的值:
.box {
width: 100px;
height: 100px;
display:inline-block;
background-size: 200% 200%;
transition: background-position 1s;
}
.to-top{
background-image: linear-gradient(to top, red 50%, black 0);
background-position: top;
}
.to-top:hover {
background-position: bottom;
}
.to-bottom{
background-image: linear-gradient(to bottom, red 50%, black 0);
background-position: bottom;
}
.to-bottom:hover {
background-position: top;
}
.to-left{
background-image: linear-gradient(to left, red 50%, black 0);
background-position: left;
}
.to-left:hover {
background-position: right;
}
.to-right{
background-image: linear-gradient(to right, red 50%, black 0);
background-position: right;
}
.to-right:hover {
background-position: left;
}
<div class="box to-top"></div>
<div class="box to-bottom"></div>
<div class="box to-left"></div>
<div class="box to-right"></div>
Here is more fancy transitions:
这里有更多花哨的过渡:
.box {
width: 100px;
height: 100px;
display:inline-block;
transition: 1s;
}
.to-center{
background:
linear-gradient(black,black) no-repeat,
red;
background-size: 100% 100%;
background-position:center;
}
.to-center:hover {
background-size: 0% 100%; /* Or 100% 0% */
}
.from-center{
background:
linear-gradient(red,red) no-repeat,
black;
background-size: 0% 100%; /* Or 100% 0% */
background-position:center;
}
.from-center:hover {
background-size: 100% 100%;
}
.diagonal-right{
background-image:linear-gradient(to bottom right,red 49.5%,black 50%);
background-size: 200% 200%;
background-position:bottom right;
}
.diagonal-right:hover {
background-position:top left;
}
.diagonal-left{
background-image:linear-gradient(to bottom left,red 49.5%,black 50%);
background-size: 200% 200%;
background-position:bottom left;
}
.diagonal-left:hover {
background-position:top right;
}
<div class="box to-center"></div>
<div class="box from-center"></div>
<div class="box diagonal-right"></div>
<div class="box diagonal-left"></div>
Related question to get more details about how background-position
works combined with background-size
: Using percentage values with background-position on a linear gradient
相关问题以获取有关如何background-position
结合使用的更多详细信息background-size
:在线性渐变上使用具有背景位置的百分比值