2016-01-21 71 views
0

我搜索了全部内容,发现了一些解决方案,但是我无法在我的应用程序中使用它。我试图做的是在图像悬停时显示黑色图层,然后显示文本。理想情况下,我希望文本具有看起来像按钮的边框。当鼠标悬停在图像上时,用文本获得黑色覆盖图

我希望这与我的悬停秤一起工作。出于某种原因,在我的实际页面上,当将鼠标悬停在图像上时,它除了缩放之外什么都没做。它不会将父div变成灰色。

我在做什么错?

$('.home-img-block').find('img').each(function() { 
 
    var imgClass = (this.width/this.height > 1) ? 'wide' : 'tall'; 
 
    console.log(imgClass); 
 
    $(this).addClass(imgClass); 
 
});
#home-img-blocks { 
 
\t width: 100%; 
 
\t height: 600px; 
 
} 
 

 
.home-img-block { 
 
\t width: 33.33%; 
 
\t /*height: 100%;*/ 
 
\t display: inline-block; 
 
\t overflow: hidden; 
 
\t cursor: pointer; 
 
} 
 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color:#fff; 
 
    position:absolute; 
 
    width:100%; height:100%; 
 
    top:0; left:0; 
 
    background:rgba(0,0,0,0.6); 
 
    opacity:0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
} 
 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 
.home-img-block:hover img{ 
 
    -webkit-transform:scale(1.25); /* Safari and Chrome */ 
 
    -moz-transform:scale(1.25); /* Firefox */ 
 
    -ms-transform:scale(1.25); /* IE 9 */ 
 
    -o-transform:scale(1.25); /* Opera */ 
 
    transform:scale(1.25); 
 
\t background: rgba(0,0,0,0.3); 
 
\t width: 33.33%; 
 
\t max-height: 100%; 
 
} 
 
.home-img-block img.wide { 
 
\t max-width: 100%; 
 
    max-height: 100%; 
 
\t height: auto; 
 
} 
 
.home-img-block img.tall { 
 
\t max-width: 100%; 
 
    max-height: 100%; 
 
\t width: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="home-img-blocks"> 
 
\t <div data-content="FIND OUT MORE" class="home-img-block"><img src="http://optimumwebdesigns.com/images/test1.jpg"></div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test2.jpg"> 
 
    </div><div class="home-img-block"><img src="http://optimumwebdesigns.com/images/test3.jpg"></div> 
 
</div>

回答

1

希望这是你想要的。看一下这个。为黑色叠加添加了叠加类。

$('.home-img-block').find('img').each(function() { 
 
    var imgClass = (this.width/this.height > 1) ? 'wide' : 'tall'; 
 
    console.log(imgClass); 
 
    $(this).addClass(imgClass); 
 
});
* { 
 
    box-sizing: border-box; 
 
} 
 

 
#home-img-blocks { 
 
    width: 100%; 
 
    height: 600px; 
 
} 
 

 
.home-img-block { 
 
    width: 33.33%; 
 
    float: left; 
 
    /*height: 100%;*/ 
 
    display: inline-block; 
 
    overflow: hidden; 
 
    cursor: pointer; 
 
    position: relative; 
 
} 
 

 
.home-img-block:hover .overlay { 
 
    background: rgba(0, 0, 0, 0.6); 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 

 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color: #fff; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    opacity: 0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
    border: 1px solid #fff; 
 
    padding: 5px; 
 
    text-align: center; 
 
} 
 

 
.home-img-block:hover:after { 
 
    opacity: 1; 
 
} 
 

 
.home-img-block img { 
 
    -webkit-transition: all 1s ease; 
 
    /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; 
 
    /* Firefox */ 
 
    -ms-transition: all 1s ease; 
 
    /* IE 9 */ 
 
    -o-transition: all 1s ease; 
 
    /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.home-img-block:hover img { 
 
    -webkit-transform: scale(1.25); 
 
    /* Safari and Chrome */ 
 
    -moz-transform: scale(1.25); 
 
    /* Firefox */ 
 
    -ms-transform: scale(1.25); 
 
    /* IE 9 */ 
 
    -o-transform: scale(1.25); 
 
    /* Opera */ 
 
    transform: scale(1.25); 
 
    background: rgba(0, 0, 0, 0.3); 
 
    width: 33.33%; 
 
    max-height: 100%; 
 
} 
 

 
.home-img-block img.wide { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    height: auto; 
 
    width: 100%; 
 
} 
 

 
.home-img-block img.tall { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    width: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="home-img-blocks"> 
 
    <div data-content="FIND OUT MORE" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test1.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
    <div data-content="FIND OUT" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test2.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
    <div data-content="FIND" class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test3.jpg"> 
 
    <div class="overlay"></div> 
 
    </div> 
 
</div>

+0

这是完美的,除了它似乎黑色叠加放大图像一点点。我将如何防止? – Becky

+0

@Becky你不想在悬停时缩放图像?或缩小一点点? –

+0

选中此https://jsfiddle.net/Jinukurian7/rb7vdacu/2/ –

0

你缺少你的CSS的几件事情,这就是为什么你没有得到的期望的影响,你正在寻找。您需要添加相对于.home-img-block类的位置,然后将一些东西放在错误的位置,以便在悬停时发生奇怪的晃动。

下面是与它周围的jsfiddle这样你就可以搞一个链接:

https://jsfiddle.net/kriscoulson/y32ekgdm/

#home-img-blocks { 
 
    width: 100%; 
 
    height: 600px; 
 
} 
 

 
.home-img-block { 
 
    width: 33.33%; 
 
    /*height: 100%;*/ 
 
    display: inline-block; 
 
    overflow: hidden; 
 
    cursor: pointer; 
 
    position: relative; 
 
} 
 
.home-img-block:after { 
 
    content: attr(data-content); 
 
    color:#fff; 
 
    position:absolute; 
 
    width:100%; height:100%; 
 
    top:0; left:0; 
 
    background:rgba(0,0,0,0.6); 
 
    opacity:0; 
 
    transition: all 0.5s; 
 
    -webkit-transition: all 0.5s; 
 
} 
 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; Safari and Chrome 
 
    -moz-transition: all 1s ease; Firefox 
 
    -ms-transition: all 1s ease; IE 9 
 
    -o-transition: all 1s ease; Opera 
 
    transition: all 1s ease; 
 
} 
 
