document.onmouseup = function (event) { document.onmousemove = null; if (event.pageX < 10) { document.getElementById('floatingButton').style.left = 10 + 'px'; document.onmouseup = null; } else if (event.pageX > w) { document.getElementById('floatingButton').style.left = w - 40 + 'px'; } if (event.pageY < 50) { document.getElementById('floatingButton').style.top = 50 + 'px'; document.onmouseup = null; } else if (event.pageY > h) { document.getElementById('floatingButton').style.top = h - 100 + 'px'; } };这段代码干嘛的
时间: 2024-04-03 15:35:57 浏览: 11
这段代码实现了浮动按钮的拖拽结束后的处理,包括停止拖拽、调整按钮位置以及重置鼠标事件监听器等。
具体来说,这段代码执行了以下操作:
1. 给整个文档对象添加了一个 `mouseup` 事件监听器。
2. 当鼠标松开时,将文档对象上的 `mousemove` 事件监听器设置为 `null`,表示停止拖拽操作。
3. 判断按钮是否超出文档边界,如果超出了左边界,则将按钮的左边距设置为 `10`,如果超出了右边界,则将按钮的左边距设置为 `w - 40`;如果超出了上边界,则将按钮的上边距设置为 `50`,如果超出了下边界,则将按钮的上边距设置为 `h - 100`。
4. 根据按钮的新位置,调整 `document.onmouseup` 的事件监听器,如果按钮超出了左边界或上边界,则将 `document.onmouseup` 的事件监听器设置为 `null`,表示停止拖拽操作。
总之,这段代码实现了一个简单的浮动按钮的拖拽结束后的处理,包括停止拖拽、调整按钮位置以及重置鼠标事件监听器等。
相关问题
请根据代码片段仿写实现div左下角拖拽移动用具体代码实现,import Vue from 'vue' Vue.directive('dialogZoomOut', { bind(el, binding, vnode, oldVnode) { let minWidth = 400;let minHeight = 300;let isFullScreen = false; let nowWidth = 0;let nowHight = 0;let nowMarginTop = 0;const dialogHeaderEl = el.querySelector('.el-dialog__header');const dragDom = el.querySelector('.el-dialog');dragDom.style.overflow = "auto";dialogHeaderEl.onselectstart = new Function("return false");dialogHeaderEl.style.cursor = 'move';const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);let moveDown = (e) => {const disX = e.clientX - dialogHeaderEl.offsetLeft;const disY = e.clientY - dialogHeaderEl.offsetTop;let styL, styT;if (sty.left.includes('%')) {styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);} else {styL = +sty.left.replace(/px/g, '');styT = +sty.top.replace(/px/g, '');};document.onmousemove = function (e) {const l = e.clientX - disX;const t = e.clientY - disY;dragDom.style.left = `${l + styL}px`;dragDom.style.top = `${t + styT}px`;};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}dialogHeaderEl.onmousedown = moveDown;dialogHeaderEl.ondblclick = (e) => {if (isFullScreen == false) {nowHight = dragDom.clientHeight;nowWidth = dragDom.clientWidth;nowMarginTop = dragDom.style.marginTop;dragDom.style.left = 0;dragDom.style.top = 0;dragDom.style.height = "100VH";dragDom.style.width = "100VW";dragDom.style.marginTop = 0;isFullScreen = true;dialogHeaderEl.style.cursor = 'initial';dialogHeaderEl.onmousedown = null;} else {dragDom.style.height = "auto";dragDom.style.width = nowWidth + 'px';dragDom.style.marginTop = nowMarginTop;isFullScreen = false;dialogHeaderEl.style.cursor = 'move';dialogHeaderEl.onmousedown = moveDown;}}let resizeEl = document.createElement("div");dragDom.appendChild(resizeEl);resizeEl.style.cursor = 'se-resize';resizeEl.style.position = 'absolute';resizeEl.style.height = '10px';resizeEl.style.width = '10px';resizeEl.style.right = '0px';resizeEl.style.bottom = '0px';resizeEl.style.zIndex = '99';resizeEl.onmousedown = (e) => {let clientX = e.clientX;let disX = e.clientX - resizeEl.offsetLeft;let disY = e.clientY - resizeEl.offsetTop;document.onmousemove = function (e) {e.preventDefault(); let x = e.clientX - disX + (e.clientX - clientX);let y = e.clientY - disY;dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}}})
以下是仿写的代码实现:
```html
<template>
<div class="dialog-wrapper" v-dialog-zoom-out>
<div class="el-dialog">
<div class="el-dialog__header">Dialog Title</div>
<div class="el-dialog__body">Dialog Content</div>
<div class="el-dialog__footer">
<button @click="$emit('close')">Close</button>
</div>
<div class="el-dialog__resize"></div>
</div>
</div>
</template>
<script>
export default {
name: "Dialog",
};
</script>
<style>
.dialog-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.el-dialog {
position: absolute;
top: 0;
left: 0;
overflow: auto;
max-height: 80vh;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.el-dialog__header {
padding: 16px;
background-color: #f5f7fa;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
cursor: move;
}
.el-dialog__body {
padding: 16px;
}
.el-dialog__footer {
display: flex;
justify-content: flex-end;
padding: 16px;
background-color: #f5f7fa;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
.el-dialog__footer button {
padding: 8px 16px;
background-color: #fff;
border: 1px solid #c1c1c1;
border-radius: 4px;
cursor: pointer;
}
.el-dialog__resize {
position: absolute;
bottom: 0;
right: 0;
width: 10px;
height: 10px;
cursor: se-resize;
background-color: #c1c1c1;
}
</style>
```
```javascript
import Vue from "vue";
Vue.directive("dialogZoomOut", {
bind(el, binding, vnode, oldVnode) {
let minWidth = 400;
let minHeight = 300;
let isFullScreen = false;
let nowWidth = 0;
let nowHight = 0;
let nowMarginTop = 0;
const dialogHeaderEl = el.querySelector(".el-dialog__header");
const dragDom = el.querySelector(".el-dialog");
dragDom.style.overflow = "auto";
dialogHeaderEl.onselectstart = new Function("return false");
dialogHeaderEl.style.cursor = "move";
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
let moveDown = (e) => {
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.offsetTop;
let styL, styT;
if (sty.left.includes("%")) {
styL = +document.body.clientWidth * (+sty.left.replace(/%/g, "") / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/%/g, "") / 100);
} else {
styL = +sty.left.replace(/px/g, "");
styT = +sty.top.replace(/px/g, "");
}
document.onmousemove = function (e) {
const l = e.clientX - disX;
const t = e.clientY - disY;
dragDom.style.left = `${l + styL}px`;
dragDom.style.top = `${t + styT}px`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
dialogHeaderEl.onmousedown = moveDown;
dialogHeaderEl.ondblclick = (e) => {
if (isFullScreen == false) {
nowHight = dragDom.clientHeight;
nowWidth = dragDom.clientWidth;
nowMarginTop = dragDom.style.marginTop;
dragDom.style.left = 0;
dragDom.style.top = 0;
dragDom.style.height = "100VH";
dragDom.style.width = "100VW";
dragDom.style.marginTop = 0;
isFullScreen = true;
dialogHeaderEl.style.cursor = "initial";
dialogHeaderEl.onmousedown = null;
} else {
dragDom.style.height = "auto";
dragDom.style.width = nowWidth + "px";
dragDom.style.marginTop = nowMarginTop;
isFullScreen = false;
dialogHeaderEl.style.cursor = "move";
dialogHeaderEl.onmousedown = moveDown;
}
};
let resizeEl = document.createElement("div");
dragDom.appendChild(resizeEl);
resizeEl.style.cursor = "se-resize";
resizeEl.style.position = "absolute";
resizeEl.style.height = "10px";
resizeEl.style.width = "10px";
resizeEl.style.right = "0px";
resizeEl.style.bottom = "0px";
resizeEl.style.zIndex = "99";
resizeEl.onmousedown = (e) => {
let clientX = e.clientX;
let disX = e.clientX - resizeEl.offsetLeft;
let disY = e.clientY - resizeEl.offsetTop;
document.onmousemove = function (e) {
e.preventDefault();
let x = e.clientX - disX + (e.clientX - clientX);
let y = e.clientY - disY;
dragDom.style.width = x > minWidth ? `${x}px` : minWidth + "px";
dragDom.style.height = y > minHeight ? `${y}px` : minHeight + "px";
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
},
});
```
上述代码实现了一个类似于Element UI中的Dialog组件的拖拽和缩放功能。在使用时,只需要将 `v-dialog-zoom-out` 指令绑定到Dialog组件的容器元素上,即可实现拖拽和缩放功能。
var box = document.querySelector('.box') var box1 = document.querySelector('.box1') box.onmousedown = function (e) { var e = e || window.event var e = event.offsetX var d = event.offsetY console.log(e) console.log(d) } box1.onmousemove = function (e) { var e = e || window.event var x =event.offsetX var y =event.offsetY console.log(x) console.log(y) box.style.left = x + 'px' box.style.top = y + 'px' } box1.onmouseup = function () { console.log('我要取消了移动事件') box1.onmousemove = null } 这段代码的错误
这段代码的错误在于以下几点:
1. 在 box.onmousedown 事件处理函数中,将 event 对象的 offsetX 属性赋值给了变量 e,导致后面的代码中 e 的值被覆盖,无法得到正确的鼠标点击位置。
2. 在 box1.onmousemove 事件处理函数中,获取鼠标移动位置时,将 event 对象的 offsetX 和 offsetY 属性分别赋值给了变量 x 和 y,但是变量 x 和 y 在后面的代码中没有被使用,导致无法实现 box 的移动。
3. 在 box1.onmouseup 事件处理函数中,尝试取消 box1.onmousemove 事件处理函数,但是这样是无法生效的,因为 box1.onmousemove 赋值为 null 并不会取消事件处理函数。
正确的代码应该是:
```javascript
var box = document.querySelector('.box');
var box1 = document.querySelector('.box1');
var offsetX, offsetY;
box.onmousedown = function (e) {
var e = e || window.event;
offsetX = e.offsetX;
offsetY = e.offsetY;
console.log(offsetX);
console.log(offsetY);
}
box1.onmousemove = function (e) {
var e = e || window.event;
var x = e.clientX - offsetX;
var y = e.clientY - offsetY;
console.log(x);
console.log(y);
box.style.left = x + 'px';
box.style.top = y + 'px';
}
box1.onmouseup = function () {
console.log('我要取消了移动事件');
box1.onmousemove = null;
}
```
在上述代码中,我们将鼠标点击位置的 offsetX 和 offsetY 值保存在变量中,在 box1.onmousemove 事件处理函数中,根据鼠标移动的位置计算出 box 的新位置,并将其赋值给 box 的 style.left 和 style.top 属性,实现了盒子的移动。在 box1.onmouseup 事件处理函数中,我们将 box1.onmousemove 赋值为 null,实现了取消 box1.onmousemove 事件处理函数的效果。