简体   繁体   English

CSS使用z-index将div定位在顶部

[英]CSS Positioning div on top with z-index

I have a container which has three divs. 我有一个有三个div的容器。 Each div contains a hidden div which has a 'hide' class ( display:none; ) inside which is supposed to show when hovering on its parent div. 每个div都包含一个隐藏的div,它有一个'hide'类( display:none; ),当它悬停在它的父div上时应该显示。

I use toggleClass('show') to which makes the secretDiv display has a block. 我使用toggleClass('show')使secretDiv显示有一个块。 I need the secretDiv to be shown when hovering on the parent div. 我需要在悬停在父div上时显示secretDiv。

The parent div should show on top of the div below, and not push the other divs 父div应该显示在下面的div之上,而不是推动其他div

http://jsfiddle.net/2xLMQ/4/ http://jsfiddle.net/2xLMQ/4/

--- HTML --- --- HTML ---

<div class="row">
    <div class="element">
        <img src="http://placehold.it/200x100" alt="" title="" />
        <div class="secretDiv hide">
            <p>Some secret text and stuff</p>
        </div>
    </div>
    <div class="element">
        <img src="http://placehold.it/200x100" alt="my image" title="image title" />
        <div class="secretDiv hide">
            <p>Some secret text and stuff</p>
        </div>
    </div>
</div>

<div class="row">
    <div class="element">
        <img src="http://placehold.it/200x100" alt="" title="" />
        <div class="secretDiv hide">
            <p>Some secret text and stuff</p>
        </div>
    </div>
    <div class="element">
        <img src="http://placehold.it/200x100" alt="my image" title="image title" />
        <div class="secretDiv hide">
            <p>Some secret text and stuff</p>
        </div>
    </div>
</div>

--- CSS --- --- CSS ---

.hide {display:none;}
.show {display:block;}
.row {height:160px;background:#dedede;float:left;width:480px;position:relative:z-index:1;}
.row .element {position:relative;z-index:9;text-align:center; float:left; background:#666;width:200px;padding:12px;margin:6px;}
.row .image {}
.row .secretDiv {background:#ff0000;padding:8px;}

--- JS --- --- JS ---

$('.element').hover(function(){
    $('.element .secretDiv').toggleClass('show');
});

First at all change your selector to only match the respective hidden div: 首先,将您的选择器更改为仅匹配相应的隐藏div:

 $('.secretDiv',this).toggleClass('show');

Then add another class on that item to display ontop of the others : 然后在该项目上添加另一个类以显示其他项目的ontop:

$(this).toggleClass('ontop');

And the class: 和班级:

.row .ontop {z-index:10;background:orange;}

Check this Demo 检查这个演示

Simply add absolute positioning to your 'secret' div: 只需为您的'秘密'div添加绝对定位:

.row .secretDiv {background:#ff0000;padding:8px; position: absolute; top: 5px; left: 5px;}

Fiddle here: http://jsfiddle.net/moonspace/2xLMQ/12/ 在这里小提琴: http//jsfiddle.net/moonspace/2xLMQ/12/

As a bonus, I've edited your jQuery to show only the 'secret' div associated with each element: 作为奖励,我编辑了你的jQuery,只显示与每个元素相关的'secret'div:

$('.secretDiv', this).toggleClass('show');

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

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