聊天窗口内容滚动到底部的方法scrollTop和scrollIntoView
2018-03-14 09:49
351 查看
测试内容样式:
<style type="text/css">
#container{
overflow-y:auto;
overflow-x:hidden;
height:150px;
border:1px solid red;
width:350px;
}
html,body{margin:0px;padding:0px;font-family: "微软雅黑";}
</style>
1、第一种方法:scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script>
//一进来就滚动到底部
$("#container").scrollTop($("#container")[0].scrollHeight);
$("#btnSubmit").click(function(){
//如果没有内容就return
var txtVal=$("#msgInput").val();
// alert(txtVal);
if(txtVal==''){
return false;
}
//有内容继续
$('#msgList').append('<li>'+txtVal+'</li>');
$("#container").scrollTop($("#container")[0].scrollHeight);
})
</script>浏览器兼容:
var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
2、第二种方法:使用JS原生提供的Element.scrollIntoView()可以定位到指定元素的位置
<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script>
//进来的时候自动滚到底部
($('#msgList').children("li:last-child")[0]).scrollIntoView();
$("#btnSubmit").click(function(){
//如果没有内容就return
var txtVal=$("#msgInput").val();
// alert(txtVal);
if(txtVal==''){
return false;
}
//有内容继续
$('#msgList').append('<li>'+txtVal+'</li>');
setTimeout(function () {
($('#msgList').children("li:last-child")[0]).scrollIntoView();
},100);
})
</script>直接用:<li onclick="javascript:document.getElementById('here').scrollIntoView()"></li>
<style type="text/css">
#container{
overflow-y:auto;
overflow-x:hidden;
height:150px;
border:1px solid red;
width:350px;
}
html,body{margin:0px;padding:0px;font-family: "微软雅黑";}
</style>
<div id="container"> <ul id="msgList" > <li>111111111</li> <li>2222222222</li> <li>333333333</li> <li>4444444444</li> <li>5555555555</li> <li>66666666666</li> <li>77777777777</li> </ul> </div> <div class="footer"> <textarea id='msgInput' rows="2" placeholder="输入点什么" class="msg-input"></textarea> <input type="button" class="btn" id='btnSubmit'value="发送" onclick="" type="reset"> </div>
1、第一种方法:scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script>
//一进来就滚动到底部
$("#container").scrollTop($("#container")[0].scrollHeight);
$("#btnSubmit").click(function(){
//如果没有内容就return
var txtVal=$("#msgInput").val();
// alert(txtVal);
if(txtVal==''){
return false;
}
//有内容继续
$('#msgList').append('<li>'+txtVal+'</li>');
$("#container").scrollTop($("#container")[0].scrollHeight);
})
</script>浏览器兼容:
var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
2、第二种方法:使用JS原生提供的Element.scrollIntoView()可以定位到指定元素的位置
<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script>
//进来的时候自动滚到底部
($('#msgList').children("li:last-child")[0]).scrollIntoView();
$("#btnSubmit").click(function(){
//如果没有内容就return
var txtVal=$("#msgInput").val();
// alert(txtVal);
if(txtVal==''){
return false;
}
//有内容继续
$('#msgList').append('<li>'+txtVal+'</li>');
setTimeout(function () {
($('#msgList').children("li:last-child")[0]).scrollIntoView();
},100);
})
</script>直接用:<li onclick="javascript:document.getElementById('here').scrollIntoView()"></li>
相关文章推荐
- javascript通过改变滚动条滚动来显示某些元素的scrollIntoView()方法
- scrollIntoView()窗口滚动
- (六十六)TableView内容超过一屏时滚动到屏幕底部的方法
- (六十六)TableView内容超过一屏时滚动到屏幕底部的方法
- ScrollInToView方法(滚动页面)
- jquery mobile 使滚动条滚动到底部scrollTop函数无效的解决方法
- jquery获取窗口高度的方法及判断scroll滚动到底部
- ListView数据更新后,自动滚动到底部(聊天时常用)| Listview Scroll to the end of the list after updating the list
- android ScrollView滚动到底部
- android实现上面的内容滚动,底部放置一固定的按钮的方法
- 实现HorizontalScrollView随ViewPager滚动的简易方法
- 使用scrollIntoView()进行内容定位
- HTML5 新的API 窗口可视区 scrollIntoView dataset calssList
- ListBox的ScrollIntoView方法
- 解决js页面滚动效果scrollTop在FireFox与Chrome浏览器间的兼容问题的方法
- UIScrollView 默认滚动到最底部 --比如聊天界面
- scrollIntoView()方法
- (七十一)关于UITableView退出崩溃的问题和滚动到底部的方法
- 解决ScrollView里如果有动态更新的ChildView时会自动滚动到底部的方法
- iOS 导航条透明(以及自定义颜色)的方法,去掉导航条底部黑线,随着tableView滚动设置导航条变色