.home-img-block:hover img{ 
 
    transform:scale(1.25); 
 
    background: rgba(0,0,0,0.3); 
 
} 
 
.home-img-block img.wide { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    height: auto; 
 
} 
 
.home-img-block img.tall { 
 
    max-width: 100%; 
 
    max-height: 100%; 
 
    width: auto; 
 
}

0

看起来与CSS的问题。主要是以下几点:

home-img-block:hover img 

.home-img-block img 

因为,.home-img-block被包裹的HTML。你不必具体。并加上第一个CSS行hover集似乎是错误的。我已将代码分成小提琴。

https://jsfiddle.net/kuape5np/

你能否确认,是你想要的?

0

检查了这一点,我希望你正在寻找相同

.home-img-block { 
 
\t width: 33.33%; 
 
\t display: inline-block; 
 
\t overflow: hidden; 
 
\t cursor: pointer; 
 
\t  position: relative; 
 
} 
 

 
.home-img-block:hover:after { 
 
    opacity:1; 
 
} 
 
.home-img-block img{ 
 
    -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
\t width: 100%; 
 
} 
 
.home-img-block:hover img{ 
 
    -webkit-transform:scale(1.25); /* Safari and Chrome */ 
 
    -moz-transform:scale(1.25); /* Firefox */ 
 
    -ms-transform:scale(1.25); /* IE 9 */ 
 
    -o-transform:scale(1.25); /* Opera */ 
 
    transform:scale(1.25); 
 
\t background: rgba(0,0,0,0.3); 
 
\t max-height: 100%; 
 
} 
 
.home-img-block:after { 
 
    background: rgba(0,0,0,.5); 
 
    content: ""; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.hover { 
 
    position: absolute; 
 
    z-index: 99; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50% , -50%); 
 
    opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 
.hover h2 { 
 
\t color:#fff; 
 
\t opacity: 0; 
 
\t -webkit-transition: all 1s ease; /* Safari and Chrome */ 
 
    -moz-transition: all 1s ease; /* Firefox */ 
 
    -ms-transition: all 1s ease; /* IE 9 */ 
 
    -o-transition: all 1s ease; /* Opera */ 
 
    transition: all 1s ease; 
 
} 
 

 
.home-img-block:hover:after,.home-img-block:hover .hover, .home-img-block:hover .hover h2 { 
 
    opacity: 1; 
 
}
<div id="home-img-blocks"> 
 
\t <div class="home-img-block"> 
 
    <img src="http://optimumwebdesigns.com/images/test1.jpg"> 
 
\t <div class="hover"> 
 
    <h2>FIND OUT MORE</h2> 
 
    </div> 
 
    </div> 
 
</div>

相关问题