最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript動畫系列之模擬滾動條

 更新時間:2016年12月13日 11:41:16   作者:小火柴的藍(lán)色理想  
本文主要介紹了js動畫模擬滾動條的實(shí)現(xiàn)原理以及分享了通過滾動條實(shí)現(xiàn)的幾個應(yīng)用的實(shí)例代碼:1.通過移動滾動條來實(shí)現(xiàn)數(shù)字的加減;2.通過拖動滾動條來實(shí)現(xiàn)元素尺寸的變化,以改變元素寬度為例;3.通過拖動滾動條來實(shí)現(xiàn)內(nèi)容滾。需要的朋友一起來看下吧

前面的話

當(dāng)元素內(nèi)容溢出元素尺寸范圍時,會出現(xiàn)滾動條。但由于滾動條在各瀏覽器下表現(xiàn)不同,兼容性不好。所以,模擬滾動條也是很常見的應(yīng)用。本文將詳細(xì)介紹滾動條模擬

原理介紹

滾動條模擬實(shí)際上和元素模擬拖拽類似。僅僅通過范圍限定,使元素只可以在單一方向上拖拽

<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-瀏覽器阻止默認(rèn)行為
 if(test.setCapture){test.setCapture();}
 //阻止默認(rèn)行為
 return false;
}
</script>

通過將上面代碼封裝成函數(shù),可以實(shí)現(xiàn)橫向和縱向兩種滾動條

<div id="box1" 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="test1" 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>
<div id="box2" style="height: 16px;width: 200px;background-color:#F5F5F5;border-radius:10px;box-shadow:inset 0 0 6px rgba(0,0,0,0.3);position:relative;">
 <div id="test2" style="height: 16px;width: 60px;background-color:#D62929;box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px;position:absolute;"></div>
</div>
<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-瀏覽器阻止默認(rèn)行為
  if(obj.setCapture){obj.setCapture();}
  //阻止默認(rèn)行為
  return false;
 } 
}
scrollbar(test1);
scrollbar(test2,'x')
</script>

應(yīng)用

下面來介紹通過滾動條實(shí)現(xiàn)的幾個應(yīng)用

數(shù)字加減

通過移動滾動條來實(shí)現(xiàn)數(shù)字的加減。比例關(guān)系為:

滾動條已移動距離/滾動條可移動距離= 數(shù)字當(dāng)前值/數(shù)字最大值

<div id="box" style="height: 16px;width: 200px;display:inline-block;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: 16px;width: 60px;background-color:#D62929;box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px;position:absolute;"></div>
</div>
<span id="result">0</span>
<script>
function scrollbar(obj,str,max){
 obj.onmousedown = function(e){
  e = e || event;
  var that = this;
  //比例系數(shù)
  var ratio;
  //x軸方向
  if(str == 'x'){
   var disX = e.clientX - this.offsetLeft;
   ratio = max/(this.parentNode.offsetWidth - this.offsetWidth);
  //否則為y軸方向
  }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();}
  }
  //IE8-瀏覽器阻止默認(rèn)行為
  if(obj.setCapture){obj.setCapture();}
  //阻止默認(rèn)行為
  return false;
 } 
}
scrollbar(test,'x',100);
</script>

元素尺寸

通過拖動滾動條來實(shí)現(xiàn)元素尺寸的變化,以改變元素寬度為例。比例關(guān)系為:

滾動條已移動距離/滾動條可移動距離= 元素當(dāng)前寬度/元素最大寬度

<div id="box" style="height: 16px;width: 200px;display:inline-block;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: 16px;width: 60px;background-color:#D62929;box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px;position:absolute;"></div>
</div>
<span id="result" style="width: 1px;height: 50px;background-color:pink;display:inline-block;"></span>
<script>
function scrollbar(obj,str,max){
 obj.onmousedown = function(e){
  e = e || event;
  var that = this;
  //比例系數(shù)
  var ratio;
  //x軸方向
  if(str == 'x'){
   var disX = e.clientX - this.offsetLeft;
   ratio = max/(this.parentNode.offsetWidth - this.offsetWidth);
  //否則為y軸方向
  }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.style.width = Math.round(ratio * 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'; 
    result.style.width = Math.round(ratio * T) + 'px'; 
   }
  }
  document.onmouseup = function(){
   document.onmousemove = null;
   //釋放全局捕獲
   if(obj.releaseCapture){obj.releaseCapture();}
  }
  //IE8-瀏覽器阻止默認(rèn)行為
  if(obj.setCapture){obj.setCapture();}
  //阻止默認(rèn)行為
  return false;
 } 
}
scrollbar(test,'x',100);
</script>

內(nèi)容滾動

通過拖動滾動條來實(shí)現(xiàn)內(nèi)容滾動,比例關(guān)系為:

滾動條已移動距離/滾動條可移動距離= 內(nèi)容已移動距離/內(nèi)容可移動距離

<div id="box" style="height: 200px;width: 16px;display:inline-block;background-color:#F5F5F5;border-radius:10px;box-shadow:inset 0 0 6px rgba(0,0,0,0.3);position:relative;vertical-align:middle;">
 <div id="test" style="height: 60px;width: 16px;background-color:#D62929;box-shadow:inset 0 0 6px rgba(0,0,0,.3);border-radius:10px;position:absolute;"></div>
