绝对位置和相对位置都不好用

Neither position absolute neither position relative is working well

本文关键字:位置 相对      更新时间:2023-09-26

当我点击一个名为"test"的div时,一个名为"outside"的div出现了,也出现了一个名为"inside"的div,具有更高的z-index。

我的问题是,当我将位置设置为绝对到"内部"时,我不能归因于margin-bottom。当我将位置设置为相对位置时,它会将div "test"放在它的下面。

这可能有点难以理解,但问题真的很简单。这里有一个小提琴:http://jsfiddle.net/malamine_kebe/QRpqs/

我的CSS是:

#insideAbsolute{
    background-color:#f8f8f8;
    position: absolute;
    top:0;
    left:20%;
    width:60%;
    margin-top:35px;
    margin-bottom:35px;
    z-index:3;
    border-radius: 7px;
    box-shadow: 6px 6px 20px black; 
}
#insideRelative{
    background-color:#f8f8f8;
    position: relative;
    top:0;
    left:20%;
    width:60%;
    margin-top:35px;
    margin-bottom:35px;
    z-index:3;
    border-radius: 7px;
    box-shadow: 6px 6px 20px black; 
}

#outside{
    position: fixed;
    left:0;
    top:0;
    height: 100%;
    width: 100%;
    background-color: black;
    opacity:0.7;
    z-index:2;
    background-attachment:fixed;
}
.test{
    z-index:1;
}

我的HTML:

<div id="outside"></div>
<div id="insideAbsolute"></div>
<div id="insideRelative"></div>
<div class="testAbsolute">test position absolute</div>
<div class="testRelative">test position relative</div>

和我的jQuery

$('#outside').hide();
    $('#insideAbsolute').hide();
    $('#insideRelative').hide();
    $(document).on('click', '.testAbsolute', function () {
        $('#outside').show(0, function() { 
            $('#insideAbsolute').show(0, function() {
                $(this).html('<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>');
                $(document).on('click','#outside',function(){
                    $('#insideAbsolute').html('');
                    $('#outside').hide();   
                });     
            }); 
        });
    });  
    $(document).on('click', '.testRelative', function () {
        $('#outside').show(0, function() { 
            $('#insideRelative').show(0, function() {
                $(this).html('<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>');
                $(document).on('click','#outside',function(){
                    $('#insideRelative').html('');
                    $('#outside').hide();   
                });     
            }); 
        });
    });  

给你一个小技巧。在你的CSS中添加:

#insideAbsolute:after{
    content:'';
    height : 35px;
    width : 100%;
    position : absolute;
    bottom : -35px;
}

创建"假"页边距!

小提琴:http://jsfiddle.net/QRpqs/1/