【问题标题】:Neither position absolute neither position relative is working well绝对位置和相对位置都不能正常工作
【发布时间】:2019-09-30 17:18:22
【问题描述】:

当我点击一个名为“test”的 div 时,会出现一个名为“outside”的 div,同时也会出现一个 z-index 更高的名为“inside”的 div。

我的问题是,当我将位置设置为绝对“内部”时,我无法设置边距底部。当我将位置设置为相对时,它会将我的 div“测试”放在它下面。

这可能有点难以理解,但问题真的很简单。这里有一个小提琴: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();   
                });     
            }); 
        });

    });  

【问题讨论】:

  • 请将您的代码分成 HTML、CSS 和 jQuery 部分。像这样读起来很难。

标签: javascript html css css-position


【解决方案1】:

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

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

这会产生“假”边距!

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

【讨论】:

  • +1 正确答案。但是,我不会称其为“假”保证金。绝对定位使用不同类型的定位,但它也可以声明一个边距以进一步偏移它。
猜你喜欢
  • 2022-11-15
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多