如何在使用div类时将自动滚动到div工作的底部 [英] How do I get autoScroll to the bottom of a div working when using div class
本文介绍了如何在使用div类时将自动滚动到div工作的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个区域,当从ajax请求中收到新文字时,会自动滚动到底部。
CSS
CSS
div.scroll {
height: 400px;
width: 95%;
overflow: auto;
/* border: 1px solid #666; */
background-color: #ccc;
padding: 8px;
}
HTML
<html>
<head>
<script type="text/javascript">
//Handle ajax data
$(document).ready(function()
{
setInterval(doAjaxStuff, 2000);
});
function doAjaxStuff()
{
$.ajax
({
url: "http:somewhere/getLog/",
dataType: 'json',
success: function(json)
{
if(json.status == "on")
{
$('#log').html(json.log);
}
}
});
</script>
</head>
<body>
<div class="scroll" id =log >pleas wait for log</div>
</body>
</html>
推荐答案
您可以使用 scrollTop()
功能:
You can use the scrollTop()
function:
//will only auto-scroll if scroll position is already at bottom
var doAutoscroll = ($("#log").scrollTop()==$("#log")[0].scrollHeight);
$('#log').html(json.log)
if (doAutoscroll) $("#log").scrollTop($("#log")[0].scrollHeight);
这篇关于如何在使用div类时将自动滚动到div工作的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文