DIV CSS 佈局教程網

JavaScript 動畫系列:模擬自定義滾動條
編輯:關於JavaScript     

當元素內容溢出元素尺寸範圍時,會出現滾動條。但由於滾動條在各瀏覽器下表現不一、兼容性不佳,因此模擬滾動條成了很常見的應用。本文將詳細介紹滾動條的模擬實現。

原理介紹

滾動條的模擬,本質上與元素的模擬拖拽類似——僅僅透過範圍限定,讓元素只能在單一方向上拖拽。

 <div id="box" style="height: 200px;width: 16px;background-color:#F5F5F5;border-radius:10px;box-shadow:inset 0 0 6px rgba(0,0,0,0.3);position:relative;"> <div id="test" style="height: 60px;width: 16px;background-color:#555;box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px;position:absolute;"></div> </div> <script> test.onmousedown = function(e){ e = e || event; var that = this; var disY = e.clientY - this.offsetTop; document.onmousemove = function(e){ e = e || event; var T = e.clientY - disY; if(T < 0){T = 0;} var TMax = parseInt(box.style.height) - that.offsetHeight; if(T > TMax){T = TMax;} that.style.top = T + 'px'; } document.onmouseup = function(){ document.onmousemove = null; //釋放全局捕獲 if(test.releaseCapture){test.releaseCapture();} } //IE8- 瀏覽器阻止默認行為 if(test.setCapture){test.setCapture();} //阻止默認行為 return false; } </script>

封裝為通用函數

將上述代碼封裝成函數,即可同時實現橫向與縱向兩種滾動條:

 function scrollbar(obj,str){ obj.onmousedown = function(e){ e = e || event; var that = this; //x 軸方向 if(str == 'x'){ var disX = e.clientX - this.offsetLeft; //否則為 y 軸方向 }else{ var disY = e.clientY - this.offsetTop; } document.onmousemove = function(e){ e = e || event; if(str == 'x'){ var L = e.clientX - disX; if(L < 0){L = 0;} var LMax = parseInt(obj.parentNode.style.width) - that.offsetWidth; if(L > LMax){L = LMax;} that.style.left = L + 'px'; }else{ var T = e.clientY - disY; if(T < 0){T = 0;} var TMax = parseInt(obj.parentNode.style.height) - that.offsetHeight; if(T > TMax){T = TMax;} that.style.top = T + 'px'; } } document.onmouseup = function(){ document.onmousemove = null; //釋放全局捕獲 if(obj.releaseCapture){obj.releaseCapture();} } //IE8- 瀏覽器阻止默認行為 if(obj.setCapture){obj.setCapture();} //阻止默認行為 return false; } } scrollbar(test1); scrollbar(test2,'x') </script>

應用場景

數字加減

透過移動滾動條來實現數字的加減,比例關係為:滾動條已移動距離 / 滾動條可移動距離 = 數字當前值 / 數字最大值。

 function scrollbar(obj,str,max){ obj.onmousedown = function(e){ e = e || event; var that = this; var ratio; if(str == 'x'){ var disX = e.clientX - this.offsetLeft; ratio = max/(this.parentNode.offsetWidth - this.offsetWidth); }else{ var disY = e.clientY - this.offsetTop; ratio =max/(this.parentNode.offsetHeight - this.offsetHeight); } document.onmousemove = function(e){ e = e || event; if(str == 'x'){ var L = e.clientX - disX; if(L < 0){L = 0;} var LMax = parseInt(obj.parentNode.style.width) - that.offsetWidth; if(L > LMax){L = LMax;} that.style.left = L + 'px'; result.innerHTML = Math.round(ratio * L); }else{ var T = e.clientY - disY; if(T < 0){T = 0;} var TMax = parseInt(obj.parentNode.style.height) - that.offsetHeight; if(T > TMax){T = TMax;} that.style.top = T + 'px'; result.innerHTML = Math.round(ratio * T); } } document.onmouseup = function(){ document.onmousemove = null; if(obj.releaseCapture){obj.releaseCapture();} } if(obj.setCapture){obj.setCapture();} return false; } } scrollbar(test,'x',100); </script>

元素尺寸

透過拖動滾動條改變元素尺寸(以改變寬度為例),比例關係為:滾動條已移動距離 / 滾動條可移動距離 = 元素當前寬度 / 元素最大寬度。

 //核心邏輯與上例一致,僅將 result.innerHTML 替換為 result.style.width = Math.round(ratio * L) + 'px'; 

內容滾動

透過拖動滾動條實現內容滾動,比例關係為:滾動條已移動距離 / 滾動條可移動距離 = 內容已移動距離 / 內容可移動距離。此時只需在 mousemove 中改動內容容器的 top 值即可:

 //在 mousemove 中 resultIn.style.top = Math.round(ratio * T) + 'px'; 即可實現內容隨滾動條同步滾動 

以上就是本文的全部內容,希望對大家的學習或工作能帶來幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved