[英]How to auto scroll to bottom with auto refresh in jquery
我正在使用此jquery ajax从数据库接收味精。 但是,当有人尝试发送消息时,消息就到了,但无法自动滚动到底部。 任何想法?
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"); });
您可以在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>
并且要确保滚动之前确实存在新的DOM元素,您可以尝试添加一些setTimeout
几毫秒,然后在其中添加此代码。
检查一下: https : //www.electrictoolbox.com/jquery-scroll-bottom/
以下Javascript将使用jQuery将页面滚动到底部:
$('html, body').animate({scrollTop:$(document).height()}, 'slow');
显然,您可以将动画速度(在上例中为“慢”)更改为“快”或以毫秒为单位的数字持续时间,其中数字越大,动画越慢。
要将滚动功能绑定到对页面中现有元素的点击上,请执行以下操作,其中#someID是元素的ID:
$(document).ready(function() {
$('#someID').click(function(){
$('html, body').animate({scrollTop:$(document).height()}, 'slow');
return false;
});
});
您可以尝试此技巧。 看看这是否对您有帮助:
$("html, body").animate({ scrollTop: $(document).height() }, 20000);
setTimeout(function() {
location.reload();
},20000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.