最新在寫聊天需求 ,遇到了個問題,就是當有新消息時,若是把老的消息頂上去。也就是把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 對其進行定義了。