溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

js如何實(shí)現(xiàn)水平和豎直滑動(dòng)條

發(fā)布時(shí)間:2021-08-10 13:44:55 來源:億速云 閱讀:144 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹js如何實(shí)現(xiàn)水平和豎直滑動(dòng)條,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

首先來看水平滑動(dòng)條,效果圖如下:

js如何實(shí)現(xiàn)水平和豎直滑動(dòng)條

代碼如下:

<html>
 <head>
  <meta charset="UTF-8">
  <title>水平滑動(dòng)條</title>
  <style>
   * {
    margin: 0;
    padding: 0;
   }
 
   .scroll {
    margin: 100px;
    width: 500px;
    height: 5px;
    background: #ccc;
    position: relative;
   }
 
   .bar {
    width: 10px;
    height: 20px;
    background: #369;
    position: absolute;
    top: -7px;
    left: 0;
    cursor: pointer;
   }
   p{
    margin-left: 100px;
   }
  </style>
 </head>
 <body>
  <div class="scroll" id="scroll">
   <div class="bar" id="bar">
   </div>
  </div>
  <p></p>
  <script>
   //獲取元素
   var scroll = document.getElementById('scroll');
   var bar = document.getElementById('bar');
   var ptxt = document.getElementsByTagName('p')[0];
   bar.onmousedown = function(event) {
    var event = event || window.event;
    //頁面事件的X減去當(dāng)前相對于最近的祖先定位元素
    var x = event.clientX - this.offsetLeft;
    document.onmousemove = function(event) {
     var event = event || window.event;
     var left = event.clientX - x;
     if (left < 0)
      left = 0;
     else if (left > scroll.offsetWidth - bar.offsetWidth) {
      left = scroll.offsetWidth - bar.offsetWidth;
     }
     //改變滑塊的left
     bar.style.left = left + "px";
     ptxt.innerHTML = "當(dāng)前滑塊的移動(dòng)的百分比:" + parseInt(left / (scroll.offsetWidth - bar.offsetWidth) * 100) + "%";
     //防止選擇內(nèi)容
     window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
    }
 
   }
   //當(dāng)鼠標(biāo)彈起的時(shí)候,不做任何操作
   document.onmouseup = function() {
    document.onmousemove = null;
   }
  </script>
 </body>
</html>

豎直滑動(dòng)條效果圖如下:

js如何實(shí)現(xiàn)水平和豎直滑動(dòng)條

代碼如下:

<html>
 <head>
  <meta charset="UTF-8">
  <title>豎直滑動(dòng)條</title>
  <style>
   * {
    margin: 0;
    padding: 0;
   }
   .scroll{
    margin: 100px;
    width: 5px;
    height: 320px;
    background: #ccc;
    position: relative;
   }
 
   .bar {
    width: 15px;
    height: 5px;
    background: #369;
    position: absolute;
    top: 0px;
    left: -5;
    cursor: pointer;
   }
   p{
    margin-left: 100px;
   }
  </style>
 </head>
 <body>
  <div class="scroll" id="scroll">
   <div class="bar" id="bar">
   </div>
  </div>
  <p></p>
  <script>
   //獲取元素
   var scroll = document.getElementById("scroll");
   var bar = document.getElementById("bar");
   var ptxt = document.getElementsByTagName('p')[0];
   //添加事件
   bar.onmousedown = function(event) {
    var event = event || window.event;
    //頁面事件的Y減去當(dāng)前相對于最近的祖先定位元素
    var y = event.clientY - this.offsetTop;
    // 拖動(dòng)需要寫到down里面
    document.onmousemove = function(event) {
     var event = event || window.event;
     //獲取移動(dòng)的距離
     var top = event.clientY - y;
     if (top < 0){
      top = 0;
     }
     else if (top > scroll.offsetHeight - bar.offsetHeight){
      top = scroll.offsetHeight - bar.offsetHeight;
     }
     //改變滑塊的top
     bar.style.top = top + "px";
     //按照百分比得到當(dāng)前滑動(dòng)的距離
     ptxt.innerHTML = "當(dāng)前滑塊的移動(dòng)的百分比:" + parseInt(top/(scroll.offsetHeight - bar.offsetHeight) * 100) + "%";
     //防止選擇內(nèi)容
     window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
    }
   }
   //當(dāng)鼠標(biāo)彈起的時(shí)候,不做任何操作
   document.onmouseup = function() {
    document.onmousemove = null; 
   }
   
  </script>
 </body>
</html>

這里之所以加入移動(dòng)百分比的展示效果,主要是考慮到后續(xù)如果需要對接后臺的數(shù)據(jù)就可以達(dá)到動(dòng)態(tài)控制的目的。

以上是“js如何實(shí)現(xiàn)水平和豎直滑動(dòng)條”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI