DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

移动盒子

实现一个鼠标按下去拖住元素移动的效果.保证移动元素盒子不能超出父盒子

Javascript
 <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .parent {
        position: relative;
        background: rgb(239, 196, 196);
        margin: 100px;
        padding: 200px;
        outline: 1px solid green;
        overflow: hidden;
      }
      .box {
        position: absolute;
        left: 50px;
        top: 50px;
        /* margin: 10px; */
        padding: 10px;
        width: 100px;
        height: 100px;
        background-color: rgb(0, 0, 0);
        border: 5px solid red;
      }
    </style>
  </head>
  <body>
    <div id="parent" class="parent">
      <div id="box" class="box"></div>
    </div>

    <script>
      var parent = document.getElementById('parent');
      var box = document.getElementById('box');
      var isDragging = false;
      var mouseOffset = { x: 0, y: 0 };
      var minX = 0;
      var maxX = parent.clientWidth - box.offsetWidth;
      var minY = 0;
      var maxY = parent.clientHeight - box.offsetHeight;

      box.addEventListener('mousedown', function (event) {
        isDragging = true;

        // 计算鼠标与元素左上角的偏移量
        mouseOffset.x = event.clientX - box.offsetLeft;
        mouseOffset.y = event.clientY - box.offsetTop;
        console.log(mouseOffset.x, mouseOffset.y);
      });

      document.addEventListener('mousemove', function (event) {
    if (isDragging) {
            // 计算元素的新位置
            var newX = event.clientX - mouseOffset.x;
            var newY = event.clientY - mouseOffset.y;

            // 限制移动范围
            newX = Math.max(minX, Math.min(newX, maxX));
            newY = Math.max(minY, Math.min(newY, maxY));

            // 更新元素的位置
            box.style.left = newX + 'px';
            box.style.top = newY + 'px';
          }
      });

      document.addEventListener('mouseup', function () {
        isDragging = false;
      });
    </script>
  </body>
</html>

Last Updated:
Contributors: rosendo
Prev
状态管理
Next
移动端开发常用snippets