最新在写聊天需求 ,遇到了个问题,就是当有新消息时,若是把老的消息顶上去。也就是把SCROLL一直在底部javascript
粘出简单的DEMO测试代码css
用了JS的mCustomScrollbar插件html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="./jquery.mCustomScrollbar.css"> </head> <body> <div style="width:500px;height:500px;overflow:hidden" id="test"> <div id="content"> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> <div style="width:400px;height:30px;backgroune:#fff">sfasdf</div> </div> </div> </body> </html> <script src="./jquery.js"></script> <script src="./jquery.mCustomScrollbar.concat.min.js"></script> <script type="text/javascript"> (function($){ $(window).on("load",function(){ $.mCustomScrollbar.defaults.scrollButtons.enable=true; //enable scrolling buttons by default $.mCustomScrollbar.defaults.axis="yx"; //enable 2 axis scrollbars by default $("#content-l").mCustomScrollbar(); $("#test").mCustomScrollbar({theme:"dark"}); }); setInterval(reask, 500, "1"); function reask(){ // alert('sfa') $("#content").append('<div style="width:400px;height:30px;backgroune:#fff">sfasdf555</div>') $("#test").mCustomScrollbar("scrollTo","bottom"); } })(jQuery); </script>
下面是相关的文档:java
mCustomScrollbar 这个插件的功能巨大,因此参数也不少,参数值固然更多。在介绍参数的时候,我想先为新手介绍两种参数值的写法,分别是直接的和合并的。jquery
咱们平时接触的插件用的参数,都是直接跟着参数写上参数值,这个比较直观简单。在这个插件中,参数值太多,因此把一些参数合并起来写。例以下面要介 绍到的 scrollButtons 这个参数,它又有四个子属性:enable、scrollType、scrollSpeed、scrollAmount,这四个属性也分别有本身的值,来 实现相应的功能。若是再加上其余的参数,那么咱们应该这样写:ajax
$("#main").mCustomScrollbar({ scrollButtons:{ enable:false, scrollType:"continuous", scrollSpeed:20, scrollAmount:40 }, horizontalScroll:false, });
必定要注意闭合的括号和语句之间的逗号,新手可能会由于不当心,没有严格的按照这个规则写致使插件没法运行。好,下面咱们介绍详细的参数。浏览器
下面是全部参数的列表和它们的默认值app
$(".content").mCustomScrollbar({ set_width:false, set_height:false, horizontalScroll:false, scrollInertia:550, scrollEasing:"easeOutCirc", mouseWheel:"auto", autoDraggerLength:true, scrollButtons:{ enable:false, scrollType:"continuous", scrollSpeed:20, scrollAmount:40 }, advanced:{ updateOnBrowserResize:true, updateOnContentResize:false, autoExpandHorizontalScroll:false, autoScrollOnFocus:true }, callbacks:{ onScrollStart:function(){}, onScroll:function(){}, onTotalScroll:function(){}, onTotalScrollBack:function(){}, onTotalScrollOffset:0, whileScrolling:false, whileScrollingInterval:30 } });
update函数
用法:$(selector).mCustomScrollbar(“update”);布局
调用 mCustomScrollbar 函数的 update 方法去实时更新滚动条当内容发生变化(例如 经过 Javascript 增长或者移除一个对象、经过 ajax 插入一段新内容、隐藏或者显示一个新内容等)
下面是例子:
$(".content .mCSB_container").append("<p>New content here...</p>"); $(".content").mCustomScrollbar("update");
$(".content .myImagesContainer").append("<img src='myNewImage.jpg' />"); $(".content .myImagesContainer img").load(function(){ $(".content").mCustomScrollbar("update"); });
$("#content-1").animate({height:800},"slow",function(){ $(this).mCustomScrollbar("update"); });
当新内容彻底加载或者动画完成以后,update 方法一直被调用。
scrollTo
用法:$(selector).mCustomScrollbar(“scrollTo”,position);
你可使用这个方法自动的滚动到你想要滚动到的位置。这个位置可使用字符串(例如 “#element-id”,“bottom” 等)描述或者是一个数值(像素单位)。下面的例子将会滚动到最下面的对象
$(".content").mCustomScrollbar("scrollTo","last");
scrollTo 方法的参数
scrollTo 方法还有两个额外的选项参数
disable
用法:$(selector).mCustomScrollbar(“disable”);
调用 disable 方法去使滚动条不可用。若是想使其从新可用,调用 update方法。disable 方法使用一个可选参数(默认 false)你能够设置 true 若是你想从新让内容区域滚动当 scrollbar 不可用时。例如:
$(".content").mCustomScrollbar("disable",true);
destroy
用法:$(selector).mCustomScrollbar(“destroy”);
调用 destroy 方法能够移除某个对象的自定义滚动条而且恢复默认样式
经过潜行者m对这些插件的使用,对这些插件的实现原理也作了一些分析。原理就是这样的:
首先获取要修改滚动条样式的内容区块,而后使用 CSS 隐藏掉默认滚动条,而后使用 Javascript 添加不少 HTML 结构,再配合 CSS 作出一个滚动条的效果。而后再使用 CSS 定义滚动条的样式,使用 Javascript 相应鼠标的滚动事件,产生滚动下滑的效果。
明白了这点,下面咱们就能够看一下滚动条的结构,而后使用 CSS 对其进行定义了。