首页 > 解决方案 > 隐藏另一个div时如何使div保持原位?

问题描述

我有 5 个 div 和这些 div 下方的按钮。我想在单击按钮以一一显示 div 之前隐藏这些 div,但问题是当我隐藏 div 时,下面的按钮会将位置更改为隐藏 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/

标签: javascripthtmljquerycss

解决方案


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

查询:

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

推荐阅读