- 用 onmousedown onmousemove onmouseup 获取鼠标位置
- 将 div 弄成绝对定位,将鼠标的位置赋值到 div 的 top 和 left 上
- 用一个 boolean 值来做开关
- clone 或者下载压缩包之后
- 使用 http-server -c-1 命令行运行
- onmousedown:在当前元素上鼠标按下瞬间会触发mousedown事件;
- onmousemove:当用户在当前元素上移动鼠标时会触发mousemove事件;
- onmouseup:当用户在当前元素上放开鼠标某个按键时会触发mouseup事件;
body {
box-sizing: border-box;
height: 100vh;
margin: 0;
}
- 操作 classList 数组,如
div.classList = ["demo"]
- 赋值 className ,
div.className = "demo"