简体   繁体   English

css 显示图像时 hover + 动画覆盖

[英]css showing image when hover + animating the overlay

I'm trying to show the image when hover... it has a liner gradient..我试图在 hover 时显示图像......它有一个线性渐变......

I read that the gradient does not support animation yet.我读到梯度还不支持 animation。

Is there a hack to make it animate?有没有办法让它动画化? do I have to use before or after in some part?我必须在某些部分beforeafter使用吗?

This is what it looks like:这是它的样子:

Before hover hover之前

After hover hover后

 .custom-box { padding: 50px; border: 1px solid #333; cursor: pointer; transition: all 1s ease-in; }.custom-box:hover { background: linear-gradient(rgba(0,0,0,.7), rgba(0,0,0,.7)), url("https://images4.alphacoders.com/197/thumb-1920-197758.jpg") no-repeat center center/cover; }
 <div class="custom-box"> <h3 class="text-center display-3">TEST <i class="fas fa-plane"></i></h3> </div>

You can't transition directly on complex background property, but you can simulate by using other elements or pseudo-elements, and hiding/showing on hover with opacity property.复杂的背景属性不能直接转场,但是可以通过其他元素或者伪元素来模拟,通过opacity属性在hover上隐藏/显示。

Example using pseudo-elements使用伪元素的例子

 .custom-box { box-sizing: border-box; width: 100%; height: 150px; position: relative; border: 1px solid #333; cursor: pointer; z-index: 2; text-shadow: 0 0 10px white; }.custom-box::before { box-sizing: border-box; width: 100%; height: 150px; position: absolute; content: ''; transition-duration: 1s; background: linear-gradient(rgba(0,0,0,.7), rgba(0,0,0,.7)), url("https://images4.alphacoders.com/197/thumb-1920-197758.jpg") no-repeat center center/cover; opacity: 0; z-index: -1; }.custom-box:hover::before { opacity: 1; }
 <div class="custom-box"> <h3 class="text-center display-3">TEST <i class="fas fa-plane"></i></h3> </div>

 .custom-box { padding: 50px; border: 1px solid #333; cursor: pointer; transition: all 1s ease-in; }.custom-box:hover { background-image: linear-gradient( rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7) ), url("https://images4.alphacoders.com/197/thumb-1920-197758.jpg"); background-repeat: no-repeat; background-position: center; background-size: cover; }
 <div class="custom-box"> <h3 class="text-center display-3">TEST <i class="fas fa-plane"></i></h3> </div>

I'm trying to show the image when hover ... it has a liner gradient ..我试图在悬停时显示图像......它有一个线性渐变......

I read that the gradient does not support animation yet .我读到渐变还不支持动画。

Is there a hack to make it animate ?有没有办法让它动画化? do I have to use before or after in some part?我必须在某些部分beforeafter使用吗?

This is what it looks like :这是它的样子:

Before hover悬停前

After hover悬停后

 .custom-box { padding : 50px; border : 1px solid #333; cursor: pointer; transition: all 1s ease-in; } .custom-box:hover { background : linear-gradient(rgba(0,0,0,.7), rgba(0,0,0,.7)), url("https://images4.alphacoders.com/197/thumb-1920-197758.jpg") no-repeat center center/cover ; }
 <div class="custom-box"> <h3 class="text-center display-3">TEST <i class="fas fa-plane"></i></h3> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM