欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现兼容PC端和移动端滑块拖动选择数字效果

程序员文章站 2023-12-28 12:19:58
本文为大家分享了js实现移动端滑块拖动选择数字的效果,可以通过鼠标拖拽滑动效果来选择数字,供大家参考,具体内容如下 ...

本文为大家分享了js实现移动端滑块拖动选择数字的效果,可以通过鼠标拖拽滑动效果来选择数字,供大家参考,具体内容如下

<!doctype html>
<html lang="zh-cn">

  <head>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>鼠标拖动小方块</title>
    <style type="text/css">
      .linediv {
        position: relative;
        height: 5px;
        background: red;
        width: 300px;
        margin: 50px auto;
      }

      .linediv .mindiv {
        position: absolute;
        top: -5px;
        left: 0;
        width: 15px;
        height: 15px;
        background: green;
        cursor: pointer
      }

      .linediv .mindiv .vals {
        position: absolute;
        font-size: 20px;
        top: -45px;
        left: -10px;
        width: 35px;
        height: 35px;
        line-height: 35px;
        text-align: center;
        background: blue;
      }

      .linediv .mindiv .vals:after {
        content: "";
        width: 0px;
        height: 0px;
        border-top: 6px solid blue;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-bottom: 6px solid transparent;
        display: block;
        margin-left: 11px;
      }
    </style>
  </head>

  <body>
    <center>
      <h3>用鼠标拖动小方块<span id="msg">0</span>%</h3>
    </center>
    <div id="linediv" class="linediv">
      <div id="mindiv" class="mindiv">
        <div id="vals" class="vals">0</div>
      </div>
    </div>
    <script>
      window.onload = function() {

        var linediv = document.getelementbyid('linediv'); //长线条
        var mindiv = document.getelementbyid('mindiv'); //小方块
        var msg = document.getelementbyid("msg");
        var vals = document.getelementbyid("vals");
        var ifbool = false; //判断鼠标是否按下
        //事件
        var start = function(e) {
          e.stoppropagation();
          ifbool = true;
          console.log("鼠标按下")
        }
        var move = function(e) {
          console.log("鼠标拖动")
          if(ifbool) {
            if(!e.touches) {  //兼容移动端
              var x = e.clientx;
            } else {   //兼容pc端
              var x = e.touches[0].pagex;
            }
            //var x = e.touches[0].pagex || e.clientx; //鼠标横坐标var x
            var linediv_left = getposition(linediv).left; //长线条的横坐标
            var mindiv_left = x - linediv_left; //小方块相对于父元素(长线条)的left值 
            if(mindiv_left >= linediv.offsetwidth - 15) {
              mindiv_left = linediv.offsetwidth - 15;
            }
            if(mindiv_left < 0) {
              mindiv_left = 0;
            }
            //设置拖动后小方块的left值
            mindiv.style.left = mindiv_left + "px";
            msg.innertext = parseint((mindiv_left / (linediv.offsetwidth - 15)) * 100);
            vals.innertext = parseint((mindiv_left / (linediv.offsetwidth - 15)) * 100);
          }
        }
        var end = function(e) {
            console.log("鼠标弹起")
            ifbool = false;
          }
          //鼠标按下方块
        mindiv.addeventlistener("touchstart", start);
        mindiv.addeventlistener("mousedown", start);
        //拖动
        window.addeventlistener("touchmove", move);
        window.addeventlistener("mousemove", move);
        //鼠标松开
        window.addeventlistener("touchend", end);
        window.addeventlistener("mouseup", end);
        //获取元素的绝对位置
        function getposition(node) {
          var left = node.offsetleft; //获取元素相对于其父元素的left值var left
          var top = node.offsettop;
          current = node.offsetparent; // 取得元素的offsetparent
            // 一直循环直到根元素
            
          while(current != null) {  
            left += current.offsetleft;  
            top += current.offsettop;  
            current = current.offsetparent;  
          }
          return {
            "left": left,
            "top": top
          };
        }
      }
    </script>
  </body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: