简体   繁体   English

如何在悬停时使用网格设计将叠加div放在另一个div上?

[英]How to place a overlay div on another div on hover with grid like design?

I tried to make a grid like design, I want to show a overlay on hover to each div. 我尝试制作一个像设计的网格,我想在悬停到每个div时显示叠加。

The overlay div has two button which should be placed in center. overlay div有两个按钮,应放在中间。

i tried the below code i believe the logic is correct but the position of the button and overlay are not placing at the center of the image div. 我尝试下面的代码我相信逻辑是正确的但按钮和叠加的位置不是放在图像div的中心。

Image Without overlay 图像没有叠加

图像没有叠加

On hover with overlay 悬停时使用叠加层

悬停时使用叠加层

Html HTML

<ul>
    <li>
        <div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
        <div class="overlay"><div class="bt1"></div><div class="bt2"></div></div>
    </li>
    <li>
<div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
         <div class="overlay"><div class="bt1"></div><div class="bt2"></div></div>
    </li>
    <li>
<div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
         <div class="overlay"><div class="bt1"></div><div class="bt2"></div></div>
    </li>
    <li>
<div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
        <div class="overlay"><div class="bt1"></div><div class="bt2"></div></div>
    </li>

</ul>

CSS CSS

.image{
 width:100px;
 height:100px;

}
.overlay{
 width:100px;
 height:100px;
 display:none; 
}

ul { list-style: none; width: 100%; }
ul li { display:inline-block; width:150px;}
.image:hover +.overlay {
    display:block;
    background-color:black;
    top:0;
    opacity:0.75;

}
.bt1 {
    background-color:orange;
    position:absolute;
    width:50px;
    height:50px;
    margin:0 0 0 5%;
}
.bt2 {
    background-color:green;
    position:absolute;
    width:50px;
    height:50px;
    margin:0 5% 0 0;
}

JSfiddle . JSfiddle Image size are changeable so fixed padding to center button may won't help here i think. 图像大小是可以改变的,所以我认为固定填充到中心按钮可能无济于事。 Can anyone help me on positioning the overlay? 任何人都可以帮我定位叠加层吗?

Changed a few things with the positions and also gave a height to the li element cause the img was bigger.. 用位置改变了一些东西,并且给了li元素一个高度因为img更大..

http://jsfiddle.net/4CqNK/8/ http://jsfiddle.net/4CqNK/8/

.image{
 width:100px;
 height:100px;

}
.overlay{
 width:100%;
 height:100%;
display:none;
    position:absolute;
    top:0px;
    left:0px;
}
.overlay div {
    position:relative;
    display:inline-block;
    top:50%;
    margin:-50% 5px 0 0;
}
ul { list-style: none; width: 100%; }
ul li { position:relative;display:inline-block; width:150px;height:150px;}
li:hover .overlay {
    display:block;
    background-color:black;
    opacity:0.75;
}
.bt1 {
    background-color:orange;
    width:50px;
    height:50px;
}
.bt2 {
    background-color:green;
    width:50px;
    height:50px;
}

Here's another solution where i've used only 1 div but you can use several other div's with the same class. 这是另一个解决方案,我只使用了1个div但你可以使用同一个类的其他几个div's FIDDLE 小提琴

You first have to be sure that your .image and .overlay classes are displayed at the same place. 首先必须确保.image.overlay类显示在同一个地方。 So here you should add a position:relative in ul li and add an absolute:position to both classes. 所以在这里你应该添加一个position:relative ul li position:relative ,并为这两个类添加一个absolute:position

Once you have that, it is easier to place your elements: ( jsfiddle ) 一旦你拥有了它,你就可以更容易地放置元素:( jsfiddle

.image{
    position:absolute;
    left:0;
    top:0;
 width:150px;
 height:150px;

}
.overlay{
    position:absolute;
    left:0;
    top:0;
 width:150px;
 height:150px;
 display:none; 
}

ul { list-style: none; width: 100%; }
ul li { 
    display:inline-block; 
    width:160px; 
    position:relative;
}
li:hover .overlay {
    display:block;
    background-color:black;
    opacity:0.75;

}
.bt1 {
    background-color:orange;
    position:absolute;
    width:50px;
    height:50px;
    left:5%; <-- you may have to play with those values if you want to place them somewhere else
    top:30%; <--  same as above
}
.bt2 {
    background-color:green;
    position:absolute;
    width:50px;
    height:50px;
    right:5%;
    top:30%;
}

You can use CSS code below for image: 您可以使用下面的CSS代码来显示图像:

position:absolute;
z-index:-1;

The z-index property specifies the stack order of an element. z-index属性指定元素的堆栈顺序。

An element with greater stack order is always in front of an element with a lower stack order. 堆栈顺序较大的元素始终位于堆栈顺序较低的元素前面。

Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed). 注意:z-index仅适用于定位元素(位置:绝对,位置:相对或位置:固定)。

My solution: http://jsfiddle.net/xAkJG/1/ 我的解决方案: http//jsfiddle.net/xAkJG/1/

Came across this Question and created my own, flexbox based , solution. 遇到了这个问题并创建了我自己的基于flexbox的解决方案。 It centers the buttons more nicely! 它使按钮更加美观!

http://cssdeck.com/labs/83bhrs3v http://cssdeck.com/labs/83bhrs3v

<ul>
    <li>
        <div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
        <div class="overlay"><div><div class="bt1"></div><div class="bt2"></div></div></div>
    </li>
    <li>
<div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
                <div class="overlay"><div><div class="bt1"></div><div class="bt2"></div></div></div>

    </li>
     <li>
<div class="image"><img src="http://i.stack.imgur.com/rOVDt.jpg"/></div>
                <div class="overlay"><div><div class="bt1"></div></div></div>

    </li>       
</ul>
.image{
    width:100px;
    height:100px;
}

.overlay{
    width:100%;
    height:100%;
    display:none;
    position:absolute;
    top:0px;
    left:0px;

    background-color:black;
    opacity:0.75;    
}

.overlay > div {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width:100%;
    height:100%;
}

ul { list-style: none; width: 100%; }
ul li { position:relative;display:inline-block; width:150px;height:150px;}

li:hover .overlay {
    display:block;
}

.bt1 {
    background-color:orange;
    width:80px;
    height:80px;
}
.bt2 {
    background-color:green;
    width:50px;
    height:50px;
}

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

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