• 元素跟随鼠标移动


    **1、index.html

    <div id="wrap">
        <div class="box"></div>
        <div class="box"></div>
    </div>
    
    • 1
    • 2
    • 3
    • 4

    2、app.js

    let MOVE_DOM_CLASS = "box";
    let wrap = document.querySelector("#wrap");
    let w_left = getWrapPositon("offsetLeft");
    let w_right = getWrapPositon("right");
    let w_top = getWrapPositon("offsetTop");
    let w_bottom = getWrapPositon("bottom");
    
    // 获取外层的边框位置
    function getWrapPositon(ps) {
      switch (ps) {
        case "offsetLeft":
          return wrap[ps];
          break;
        case "offsetTop":
          return wrap[ps];
          break;
        case "right":
          return wrap.clientWidth;
          break;
        case "bottom":
          return wrap.clientHeight;
          break;
        default:
          break;
      }
    }
    
    document.onmousedown = function (e) {
      mousedownEvent(e);
    };
    
    document.onmouseup = function (e) {
      mouseupEvent(e);
    };
    
    // 按下鼠标
    function mousedownEvent(d_ev) {
      setDomStyle(d_ev, "add");
      document.onmousemove = function (m_ev) {
        setDomPostion(m_ev, d_ev);
      };
    }
    
    // 松开鼠标
    function mouseupEvent(e) {
      setDomStyle(e, "remove");
      document.onmousemove = null;
    }
    
    // 处理 dom 样式
    function setDomStyle(e, type) {
      let box = getTarget(e);
      box && box.classList[type]("box_bg");
    }
    
    // 设置 dom 位置
    function setDomPostion(m_ev, d_ev) {
      const { x, y } = getPostion(m_ev, d_ev);
      let box = getTarget(d_ev);
      if (box) {
        box.style.left = x + "px";
        box.style.top = y + "px";
      }
    }
    
    // 获取元素位置
    function getPostion(m_ev, d_ev) {
      const { clientX, clientY } = m_ev;
      const { offsetX, offsetY } = d_ev;
      let x = clientX - offsetX;
      let y = clientY - offsetY;
      return { x, y };
    }
    
    // 获取目标元素
    function getTarget(e) {
      let target = e.target;
      if (target.className.includes(MOVE_DOM_CLASS)) return target;
      return "";
    }
    
    // 是否在可视区域
    function inVisibleArea(x, y) {
      console.log("X轴", w_left, x, w_right, w_left < x && x < w_right);
      console.log("Y轴", w_top, y, w_bottom, w_top < y && y < w_bottom);
      return w_left < x && x < w_right && w_top < y && y < w_bottom;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    **

  • 相关阅读:
    画CMB天图使用Planck配色方案
    C++继承探究
    从四分钟到两秒——谈谈客户端性能优化的一些最佳实践
    DataBinding 进阶用法
    大数据Hadoop入门教程 | (二)Linux
    HTMl案例二:注册页面
    [附源码]java毕业设计医院档案管理系统
    react动态加载组件
    【软件测试】理论知识基础第二章
    【AI】创建自己的基于会话的自定义模型的ChatGPT
  • 原文地址:https://blog.csdn.net/kxmdjqq/article/details/132993537