简体   繁体   English

在CSS中将鼠标悬停在div上时显示内容

[英]Show content when hovering over div in CSS

I am using Freewall JS plugin to generate a mosaic image grid. 我正在使用Freewall JS插件生成镶嵌图像网格。 I am also applying a background overlay color with opacity: 0.7 and when you hover over it, the background overlay color becomes clear with opacity: 0.2 我还应用了opacity: 0.7的背景叠加颜色,当您将鼠标悬停在上面时,背景opacity: 0.2变得清晰

I also have content inside each block, I've set it to display none by default but I can't get it to appear on hover. 在每个块中也都有内容,我将其设置为默认情况下不显示任何内容,但无法使其悬停显示。

The grid items are being generated from the javascript so the HTML is an empty div with an id 网格项目是通过JavaScript生成的,因此HTML是带有ID的空div

jsFiddle https://jsfiddle.net/3o63eor8/3/ jsFiddle https://jsfiddle.net/3o63eor8/3/

<div id="freewall" class="free-wall"></div>

The CSS CSS

.overlay-background {
    position: relative;
}
.overlay-background:before{
    position: absolute;
    content:" ";
    top:0;
    left:0;
    width:100%;
    height:100%;
    display: none;
    z-index:0;
}
.overlay-background:before{
    display: block;
}
.dark:before {
    background-color: rgba(0,0,0,0.7);
}
.free-wall {
    width: 100%;
    height: 100%;
}
#freewall .dark:hover::before {
    background-color: rgba(0,0,0,0.2);
}
#freewall .freewall-content span {
    display: none
}
#freewall .freewall-content:hover span {
    display: block
}

The JS that generates the image blocks automatically, I've added my css classes to the temp string 自动生成图像块的JS,我已将CSS类添加到temp字符串中

var temp = "<div class='brick overlay-background dark' style='width:{width}px; " +
    "height: {height}px; " +
    "background-image: url(assets/images/posts/post-{index}.jpg); " +
    "background-repeat: no-repeat; " +
    "background-size: cover; " +
    "background-position: center center;" +
    "background-color: {color}'>" +
    "<div class='v-center text-center freewall-content'><span><i class='fa fa-search'></i></span></div>" +
    "</div>";

var colour = [
    "rgb(142, 68, 173)",
    "rgb(243, 156, 18)",
    "rgb(211, 84, 0)",
    "rgb(0, 106, 63)",
    "rgb(41, 128, 185)",
    "rgb(192, 57, 43)",
    "rgb(135, 0, 0)",
    "rgb(39, 174, 96)"
];

var w = 1, h = 1, html = '', color = '', limitItem = 17;
for (var i = 0; i < limitItem; ++i) {
    h = 1 + 3 * Math.random() << 0;
    w = 1 + 3 * Math.random() << 0;
    color = colour[colour.length * Math.random() << 0];
    html += temp.replace(/\{height\}/g, h*150).replace(/\{width\}/g, w*150).replace("{color}", color).replace("{index}", i + 1);
}
$("#freewall").html(html);

The issue is how your HTML is structured. 问题是HTML的结构。 Technically, the div where class="brick overlay-background dark" is on top of the freewall-content so you never actually hover over it. 从技术上讲, div其中class="brick overlay-background dark"位于freewall-content顶部,因此您实际上从未将其悬停在上面。 You can see this through the developer tools (f12) and looking at the live DOM tree 您可以通过开发人员工具(f12)并查看实时DOM树来查看

Change the css selector to look like - 将css选择器更改为-

.brick.overlay-background.dark:hover .freewall-content .span {
    display: block
}

https://jsfiddle.net/3o63eor8/8/ https://jsfiddle.net/3o63eor8/8/

Don't use display: none; 不要使用display:无;

Make the opacity of the parent element 1 and then lower the opacity to so the content can be visible. 设置父元素1的不透明度,然后将其降低到,以使内容可见。

I think you're overcomplicating something that has a simple solution. 我认为您过于简单了一些简单的解决方案。

https://jsfiddle.net/erwdepqp/ https://jsfiddle.net/erwdepqp/

.brick {
      width: 300px;
      height: 300px
    }
    .overlay-background {
        position: relative;
    }
    .overlay-background:before{
        position: absolute;
        content:" ";
        top:0;
        left:0;
        width:100%;
        height:100%;
        display: none;
        z-index:0;
    }
    .overlay-background:before{
        display: block;
    }
    .dark:before {
        background-color: rgba(0,0,0, 1);
    }
    .dark:hover::before {
        background-color: rgba(0,0,0,0.2);
    }
    .freewall-content:hover span {
        display: block;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM