js实现弹窗拖拽功能

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			a {
				text-decoration: none;
			}
			html,
			body {
				width: 100%;
				height: 100%;
			}
			.box {
				width: 100%;
				top: 0;
				left: 0;
				bottom: 0;
				right: 0;
				height: 100%;
				background: rgba(0, 0, 0, 0.4);
				position: fixed;
				display: none;
			}

			.box1 {
				width: 60%;
				height: 60%;
				margin: 50 auto;
				background: #fff;
				position: absolute;
				left: 300px;
				top: 100px;
				border-radius: 2px;
				box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
			}
			.list {
				height: 50px;
				background-color: #4491e1;
				font-size: 18px;
				line-height: 50px;
				position: relative;
				padding: 0 20px;
				display: flex;
				justify-content: space-between;
				color: #fff;
			}
			#ipt {
				color: #fff;
			}
		</style>
	</head>
	<body>
		<input type="button" value="点击弹出窗口" id="btn" />
		<div class="box">
			<div class="box1">
				<div class="list">
					<div>标题</div>
					<a href="javascript:;" id="ipt">x</a>
				</div>
				<div>放内容</div>
			</div>
		</div>
		<script type="text/javascript">
			//点击按钮弹出窗口
			let btn = document.querySelector('#btn')
			let box = document.querySelector('.box')
			btn.onclick = function() {
				// btn.style.display = 'none'	
				box.style.display = 'block'
			}
			//点击按钮关闭窗口
			let ipt = document.querySelector('#ipt')
			ipt.onclick = function() {
				box.style.display = 'none'
				btn.style.display = 'block'
			}
			//拖拽
			let list = document.querySelector('.list')
			let box1 = document.querySelector('.box1')
			list.onmousedown = function(e) {
				console.log(e);
				console.log(box1.offsetLeft);
				console.log(box1.offsetTop);
				let offsetX = e.pageX - box1.offsetLeft
				let offsetY = e.pageY - box1.offsetTop

				document.onmousemove = function(d) {
					let left = d.pageX - offsetX;
					let top = d.pageY - offsetY;

					box1.style.left = left + 'px'
					box1.style.top = top + 'px'
				}
				//鼠标抬起,清除鼠标移动事件			
				document.onmouseup = function() {
					document.onmousemove = null;
				}
			}
		</script>
	</body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Vue中,可以通过自定义指令实现弹窗拖拽和拉伸功能。 首先,对于拖拽功能,可以创建一个自定义指令`v-draggable`。在该指令的绑定元素上设置`mousedown`事件监听器,当鼠标按下时触发拖拽操作。在`mousemove`和`mouseup`事件监听器中处理鼠标移动和释放的逻辑。具体代码如下: ```javascript Vue.directive('draggable', { bind(el) { el.style.position = 'absolute'; el.addEventListener('mousedown', startDrag); function startDrag(e) { const startX = e.clientX; const startY = e.clientY; const initialX = el.offsetLeft; const initialY = el.offsetTop; document.addEventListener('mousemove', dragElement); document.addEventListener('mouseup', releaseElement); function dragElement(e) { el.style.left = `${initialX + (e.clientX - startX)}px`; el.style.top = `${initialY + (e.clientY - startY)}px`; } function releaseElement() { document.removeEventListener('mousemove', dragElement); document.removeEventListener('mouseup', releaseElement); } } } }); ``` 接下来,对于拉伸功能,可以创建一个自定义指令`v-resizable`。在该指令的绑定元素上设置`mousedown`事件监听器,当鼠标按下时触发拉伸操作。在`mousemove`和`mouseup`事件监听器中处理鼠标移动和释放的逻辑。具体代码如下: ```javascript Vue.directive('resizable', { bind(el) { el.style.position = 'absolute'; el.style.resize = 'both'; el.addEventListener('mousedown', startResize); function startResize(e) { const startX = e.clientX; const startY = e.clientY; const initialWidth = el.offsetWidth; const initialHeight = el.offsetHeight; document.addEventListener('mousemove', resizeElement); document.addEventListener('mouseup', releaseElement); function resizeElement(e) { el.style.width = `${initialWidth + (e.clientX - startX)}px`; el.style.height = `${initialHeight + (e.clientY - startY)}px`; } function releaseElement() { document.removeEventListener('mousemove', resizeElement); document.removeEventListener('mouseup', releaseElement); } } } }); ``` 最后,在需要拖拽和拉伸的弹窗组件上使用`v-draggable`和`v-resizable`指令即可实现相应功能。例如: ```html <template> <div class="modal" v-draggable v-resizable> <!-- 弹窗内容 --> </div> </template> ``` 通过上述自定义指令的方式,我们可以在Vue中实现弹窗拖拽和拉伸功能,提升用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈琦鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值