悬停效果两个类同时和同样的方式

时间:2014-08-27 06:40:48

标签: html css

我正在使用类来显示图像,我想以两种方式添加悬停效果。首先,如果我将鼠标悬停在图像上,则过渡效果会显示标题和叠加图像。

HTML code:

<div class="xoverlay x-simple"> <img class="x-img-main" src="img/best-img.jpg" />
      <div class="xoverlay-box">
        <div class="xoverlay-data"> <span class="x-circle x-white"> <a href="#"><i class="fa fa-facebook"></i></a> </span> <span class="x-circle x-white"> <a href="#"><i class="fa fa-twitter"></i></a> </span> <span class="x-circle x-white"> <a href="#"><i class="fa fa-user"></i></a> </span> <span>
          <h2 class="hashTag">#hashTag</h2>
          <p>It is a long established fact that a reader will be distracted by
            the readable content of a page when looking at its layout</p>
          <a class="x-more"  href="#">More</a></span> </div>
      </div>
    </div>

和我的CSS代码:

.xoverlay {
    position: relative;
    overflow: hidden;
    perspective: 300px;
    -webkit-perspective: 300px;
    -ms-perspective: 300px;
    -o-perspective: 300px;
}
.x-simple {
    width: 100%;
    height: 300px;
}

.x-img-main {
    width: 100%;
    -webkit-transition: all .45s ease-in-out;
    -moz-transition: all .45s ease-in-out;
    -o-transition: all .45s ease-in-out;
    -ms-transition: all .45s ease-in-out;
    transition: all .45s ease-in-out;
}

.x-img-main:hover{
    width:110%;
    height:auto;
}

.x-simple:hover .xoverlay-box {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    opacity: 1;
    background: rgba(27,27,27,.7);
}

此代码为JSFiddle

1 个答案:

答案 0 :(得分:0)

当前代码中存在的问题是,当您将鼠标悬停在x-simple元素上时,xoverlay-box会出现在img前面,并且悬停在img元素上时不会触发。

您不必在两个元素上应用悬停以实现对两个元素的效果。您可以在父元素上应用悬停,并将其用于子元素。在CSS中只需替换

.x-img-main:hover{

.x-simple:hover .x-img-main{

修改后的JSFiddle