JS弹出框实现拖拽移动和修改宽高的功能

这里写自定义目录标题

拖拽移动

通过定义dom节点的onmousedown函数,可以实现对拖拽事件的处理:

创建div:

const popup = document.createElement('div');
popup.style.position = 'absolute';
popup.style.top = '50px';
popup.style.left = '50px';
popup.style.zIndex = 1;
popup.style.cursor = "move";
popup.style.width = '1300px';
popup.style.paddingTop = '30px';
popup.style.height = '420px';
popup.style.backgroundColor = 'white';
popup.style.boxSizing = 'border-box';
popup.style.borderRadius = '5px';
popup.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.1)';

添加事件处理:

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  elmnt.onmousedown = dragMouseDown;

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

dragElement(popup);

拖拽修改宽高

创建一个按钮,添加该按钮的onmousedown函数,可以实现对拖拽事件的处理:

const resizeButton = document.createElement('button');
resizeButton.innerHTML = '↘';
resizeButton.style.position = 'absolute';
resizeButton.style.right = '0';
resizeButton.style.bottom = '0';
resizeButton.style.width = '20px';
resizeButton.style.height = '20px';
resizeButton.style.backgroundColor = 'white';
resizeButton.style.color = 'black';
resizeButton.style.fontSize = '16px';
resizeButton.style.border = '1px solid black';
// resizeButton.style.borderRadius = '50%';
resizeButton.style.boxShadow = '1px 1px 5px rgba(0, 0, 0, 0.2)';
resizeButton.style.cursor = 'nwse-resize';
popup.appendChild(resizeButton);

添加onmousedown的事件处理:

function resizeElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  resizeButton.onmousedown = resizeMouseDown;

  function resizeMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    e.stopPropagation();
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeResizeElement;
    document.onmousemove = elementResize;
  }

  function elementResize(e) {
    e = e || window.event;
    e.preventDefault();
    pos1 = e.clientX - pos3;
    pos2 = e.clientY - pos4;
    pos3 = e.clientX;
    pos4 = e.clientY;
    elmnt.style.width = (elmnt.offsetWidth + pos1) + "px";
    elmnt.style.height = (elmnt.offsetHeight + pos2) + "px";
  }

  function closeResizeElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

resizeElement(popup);

完整代码

以下是一个使用js实现的关于弹出框实现拖拽移动和修改宽高的代码:

<!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>
</head>

<body>

</body>
<script>

const popupContainer = document.createElement('div');
popupContainer.style.display = 'none';
popupContainer.style.position = 'fixed';
popupContainer.style.left = '0';
popupContainer.style.top = '0';
popupContainer.style.width = '100%';
popupContainer.style.height = '100%';
popupContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
popupContainer.style.zIndex = '1000';

const popup = document.createElement('div');
popup.style.position = 'absolute';
popup.style.top = '50px';
popup.style.left = '50px';
popup.style.zIndex = 1;
popup.style.cursor = "move";
popup.style.width = '1300px';
popup.style.paddingTop = '30px';
popup.style.height = '420px';
popup.style.backgroundColor = 'white';
popup.style.boxSizing = 'border-box';
popup.style.borderRadius = '5px';
popup.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.1)';

const iframe = document.createElement('iframe');
iframe.src = '';
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = '0';

const closeButton = document.createElement('button');
closeButton.innerHTML = 'X';
closeButton.style.position = 'absolute';
closeButton.style.right = '10px';
closeButton.style.top = '5px';
closeButton.style.zIndex = '10';
closeButton.addEventListener('click', function () {
  document.body.removeChild(popupContainer);
});

popup.appendChild(closeButton);

const resizeButton = document.createElement('button');
resizeButton.innerHTML = '↘';
resizeButton.style.position = 'absolute';
resizeButton.style.right = '0';
resizeButton.style.bottom = '0';
resizeButton.style.width = '20px';
resizeButton.style.height = '20px';
resizeButton.style.backgroundColor = 'white';
resizeButton.style.color = 'black';
resizeButton.style.fontSize = '16px';
resizeButton.style.border = '1px solid black';
// resizeButton.style.borderRadius = '50%';
resizeButton.style.boxShadow = '1px 1px 5px rgba(0, 0, 0, 0.2)';
resizeButton.style.cursor = 'nwse-resize';
popup.appendChild(resizeButton);
popup.appendChild(iframe);
popupContainer.appendChild(popup);
document.body.appendChild(popupContainer);

popupContainer.style.display = 'block';

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  elmnt.onmousedown = dragMouseDown;

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

dragElement(popup);

function resizeElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  resizeButton.onmousedown = resizeMouseDown;

  function resizeMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    e.stopPropagation();
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeResizeElement;
    document.onmousemove = elementResize;
  }

  function elementResize(e) {
    e = e || window.event;
    e.preventDefault();
    pos1 = e.clientX - pos3;
    pos2 = e.clientY - pos4;
    pos3 = e.clientX;
    pos4 = e.clientY;
    elmnt.style.width = (elmnt.offsetWidth + pos1) + "px";
    elmnt.style.height = (elmnt.offsetHeight + pos2) + "px";
  }

  function closeResizeElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

resizeElement(popup);

</script>

</html>

直接复制代码,新建一个html文件即可运行查看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JS实现留言板的过程如下: 首先,我们需要在HTML中创建一个留言板的容器,可以是一个div元素,然后在CSS中设置其样式,包括显示位置、大小、背景颜色等。 接下来,在JS中,我们可以通过事件监听,例如点击事件,来触发留言功能。可以通过给留言板容器添加一个点击事件监听器,当点击事件发生时,触发留言的逻辑。 留言的逻辑可以通过JS动态创建一个留言框的元素,可以是一个文本输入框加上一个发送按钮。创建元素的过程可以使用DOM操作,通过createElement方法来创建元素节点,然后通过appendChild方法将其添加到留言板容器中。 接着,我们可以通过事件监听器来实现移动蒙版的功能。可以通过给蒙版元素添加一个鼠标按下事件监听器,在鼠标按下时获取鼠标的位置坐标。然后在鼠标移动事件监听器中,计算鼠标位置的偏移量,并将蒙版元素的位置跟随鼠标移动进行相应的更新,可以通过设置蒙版元素的定位属性来实现。 最后,我们可以在JS中添加一层蒙版元素,用于遮挡页面的其他内容,以便实现点击留言板时,将焦点限制在留言板内。可以通过给蒙版元素设置一些样式,例如半透明的背景颜色,来实现这一效果。 综上所述,就是用JS实现留言板的基本思路和步骤。通过事件监听和DOM操作,我们可以实现点击留言功能移动蒙版等功能,以提升留言板的交互性和用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值