index.wxml
<view class="drag" bindtouchstart="fnS" bindtouchmove="fnM" style="top: {{y}}px; left: {{x}}px;"></view>
index.wxss
/**index.wxss**/
.drag{
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
}
index.wxjs
const app = getApp()
Page({
data:{
disx:0,
disy:0,
y:0,
x:0
},
fnS(e){
this.setData({
disx:e.changedTouches[0].pageY-this.data.x,
disy:e.changedTouches[0].pageX-this.data.y
})
},
fnM(e){
this.setData({
x:e.changedTouches[0].pageX-this.data.disx,
y:e.changedTouches[0].pageY-this.data.disx
})
}
})