我有一个带有背景颜色叠加层的背景图像,当我将鼠标悬停在它的容器上时,我试图使用 transform: scale3d(1.1,1.1,1);
对其进行缩放。
即使代码出现在检查器中,它也没有生效。
这是应该在我将鼠标悬停在 item
容器上时缩放背景图像的 CSS
.item:hover .img-container {
transform: scale3d(1.1,1.1,1);
-webkit-transform: scale3d(1.1,1.1,1);
-moz-transform: scale3d(1.1,1.1,1);
opacity: .2;
}
演示 https://jsfiddle.net/xaw3vcL1/
HTML
<article class="item" style="background: url('https://images.pexels.com/photos/235470/pexels-photo-235470.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb') center center / cover;">
<div class="item-content">
<a href="#" class="img-container"></a>
</div>
<div class="content-container">
Title here
</div>
</article>
CSS
.item {
height: 450px;
position: relative;
overflow: hidden;
}
.item:before {
display: block;
content: " ";
width: 100%;
padding-top: 68.17%;
}
.item-content {
position: absolute!important;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.img-container {
webkit-background-size: cover!important;
-moz-background-size: cover!important;
-o-background-size: cover!important;
background-size: cover!important;
position: absolute;
top: -1px;
left: -2px;
right: -2px;
bottom: -1px;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-transform-origin: 0 0;
transform: translate3d(0,0,0);
}
.img-container:after {
opacity: .5;
background: #26D0CE;
background: -moz-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -webkit-gradient(left bottom,right top,color-stop(0,#1A2980),color-stop(100%,#26D0CE));
background: -webkit-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -o-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -ms-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
-webkit-transition: all .35s ease;
-moz-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.item:hover .img-container {
transform: scale3d(1.1,1.1,1);
-webkit-transform: scale3d(1.1,1.1,1);
-moz-transform: scale3d(1.1,1.1,1);
opacity: .2;
}
.content-container {
position: absolute;
bottom: 15px;
left: 20px;
right: 20px;
padding: 0;
max-height: 75%;
overflow: hidden;
pointer-events: none;
transform: translate3d(0,0,0);
-webkit-backface-visibility: hidden;
}
最佳答案
它不起作用,因为背景图像未设置为 img-container
,而是设置为 article.item
。
如果你想让你的代码工作,你应该把背景图像放到正确的容器中。
并且,从 .item:hover .img-container
中删除 opacity:.2
并添加 .item:hover .img-container:after {opacity :.2}
参见片段或 jsFiddle
.item {
height: 450px;
position: relative;
overflow: hidden;
}
.item:before {
display: block;
content: " ";
width: 100%;
padding-top: 68.17%;
}
.item-content {
position: absolute!important;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
}
.img-container {
webkit-background-size: cover!important;
-moz-background-size: cover!important;
-o-background-size: cover!important;
background-size: cover!important;
position: absolute;
top: -1px;
left: -2px;
right: -2px;
bottom: -1px;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-transform-origin: 0 0;
transform: translate3d(0,0,0);
}
.img-container:after {
opacity: .5;
background: #26D0CE;
background: -moz-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -webkit-gradient(left bottom,right top,color-stop(0,#1A2980),color-stop(100%,#26D0CE));
background: -webkit-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -o-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: -ms-linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
background: linear-gradient(45deg,#1A2980 0,#26D0CE 100%);
-webkit-transition: all .35s ease;
-moz-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.item:hover .img-container {
transform: scale3d(1.1,1.1,1);
-webkit-transform: scale3d(1.1,1.1,1);
-moz-transform: scale3d(1.1,1.1,1);
}
.item:hover .img-container:after {
opacity:.2
}
.content-container {
position: absolute;
bottom: 15px;
left: 20px;
right: 20px;
padding: 0;
max-height: 75%;
overflow: hidden;
pointer-events: none;
transform: translate3d(0,0,0);
-webkit-backface-visibility: hidden;
}
<article class="item" >
<div class="item-content">
<a href="#" class="img-container" style="background: url('https://images.pexels.com/photos/235470/pexels-photo-235470.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb') center center / cover;"></a>
</div>
<div class="content-container">
Title here
</div>
</article>
关于html - 在容器悬停时缩放背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43653277/