2017-04-27 58 views
0

我有一个背景图像,背景颜色重叠,我试图在悬停在其容器上时使用transform: scale3d(1.1,1.1,1);进行缩放。在容器悬停上缩放背景图像

即使代码出现在检查器中,它也不会生效。

这是一个应该每当我将鼠标悬停在item容器

.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; 
} 

DEMOhttps://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

缩放背景图片的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; 
} 

回答

2

它不起作用,因为背景图像未设置为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 { 
 
\t 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>

+0

这太好了,谢谢。但是,如果你不介意我问,为什么现在我将鼠标悬停在白色背景上而不选择我选择的颜色? – Halnex

+0

白色背景是因为不透明度:HOVER上的0.2和“item”下面的背景是灰色/白色(默认的背景背景)。并且因为':after'被设置为相同的物品,您赋予不透明度:0.2,它也会得到该样式。因此将':after'设置为'item'而不是'img-container'参见https://jsfiddle.net/xaw3vcL1/3/ –

+0

这增加了另一个问题,我想让背景图像正确显示,而不是白色背景更加模糊了它。像我原来的代码一样,当你将鼠标悬停在上面时,图像变得更清晰。 – Halnex

-1

这个类添加到您的CSS

article:hover{transform: scale3d(1.1,1.1,1); 
-webkit-transform: scale3d(1.1,1.1,1); 
-moz-transform: scale3d(1.1,1.1,1); 
opacity:0.2;}