【问题标题】:Div turns black on hoverdiv在悬停时变黑
【发布时间】:2015-06-13 16:51:22
【问题描述】:

所以我希望这个 div 淡化另一个具有 0.5 不透明度的黑色背景的 div。但是当我悬停时,它会忽略 div 中已经存在的图像并填充黑色然后淡化另一个 div。所以它会出现图像->纯黑色div->然后淡化我的第二个div。 https://jsfiddle.net/70e890e6/

HTML:

<div class="box1">
    <div class="img_hover_effect"></div>
    <img src="images/portfolio/charity.png" class="box1_img">
    <img src="images/portfolio/charity/2.png" class="box1_img2">
</div>

CSS:

.img.img_hover_effect {

display: none;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);

}

jQuery:

$(document).ready(function(){

   $('.box1').on('mouseenter', function() {

$('.img_hover_effect').fadeIn(1000);

});

});

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

你可以像这样使用 jQuery hover

$('.box1').hover(function() {
    $('.hover').fadeIn(1000);
}, function(){
    $('.hover').fadeOut(1000);
});

还有一些 CSS 更改:

.box1 {
    position:relative;
    height: 400px;
    width: 350px;
    margin: 0 auto;
    margin-top: 100px;
    float: left;
    overflow: hidden;
}

.tree {
    height: 100%;
    width: auto;
    display: block;
    position: relative;
}

.hover {
    position:absolute;
    background-color: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    display: none;
    z-index: 10;
}

JSFIDDLE:https://jsfiddle.net/70e890e6/4/

【讨论】:

    【解决方案2】:

    您可以使用 CSS 伪类来实现所需的悬停效果。我建议将该类直接应用于您希望具有悬停效果的图像,而不是与另一个 div 叠加。

    .hover-effect:hover{
        background-color: #000000; //or whatever colour fade you are looking for
        opacity: 0.5;
        -webkit-transition: all 0.2s ease-in-out;
        -moz-transition: all 0.2s ease-in-out;
        -o-transition: all 0.2s ease-in-out;
        transition: all 0.2s ease-in-out;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多