jquery - css背景缩放效果淡入淡出

标签 jquery html css image css-transitions

从这里使用以下放大效果https://codepen.io/dragoeco/pen/ibrzK

需要一些帮助来使背景图像在悬停时淡入并具有缩放效果,就像这里的一样 http://fantasy.co/work保持前景 Logo 相同。

.image-box{
  width:300px;
  overflow:hidden;
}
.image {
	width:300px;
  height:200px;
	background: url("http://lorempixel.com/300/200");
	background-position:center;
	transition: all 1s ease;
  -moz-transition: all 1s ease;
  -ms-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  -o-transition: all 1s ease;
} 
.image:hover {
  transform: scale(1.5);
  -moz-transform: scale(1.5);
  -webkit-transform: scale(1.5);
  -o-transform: scale(1.5);
  -ms-transform: scale(1.5); /* IE 9 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')"; /* IE8 */
   filter: progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand'); /* IE6 and 7 */ 
} 
<div class="image-box">
  <div class="image">
  </div>
</div>  

最佳答案

将悬停从 image 更改为 image-box,在 image-box 内放置一个标签并添加以下 css:

.image-box, .image {
  position: relative;
}
.image:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  background-color: red;
  transition: all 1s ease;
  -moz-transition: all 1s ease;
  -ms-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  -o-transition: all 1s ease;
}
.label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 25px;
  font-weight: bolder;
}
.image-box:hover .image:after {
  opacity: 0.5;
}

检查一下,让我知道您的反馈。谢谢!

编辑:

也增加了 image 的过渡时间。 :)

.image-box {
  width: 300px;
  overflow: hidden;
}
.image {
  width: 300px;
  height: 200px;
  background: url("http://lorempixel.com/300/200");
  background-position: center;
  transition: all 10s ease;
  -moz-transition: all 10s ease;
  -ms-transition: all 10s ease;
  -webkit-transition: all 10s ease;
  -o-transition: all 10s ease;
}
.image-box:hover .image {
  transform: scale(1.5);
  -moz-transform: scale(1.5);
  -webkit-transform: scale(1.5);
  -o-transform: scale(1.5);
  -ms-transform: scale(1.5);
  /* IE 9 */
  -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand')";
  /* IE8 */
  filter: progid: DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand');
  /* IE6 and 7 */
}

.image-box, .image {
  position: relative;
}
.image:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  background-color: red;
  transition: all 1s ease;
  -moz-transition: all 1s ease;
  -ms-transition: all 1s ease;
  -webkit-transition: all 1s ease;
  -o-transition: all 1s ease;
}
.label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 25px;
  font-weight: bolder;
}
.image-box:hover .image:after {
  opacity: 0.5;
}
<div class="image-box">
  <div class="image"></div>
  <div class="label">LABEL</div>
</div>

关于jquery - css背景缩放效果淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39056295/

相关文章:

javascript - 从另一个表单中包含的文本字段传递参数

html - CSS Floated Div 高度问题,没有简单的解决办法?

javascript - 测量同级元素的总高度

html - 将 X 放在 div 的右上角而不使顶部可点击

jquery - 悬停没有出现在准确的位置

jquery - 如何加快jquery mobile中的js加载速度?

javascript - Jquery 必填字段 - 无插件

php - 类型错误 : 'checkValidity' called on an object that does not implement interface HTMLTextAreaElement

jQuery 查找没有指定祖先的元素

javascript - 如何使用 JS 从 CSS 属性获取默认值?