javascript - 隐藏另一个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/
解决方案
您可以尝试使用 css() 将可见性更改为隐藏,而不是执行 hide()。将可见性设置为隐藏不会影响其他元素的位置。
查询:
$('.cht').css('visibility','hidden').attr('hide','true');
$('#btn-chat').click(function () {
$('.cht[hide=true]:first').css('visibility','visible').attr('hide','false');
});
推荐阅读
- vba - 选择单元格时创建文件夹和子文件夹
- vue.js - 你如何在 firebase 服务工作者和 Vue/Vuex 网络应用程序之间进行通信
- xamarin - 如何在 Xamarin.iOS 中使用 Google 的人员 API?
- python - 为什么“5 in range(5,6) in [range(5,6)]”返回True?
- java - java rest 调用获取 url net::ERR_FAILED
- php - 在 PHP 中删除和创建 MySQL 视图不起作用
- python - 如何在while循环中做elif
- java - 如何使用 androidx.recyclerview.selection 选择 Recyclerview 中列出的所有项目?
- python-3.x - 在 ZMQ PUSH/PULL 如何在 python 中知道套接字状态(连接与否)?
- c# - 如何将 Json 数组键转换为小写?