手机端拖动事件是touchstart touchmove touchend,对用pc端的mousedown mousemove mouseup事件,下面实现了一个兼容手机端和pc端的vue拖放代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />
<style>
.xuanfu {
height: 4.5rem;
width: 4.5rem;
z-index: 990;
position: fixed;
top: 4.2rem;
right: 3.2rem;
border-radius: 0.8rem;
background-color: rgba(0, 0, 0, 0.55);
}
.yuanqiu {
height: 2.7rem;
width: 2.7rem;
border: 0.3rem solid rgba(140, 136, 136, 0.5);
margin: 0.65rem auto;
color: #000000;
font-size: 1.6rem;
line-height: 2.7rem;
text-align: center;
border-radius: 100%;
background-color: #ffffff;
}
</style>
</head>
<body>
<div id="app">
<div id="customer">
<!-- Suspended HTML -->
<div ref="moveDiv" class="xuanfu" id="div2" @mousedown="down" @touchstart="down" @mousemove="move" @touchmove="move" @mouseup="end" @touchend="end">
<div class="yuanqiu">
</div>
</div>
</div>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue.2.2.min.js"></script>
<script>
new Vue({
el: '#app',
data:function() {
return {
flags: false,
position: {
x: 0,
y: 0
},
nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
}
},
methods: {
// Mobile drag
down(){
this.flags = true;
var touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
this.position.x = touch.clientX;
this.position.y = touch.clientY;
this.dx = this.$refs.moveDiv.offsetLeft;
this.dy = this.$refs.moveDiv.offsetTop;
},
move(){
if(this.flags){
var touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
this.nx = touch.clientX - this.position.x;
this.ny = touch.clientY - this.position.y;
this.xPum = this.dx+this.nx;
this.yPum = this.dy+this.ny;
this.$refs.moveDiv.style.left = this.xPum+"px";
this.$refs.moveDiv.style.top = this.yPum +"px";
//Prevent page from sliding default events
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
}
},
//Function when mouse is released
end(){
this.flags = false;
},
}
})
</script>
</body>
</html>
网友回复
为啥所有的照片分辨率提升工具都会修改照片上的图案细节?
js如何在浏览器中将webm视频的声音分离为单独音频?
微信小程序如何播放第三方域名url的mp4视频?
ai多模态大模型能实时识别视频中的手语为文字吗?
如何远程调试别人的chrome浏览器获取调试信息?
为啥js打开新网页window.open设置窗口宽高无效?
浏览器中js的navigator.mediaDevices.getDisplayMedia屏幕录像无法录制SpeechSynthesisUtterance产生的说话声音?
js中mediaRecorder如何录制window.speechSynthesis声音音频并下载?
python如何直接获取抖音短视频的音频文件url?
js在浏览器中如何使用MediaStream与MediaRecorder实现声音音频多轨道混流?