<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/browser.js"></script>
<script src="./js/react.js"></script>
<script src="./js/react-dom.js"></script>
<style>
.ball{
position: relative;
top: 0;
left: 0;
width: 100px;
height: 100px;
background-color: salmon;
border-radius: 50%;
}
body{
margin: 0;
}
</style>
</head>
<body>
<div class="root"></div>
</body>
<script type="text/babel">
class Drag extends React.Component {
constructor(...args){
super(...args)
}
FnStart(ev){
this.oDiv=ev.target
this.PageX=ev.changedTouches[0].pageX-this.oDiv.offsetLeft
this.PageY=ev.changedTouches[0].pageY-this.oDiv.offsetTop
document.ontouchmove=this.FnMove.bind(this)
document.ontouchend=this.FnEnd.bind(this)
}
FnMove(ev){
this.X=ev.changedTouches[0].pageX-this.PageX
this.Y=ev.changedTouches[0].pageY-this.PageY
this.oDiv.style.left=this.X+'px'
this.oDiv.style.top=this.Y+'px'
}
FnEnd(ev){
document.ontouchmove=null
document.ontouchend=null
}
render(){
return (<div className="ball" onTouchStart={this.FnStart.bind(this)}></div>)
}
}
ReactDOM.render(<Drag/>,document.querySelector('.root'))
</script>
</html>
09-24
2284
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-22
5592
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
06-08
11-26
1757
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-13