拖拽功能实现vue
1.按钮
<el-button
@mousedown="down" @touchstart="down" @mousemove="move"
@touchmove="move" @mouseup="end" @touchend="end" @touchcancel="end" >
</el-button>
2.检测平台,可以不检测
isMobile() {
if (window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
return false; // 移动端
} else {
return true; // PC端
}
}
3.js代码
down(e) {
this.flags = true
let styleLoc = this.moveBtn.getBoundingClientRect()
this.ispc = this.browserRedirect()
let clientX = this.ispc === 'pc' ? e.clientX : e.targetTouches[0].clientX
let clientY = this.ispc === 'pc' ? e.clientY : e.targetTouches[0].clientY
this.disMove = [
styleLoc.left - clientX,
styleLoc.top - clientY,
]
},
move(e) {
if (this.flags) {
let clientX = this.ispc === 'pc' ? e.clientX : e.targetTouches[0].clientX
let clientY = this.ispc === 'pc' ? e.clientY : e.targetTouches[0].clientY
this.moveBtn.style.left = clientX + this.disMove[0] + 'px'
this.moveBtn.style.top = clientY + this.disMove[1] + 'px'
}
},
//鼠标释放时候的函数
end() {
this.flags = false
document.addEventListener('touchmove', this.handler, {
passive: false,
})
},
handler(e) {
if (this.flags) {
event.preventDefault()
} else {
return true
}
},
问题
mousemove并非逐帧监听,而是大概10-20毫秒触发一次,所以pc端用户操作过快会脱离
本文来自博客园,作者:流云君,转载请注明原文链接:https://www.cnblogs.com/yun10011/p/16520394.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)