html
<div id="speech"></div>
<div id="test"></div>
<div id="testt"></div>
CSS
/*speech bubble*/
.bubble {
position: relative;
width: auto;
height: 40px;
padding-left: 10px;
padding-right: 10px;
background: #FFFFFF;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
font-family: sans-serif;
font-size: 15px;
display: inline-block;
left: 17px;
vertical-align: middle;
line-height: 40px;
max-width: 240px;
float: left;
clear: both;
}
.bubble:after {
content: '';
position: absolute;
border-style: solid;
border-width: 10px 10px 10px 0;
border-color: transparent #FFFFFF;
display: block;
width: 0;
z-index: 1;
left: -10px;
top: 10px;
}
#test, #testt {
height: 100px;
width: 100px;
}
#speech {
height: 500px;
width: 200px;
float: right;
overflow-y: scroll;
background-color: #000;
}
JavaScript
$(document).ready(function () {
"use strict";
$("#test").click(function () {
$("#speech").append('<p class="bubble">test223123</p>');
});
$("#testt").click(function () {
$("#speech").append('<p class="bubble">test</p>');
});
});
当我点击“测试”几次时,附加到语音 div 中的对话气泡继续向下,但滚动的东西停留在顶部。
如何让滚动条在添加新对话泡泡时回到底部?
最佳答案
很简单,使用scrollTop函数即可。添加这一行:
$('#speech').scrollTop($('#speech').height());
您现在只需附加您的气泡。添加气泡后,您会找到 div 的高度,并使用该值向下滚动。
我做了一点codepen您可以在其中查看所有代码。
关于javascript - 自动向下移动可滚动的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31903015/