右键移动盒子且可拖拽


export function move(obj) {
  obj.oncontextmenu = function (e) {
    e.preventDefault();
  };
  var direction = 0;
  var leftRange;
  var rightRange;
  obj.onmousemove = (e) => {
    var mouseOverX = e.clientX;
    var BoxLeft = obj.offsetLeft;
    var BoxWidth = obj.offsetWidth;
    leftRange = mouseOverX - BoxLeft > -30 && mouseOverX - BoxLeft < 30;
    rightRange =
      mouseOverX - (BoxLeft + BoxWidth) > -30 &&
      mouseOverX - (BoxLeft + BoxWidth) < 30;

    if (leftRange || rightRange) {
      obj.style.cursor = "e-resize";
    } else {
      obj.style.cursor = "default";
    }
  };

  obj.onmousedown = function (ev) {
    ev.stopPropagation();
    let keySide = ev.button;
    if (keySide == 0) return;
    var ev = window.even || ev;
    // ev.clientX:可视窗口 鼠标到body的距离,box.offsetLeft:box的边框到body的距离
    var x = ev.clientX - obj.offsetLeft; //box到鼠标点击位置的水平距离
    var y = ev.clientY - obj.offsetTop;

    var mouseDownX = ev.clientX; //获取鼠标按下时距离浏览器左侧的距离
    var clickBoxLeft = obj.offsetLeft; //获取当前元素距离左侧的距离
    var clickBoxWeight = obj.offsetWidth; //获取当前元素的宽度

    if (mouseDownX < clickBoxLeft + 30) {
      direction = "left";
    } else if (mouseDownX > clickBoxLeft + clickBoxWeight - 30) {
      direction = "right";
    } else {
      direction = 0;
    }

    document.onmousemove = function (ev) {
      var ev = window.even || ev;
      var minleft = ev.clientX - x;
      var mintop = ev.clientY - y;
      if (minleft < 0) {
        // 左边边界
        minleft = 0;
      }
      if (minleft > document.documentElement.clientWidth - obj.offsetWidth) {
        // 右边边界
        minleft = document.documentElement.clientWidth - obj.offsetWidth;
      }
      if (mintop < 0) {
        // 上边界
        mintop = 0;
      }
      if (mintop > document.documentElement.clientHeight - obj.offsetHeight) {
        // 下边界
        mintop = document.documentElement.clientHeight - obj.offsetHeight;
      }
      if (keySide == 2 && direction == 0) {
        obj.style.left = minleft + "px";
        obj.style.top = mintop + "px";
      } else if (keySide == 2 && direction != 0) {
        var xx = ev.clientX;
        if (direction === "left") {
          obj.style.width = clickBoxWeight + mouseDownX - xx + "px";
          if (obj.offsetWidth <= 220) return;
          obj.style.left = xx + "px";
        } else if (direction === "right" && obj.offsetWidth >= 220) {
          obj.style.width = clickBoxWeight + xx - mouseDownX + "px";
        }
      }
    };
    document.onmouseup = function (e) {
      direction = 0;
      console.log("注销事件");
      // 停止移动事件
      document.onmousemove = null;
      document.onmouseup = null;
      e.preventDefault();
    };
    return false;
  };
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值