Home > front end >  Mobile terminal offsetLeft drag value change
Mobile terminal offsetLeft drag value change

Time:12-29

 & lt; ! DOCTYPE html> 

<meta charset="utf-8" & gt;
<meta name="viewport" content="width=device - width, initial - scale=1.0" & gt;
Document
<style>
* {
padding:0;
margin:0;
}
The box {
Width: 200 px;
Height: 200 px;
Background - color: rosybrown;
/* position: absolute; */
}
</style>

<body>

<script>
Var box=document. QuerySelector (' box ');
Var startX=0;
Var startY=0;
Var x=0;
Box. AddEventListener (' touchstart ', function (e) {
StartX=e. argetTouches [0]. PageX;
The console. The log (startX)
})
Box. AddEventListener (' touchmove ', function (e) {
Var moveX=e. argetTouches [0]. PageX - startX;
The console. The log (box. The offsetLeft);
This. Style. Transform='translateX (px)' + x + ' ';
This. Style. Transform='translateX (px)' + moveX + ' ';
The console. The log (e);
})
Box. AddEventListener (' touchend ', function () {
})
</script>


Why mobile div why offsetLeft hasn't changed?

CodePudding user response:

Pay attention to the use of the transform

CodePudding user response:



<style>
* {
padding:0;
margin:0;
}
The aaa {
width: 100%;
position: relative;
}
The box {
Width: 200 px;
Height: 200 px;
Background - color: rosybrown;
position: absolute;
}
</style>

<body>


<script>
Var box=document. QuerySelector (' box ');
Var startX=0;
Var startY=0;
Var x=0;
Box. AddEventListener (' touchstart ', function (e) {
StartX=e. argetTouches [0]. PageX;
})
Box. AddEventListener (' touchmove ', function (e) {
Var moveX=e. argetTouches [0]. PageX - startX;
The console. The log (box. The offsetLeft);
This. Style. Left=x + 'px';
This. Style. Left=moveX + 'px';
})
Box. AddEventListener (' touchend ', function () {
})
</script>

You need to change the transform to the left to get to the value, or you just need to get the transform values, rather than the offsetLeft
  • Related