欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)音量控制拖動(dòng)

 更新時(shí)間:2020年01月15日 14:37:12   作者:SSSkyCong  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)音量控制拖動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)音量控制拖動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

描述:

JS——實(shí)現(xiàn)音量控制拖動(dòng)

    1)、有底條,有拖拽按鈕
    2)、設(shè)置最小和最大值
    3)、拖動(dòng)定位后,拋出事件當(dāng)前的所在值

效果:

實(shí)現(xiàn):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    #all {
      width: 500px;
      height: 86px;
      margin: 100px auto;
      position: relative;
    }
 
    #bar {
      width: 500px;
      height: 20px;
      border-radius: 10px;
      background: #9acfea;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
      cursor: pointer;
    }
 
    #box {
      width: 30px;
      height: 30px;
      background: #ec971f;
      position: absolute;
      bottom: 0;
      top: 0;
      margin: auto 0;
      border-radius: 50%;
      cursor: pointer;
      transition: left 0.1s linear 0s;
    }
  </style>
</head>
<body>
  <div id="all">
    <p>當(dāng)前位置0%</p>
    <div id="bar">
      <div id="box"></div>
    </div>
  </div>
<script>
 
  var all=document.getElementById("all");//容器
  var p=document.querySelector("p");//進(jìn)度百分比
  var bar=document.getElementById("bar");//進(jìn)度顯示條
  var box=document.getElementById("box");//進(jìn)度按鈕
 
  var boxL,newL,moveL,mouseX,left;
  var cha = bar.offsetWidth - box.offsetWidth;
  var index=0;//標(biāo)記狀態(tài)
 
  var evt=new Event("change");//本身的事件
  init();
  function init() {
    box.addEventListener("mousedown",mouseDownclickHandler);
    document.addEventListener("mousemove",mouseMoveclickHandler)
    document.addEventListener("mouseup",mouseUpclickHandler);
    document.addEventListener("change",changeHandler);
    bar.addEventListener("click",clickHandler);
  }
 
  function mouseDownclickHandler(e) {
    index=1;
    boxL=box.offsetLeft;
    mouseX=e.clientX;//鼠標(biāo)按下拖動(dòng)的位置
  }
 
  function mouseMoveclickHandler(e) {
    if(index===1){
      moveL=e.clientX-mouseX;//鼠標(biāo)移動(dòng)
      newL=boxL+moveL;//left值
 
      //判斷最小值與最大值
      if(newL<0){
        newL = 0;
      }
      if(newL>=cha){
        newL=cha;
      }
      // 改變left值
      box.style.left = newL + 'px';
      // 計(jì)算比例
      var bili = newL / cha * 100;
      p.textContent = '當(dāng)前位置' + Math.ceil(bili) + '%';
      evt.elem=this;//當(dāng)前指向 對(duì)象
      document.dispatchEvent(evt);//朝誰發(fā)送 拋發(fā)
    }
  }
 
  function mouseUpclickHandler(e) {
    index=0;
    evt.elem=this;//當(dāng)前指向 對(duì)象
    document.dispatchEvent(evt);//朝誰發(fā)送 拋發(fā)
  }
 
  function clickHandler(e) {
    left = e.clientX-all.offsetLeft-box.offsetWidth/2;
    if(left<0){
      left=0;
    }
    if(left>=cha){
      left=cha;
    }
    box.style.left=left+'px';
    bili=left/cha*100;
    p.innerHTML='當(dāng)前位置'+ Math.ceil(bili)+'%';
    evt.elem=this;//當(dāng)前指向 對(duì)象
    document.dispatchEvent(evt);//朝誰發(fā)送 拋發(fā)
  }
 
  function changeHandler(e) {
    console.log(e);
  }
</script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論