缩放在容器翱翔的背景图象

时间:2017-04-27 09:03:20

标签: html css css3

我有一张带有背景颜色叠加层的背景图片,当我将鼠标悬停在其容器上时,我试图使用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;
}

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

2 个答案:

答案 0 :(得分:2)

它不起作用,因为背景图片未设置为img-container,而是设置为article.item

如果您希望代码正常工作,则应将背景图像放到正确的容器中。

并从opacity:.2移除.item:hover .img-container并添加.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>

答案 1 :(得分:-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;}