2017-04-27 72 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;}