</div>
<span id="result" style="width: 100px;height: 200px;background-color:pink;display:inline-block;line-height:30px;vertical-align:middle;position:relative;overflow:hidden;"><div id="resultIn" style="position:absolute;top:0;">測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br>測試文字<br></div></span>
<script>
function scrollbar(obj,str){
 var max = result.offsetHeight - resultIn.offsetHeight;
 obj.onmousedown = function(e){
  e = e || event;
  var that = this;
  //比例系數(shù)
  var ratio;
  //x軸方向
  if(str == 'x'){
   var disX = e.clientX - this.offsetLeft;
   ratio = max/(this.parentNode.offsetWidth - this.offsetWidth);
  //否則為y軸方向
  }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'; 
    resultIn.style.top = Math.round(ratio * 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'; 
    resultIn.style.top = Math.round(ratio * T) + 'px';
   }
  }
  document.onmouseup = function(){
   document.onmousemove = null;
   //釋放全局捕獲
   if(obj.releaseCapture){obj.releaseCapture();}
  }
  //IE8-瀏覽器阻止默認(rèn)行為
  if(obj.setCapture){obj.setCapture();}
  //阻止默認(rèn)行為
  return false;
 } 
}
scrollbar(test,'y');
</script>

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • uniapp頁面通訊講解之uni.$emit、uni.$on、uni.$once和uni.$off

    uniapp頁面通訊講解之uni.$emit、uni.$on、uni.$once和uni.$off

    uni-app?是一個使用vue.js開發(fā)所有前端應(yīng)用的框架,下面這篇文章主要給大家介紹了關(guān)于uniapp頁面通訊之uni.$emit、uni.$on、uni.$once和uni.$off的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • Array, Array Constructor, for in loop, typeof, instanceOf

    Array, Array Constructor, for in loop, typeof, instanceOf

    雖然在 JavaScript 中數(shù)組是是對象,但是沒有好的理由去使用 `for in` 循環(huán) 遍歷數(shù)組。相反,有一些好的理由不去使用 for in 遍歷數(shù)組。
    2011-09-09
  • JS面試之手寫節(jié)流防抖詳解

    JS面試之手寫節(jié)流防抖詳解

    作為一個程序員,代碼實(shí)現(xiàn)才是能力體現(xiàn),在大部分面試的時候,我們都會被要求手寫代碼實(shí)現(xiàn)一個功能,本文總結(jié)了一下經(jīng)常被面試官問到的節(jié)流和防抖功能的實(shí)現(xiàn),分享給有需要的小伙伴
    2023-07-07
  • javascript轉(zhuǎn)換靜態(tài)圖片,增加粒子動畫效果

    javascript轉(zhuǎn)換靜態(tài)圖片,增加粒子動畫效果

    這篇文章主要介紹了javascript轉(zhuǎn)換靜態(tài)圖片,增加粒子動畫效果,非常的炫酷,需要的朋友可以參考下
    2015-05-05
  • javascript 彈出層居中效果的制作

    javascript 彈出層居中效果的制作

    做一個帶拖動的彈出層效果(像提示框那種) ,看了下代碼與實(shí)現(xiàn)效果,值得學(xué)習(xí)應(yīng)用。
    2009-09-09
  • 深入理解ES6的迭代器與生成器

    深入理解ES6的迭代器與生成器

    本篇文章主要介紹了深入理解ES6的迭代器與生成器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • web網(wǎng)頁按比例顯示圖片實(shí)現(xiàn)原理及js代碼

    web網(wǎng)頁按比例顯示圖片實(shí)現(xiàn)原理及js代碼

    由于上傳圖片的大小是未知的,在顯示成縮略圖的時候必須進(jìn)行按比例的縮放才能美觀地顯示,本文將為大家簡單的介紹一種比較不錯的方法,有此需求的朋友可以參考下
    2013-08-08
  • 微信小程序左滑刪除實(shí)現(xiàn)代碼實(shí)例

    微信小程序左滑刪除實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了微信小程序左滑刪除實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • js密碼強(qiáng)度校驗

    js密碼強(qiáng)度校驗

    這篇文章主要介紹了javascript密碼強(qiáng)度校驗的實(shí)現(xiàn)方法,并給出了詳細(xì)代碼,需要的朋友可以參考下
    2015-11-11
  • JS數(shù)組去重與取重的示例代碼

    JS數(shù)組去重與取重的示例代碼

    本篇文章主要是對JS數(shù)組去重與取重的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01

最新評論

南丰县| 安庆市| 九龙城区| 新宾| 巴青县| 滦平县| 金山区| 庆云县| 南陵县| 永嘉县| 高青县| 札达县| 乐安县| 集贤县| 巴彦淖尔市| 凤翔县| 余干县| 新昌县| 大名县| 陇南市| 卢湾区| 如东县| 淳安县| 英吉沙县| 大埔县| 札达县| 株洲县| 舞钢市| 昌都县| 宣城市| 河间市| 马尔康县| 长垣县| 台东市| 葵青区| 如东县| 油尖旺区| 云安县| 铜鼓县| 绿春县| 文登市|