[英]How to auto scroll to bottom with auto refresh in jquery
I am using this jquery ajax to receive msg from the database. 我正在使用此jquery ajax从数据库接收味精。 But when someone trying to send a msg then it's coming here but could not auto scroll to the bottom. 但是,当有人尝试发送消息时,消息就到了,但无法自动滚动到底部。 Any idea? 任何想法?
setTimeout(startRefresh,1000);
var myKeyVals = { some value };
var saveData = $.ajax({
type: 'POST',
url: "https://example.php",
data: myKeyVals,
success: function(resultData) {
$("#chatwindow").html(resultData);
//######################################
}
});
saveData.error(function() { alert("Something went wrong"); });
You can try something using animate
& scrollTop
inside ajax success
like: 您可以在ajax success
内使用animate
和scrollTop
尝试一些操作,例如:
$("html, body").animate({ scrollTop: $(document).height() - $(window).height() }, 'slow');
div {height: 900px; border:2px solid #333; padding:10px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mydiv1">DATA FOR SAMPLE 1</div> <div id="mydiv2">SAMPLE DATA</div> <div id="mydiv2">SAMPLE DATA</div>
And to take care that new DOM elements are actually present before scrolling, you can try to add some setTimeout
for few milliseconds and add this code inside it. 并且要确保滚动之前确实存在新的DOM元素,您可以尝试添加一些setTimeout
几毫秒,然后在其中添加此代码。
check this out: https://www.electrictoolbox.com/jquery-scroll-bottom/ 检查一下: https : //www.electrictoolbox.com/jquery-scroll-bottom/
The following Javascript will scroll the page to the bottom using jQuery: 以下Javascript将使用jQuery将页面滚动到底部:
$('html, body').animate({scrollTop:$(document).height()}, 'slow');
Obviously you can change the animation speed ('slow' in the above example) to something else such as 'fast' or a numeric duration in milliseconds where the higher the number, the slower the animation. 显然,您可以将动画速度(在上例中为“慢”)更改为“快”或以毫秒为单位的数字持续时间,其中数字越大,动画越慢。
To bind the scrolling function to a click done on an existing element in the page, do something like this, where #someID is the element's ID: 要将滚动功能绑定到对页面中现有元素的点击上,请执行以下操作,其中#someID是元素的ID:
$(document).ready(function() {
$('#someID').click(function(){
$('html, body').animate({scrollTop:$(document).height()}, 'slow');
return false;
});
});
You can try this trick. 您可以尝试此技巧。 See if this helps you: 看看这是否对您有帮助:
$("html, body").animate({ scrollTop: $(document).height() }, 20000);
setTimeout(function() {
location.reload();
},20000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.