可拖拽div

html:

<div class="container">拖动</div>

css:

.container {
	padding: 0.2rem;
	background: #409EFF;
	display: inline-block;
	position: absolute;
	left: 100px;
	top: 200px;
	height: 100px;
	width: 200px;
	text-align: center;
	color: #fff;
}

一、简单拖拽

1.效果

2.JS代码

function drag(e) {
	const oldStyle = window.getComputedStyle(container),
	left = parseFloat(oldStyle.left),
	top = parseFloat(oldStyle.top)
	container.style.left = `${left + e.movementX}px`
	container.style.top = `${top + e.movementY}px`
	document.addEventListener('mouseup', function() {
		document.removeEventListener('mousemove', drag)
		document.removeEventListener('mouseup', arguments.callee)
	})
}
const container = document.querySelector('.container')
container.addEventListener('mousedown', () => {
	document.addEventListener('mousemove', drag)
})

二、边界限制拖拽

解决当容器拖拽超出边界时会产生滚动条问题。

1.效果

2.JS代码

function drag(e) {
	const padding = 10,
	oldStyle = window.getComputedStyle(container),
	left = parseFloat(oldStyle.left),
	top = parseFloat(oldStyle.top),
	width = container.clientWidth,
	height = container.clientHeight,
	temLeft = left + e.movementX,
	temTop = top + e.movementY,
	clientWidth = document.documentElement.clientWidth,
	clientHeight = document.documentElement.clientHeight,
	maxLeft = clientWidth - width - padding,
	maxTop = clientHeight - height - padding,
	newLeft = temLeft < padding ? padding : temLeft > maxLeft ? maxLeft : temLeft, 
	newTop = temTop < padding ? padding : temTop > maxTop ? maxTop : temTop
	container.style.left = `${newLeft}px`
	container.style.top = `${newTop}px`
	container.setAttribute('move', '')
	document.addEventListener('mouseup', function() {
		container.removeAttribute('move')
		document.removeEventListener('mousemove', drag)
		document.removeEventListener('mouseup', arguments.callee)
	})
}
const container = document.querySelector('.container')
container.addEventListener('mousedown', () => {
	document.addEventListener('mousemove', drag)
})

三、吸附拖拽

松开鼠标后自动根据最近距离吸附左右侧

1.效果

 2.代码

css:

.container {
	padding: 0.2rem;
	background: #409EFF;
	display: inline-block;
	position: absolute;
	left: 100px;
	top: 200px;
	height: 100px;
	width: 200px;
	text-align: center;
	color: #fff;
}
.container:hover {
	cursor: move;
}
.container[move] {
	border-radius: 10px;
	background-color: #67C23A;
}
.container[up] {
	transition: left 1s;
}

 js:

function drag(e) {
	const padding = 10,
	oldStyle = window.getComputedStyle(container),
	left = parseFloat(oldStyle.left),
	top = parseFloat(oldStyle.top),
	width = container.clientWidth,
	height = container.clientHeight,
	temLeft = left + e.movementX,
	temTop = top + e.movementY,
	clientWidth = document.documentElement.clientWidth,
	clientHeight = document.documentElement.clientHeight,
	maxLeft = clientWidth - width - padding,
	maxTop = clientHeight - height - padding,
	newLeft = temLeft < padding ? padding : temLeft > maxLeft ? maxLeft : temLeft, 
	newTop = temTop < padding ? padding : temTop > maxTop ? maxTop : temTop
	container.style.left = `${newLeft}px`
	container.style.top = `${newTop}px`
	container.setAttribute('move', '')
	document.addEventListener('mouseup', function() {
		container.setAttribute('up', '')
		container.style.left = `${newLeft < (clientWidth - width) / 2 ? padding : maxLeft}px`
		container.removeAttribute('move')
		document.removeEventListener('mousemove', drag)
		document.removeEventListener('mouseup', arguments.callee)
	})
}
const container = document.querySelector('.container')
container.addEventListener('mousedown', () => {
	container.removeAttribute('up')
	document.addEventListener('mousemove', drag)
})

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 可以通过绑定事件的方式来实现可拖动的 DIV。具体实现方式如下: ```html <template> <div> <div class="drag-box" draggable="true" @dragstart="dragStart" @dragend="dragEnd" :style="{top: position.top + 'px', left: position.left + 'px'}" > 可拖动的 DIV </div> </div> </template> <script> export default { data() { return { position: { x: 0, y: 0, top: 0, left: 0, dragging: false } } }, methods: { dragStart(event) { // 获取鼠标在元素内的偏移量 const offsetX = event.clientX - event.target.offsetLeft; const offsetY = event.clientY - event.target.offsetTop; this.position.x = offsetX; this.position.y = offsetY; this.position.dragging = true; }, dragEnd(event) { this.position.dragging = false; } }, mounted() { // 监听鼠标移动事件 document.addEventListener('mousemove', this.handleMouseMove); }, beforeDestroy() { // 移除鼠标移动事件监听 document.removeEventListener('mousemove', this.handleMouseMove); }, methods: { handleMouseMove(event) { if (this.position.dragging) { // 计算拖动后的位置 this.position.left = event.clientX - this.position.x; this.position.top = event.clientY - this.position.y; } } } } </script> <style> .drag-box { position: absolute; width: 100px; height: 100px; background-color: #f00; color: #fff; text-align: center; line-height: 100px; } </style> ``` 上面的代码中,我们创建了一个可拖动的 DIV,它的 `draggable` 属性被设置为 `true`。在 `dragstart` 事件中,我们获取了鼠标在元素内的偏移量,并将其保存到 `position` 对象中。在 `dragend` 事件中,我们将 `dragging` 属性设置为 `false`。 在 `mounted` 钩子函数中,我们监听了鼠标移动事件,并在 `beforeDestroy` 钩子函数中移除了事件监听。在 `handleMouseMove` 方法中,我们计算了拖动后的位置,并将其保存到 `position` 对象中。在模板中,我们使用 `:style` 绑定了位置信息,使得元素可以随鼠标拖动而移动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值