繁体   English   中英

隐藏另一个 div 时如何使 div 保持原位?

[英]How to make div stay in place when another div is hidden?

我有 5 个 div(s) 和这些 div(s) 下方的按钮。 我想在单击按钮之前隐藏那些 div(s) 以一个一个地显示 div 但问题是当我隐藏 div(s) 时,下面的按钮会将位置更改为隐藏 div 的顶部位置。

HTML:

<div class="wrapper">
       <div class="monitor-box">
            <div id="chat1" class="speech-bubble-kiri cht">
                <p class="chat-text">Hai 👋</p>
            </div>
            <div id="chat2" class="speech-bubble-kanan cht">
                <p class="chat-text">Hai juga 🥰</p>
            </div>
            <div id="chat3" class="speech-bubble-kiri cht">
                <p class="chat-text">Lagi ngapain?</p>
            </div>
            <div id="chat4" class="speech-bubble-kanan cht">
                <p class="chat-text">Mikirin kamu</p>
            </div>
            <div id="chat5" class="speech-bubble-kiri cht">
                <p class="chat-text">Aww, so sweet 🤗💕</p>
            </div>
            <div class="chat-button">
                <button id="btn-chat">SEND</button>
            </div>
        </div>
    </div>

CSS:

html, body {
    background-image: url(https://api.time.com/wp-content/uploads/2020/11/GettyImages-1207834649.jpg);
    overflow-y: hidden;
    background-size: cover;
    background-repeat: no-repeat;
}

.wrapper {
    width: 100%;
    display: flex;
    justify-content: center;
}

.bg-img {
    position: absolute;
    width: 80%;
}

.monitor-box {
    backdrop-filter: blur(10px);
    width: 720px;
    height: 500px;
    border: 10px solid black;
    border-radius: 20px;
    margin-top: 5%;
}

.chat-text {
    margin-left: 5%;
    padding-top: 4%;
}

.speech-bubble-kiri {
    position: relative;
    background: white;
    border-radius: 20px;
    width: 50%;
    height: 10%;
    margin: 5%;
}

.speech-bubble-kiri:after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 10%;
    width: 0;
    height: 0;
    border: 15px solid transparent;
    border-top-color: white;
    border-bottom: 0;
    border-left: 0;
    margin-left: -7.5px;
    margin-bottom: -15px;
}

.speech-bubble-kanan {
    position: relative;
    background: white;
    border-radius: 20px;
    width: 50%;
    height: 10%;
    margin-left: auto;
    margin-right: 5%;
}

.speech-bubble-kanan:after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 90%;
    width: 0;
    height: 0;
    border: 15px solid transparent;
    border-top-color: white;
    border-bottom: 0;
    border-right: 0;
    margin-left: -7.5px;
    margin-bottom: -15px;
}

.chat-button {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    margin-top: -20px;
}

#btn-chat {
    background-color:yellowgreen;
    color: white;
    border: none;
    padding: 10px;
    border-radius: 20px;
    margin-right: 1.5em;
    cursor: pointer;
    transition: all .2s ease-in-out;
    position: fixed;
}

#btn-chat:hover {
    transform: scale(1.1);
}

.o {
    width: 100%;
    height: 100%;
}

查询:

$('.cht').hide();
$('#btn-chat').click(function () {
    $('.cht:hidden:first').show();
});

我还在这里提供了一个小提琴: https : //jsfiddle.net/pg71jw2t/

您可以尝试使用 css() 将可见性更改为隐藏,而不是执行 hide()。 将可见性设置为隐藏不会影响其他元素的位置。

查询:

$('.cht').css('visibility','hidden').attr('hide','true');
$('#btn-chat').click(function () {
    $('.cht[hide=true]:first').css('visibility','visible').attr('hide','false');
});

您已设置position: fixed按钮,但未提供其在屏幕上的位置。 只需添加类似bottom: 100px; right: 30px; bottom: 100px; right: 30px; . 您可能希望删除边距以避免混淆。

https://jsfiddle.net/sLcpnj1w/

暂无
暂无

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

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