(function(i) {
$(".click").click(function() {
if (parseInt($(".chat .placeholder").css("margin-bottom")) > 32) {
$(".chat .placeholder").css("margin-bottom", "-=50px");
}
else if ($(".chat .placeholder").css("margin-bottom") == "32px") {
$(".chat .placeholder").css("margin-bottom", "0px");
}
$(".chat").append("<p>hey " + (++i) + "</p>");
$(".chat").scrollTop($(".chat").prop('scrollHeight') - $(".chat").outerHeight());
})
})(0);
.main .chat {
width: 100%;
position: absolute;
top: 30px;
background: red;
box-sizing: border-box;
height: 200px;
overflow: auto;
}
.chat p {
background: blue;
padding: 16px 20px;
margin: 0;
}
.main .header {
position: relative;
background: yellow;
height: 18px;
}
p.placeholder {
width: 100%;
padding: 0;
margin-top: 0;
margin-bottom: 132px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' class='click' value='add'>
<div class='main'>
<div class='chat'>
<p class="placeholder header">Header</p>
<p>hello</p>
</div>
</div>
謝謝t他回答說,但z-索引只能使「標題」在其他divs「絕對||相關」和像固定div一樣的行爲上可見。我想聊天divs擴展並將頁眉擴展出屏幕,創建一個滾動條主要div在底部保留新消息。完全像Facebook一樣。 –
@RicardoGomes檢查編輯。 – user31782
這裏還是一樣的。頭固定。我可以用display:flex ..但是我只需要用css2 ..檢查flex版本https://jsfiddle.net/cLtkt0sw/。謝謝 –