<!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>滑块拖拽移动端2</title>
<script src="./js/browser.js"></script>
<script src="./js/react.js"></script>
<script src="./js/react-dom.js"></script>
<style>
.box {
position: absolute;
top: 20px;
height: 20px;
width: 60px;
height: 60px;
background-color: palevioletred;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="add"></div>
</body>
<script type="text/babel">
class Drag extends React.Component {
constructor(...arge) {
super(...arge);
}
FnStart(ev) {
this.Touch = ev.changedTouches[0];
this.startX = this.Touch.pageX - ev.target.offsetLeft;
this.startY = this.Touch.pageY - ev.target.offsetTop;
document.ontouchmove = this.FnMove.bind(this);
document.ontouchend = this.FnEnd.bind(this);
}
FnMove(ev) {
this.Touch = ev.changedTouches[0];
this.X = this.Touch.pageX - this.startX;
this.Y = this.Touch.pageY - this.startY;
ev.target.style.left = this.X + 'px';
ev.target.style.top = this.Y + 'px';
}
FnEnd(ev) {
document.ontouchmove = null;
document.ontouchend = null;
}
render() {
return (
<div className="box" onTouchStart={this.FnStart.bind(this)}></div>
)
}
}
ReactDOM.render(<Drag />, document.querySelector("#add"))
</script>
</html>