简体   繁体   中英

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

I have 5 div(s) and a button below those div(s). I want to hide those div(s) before I click the button to show the div one by one but the problem is when I hide the div(s), the button below will change the position to the top position of the hidden 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%;
}

JQuery:

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

I also provide a fiddle here: https://jsfiddle.net/pg71jw2t/

Instead of performing hide(), you can try to change the visibility to hidden using css(). Setting the visibility to hidden will not affect the position of others elements.

JQuery:

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

You've set position: fixed for the button, but didn't provide its position on the screen. Just add something like bottom: 100px; right: 30px; bottom: 100px; right: 30px; . And you may want to delete margin to avoid confusion.

https://jsfiddle.net/sLcpnj1w/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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