我想用JS来描述用户所走过的距离。以下内容在移动模式下对铬进行工作,但当我切换到iOS时,没有任何工作。我做了一些调查,发现手机上的setTimeout()有一些特别之处,尽管给出的解决方案不起作用。如果删除setTimeout(),所有内容都可以在iOS上工作,但它的触发速度太快,无法注册良好的移动。
window.addEventListener('load', function(){
var box1 = document.getElementById('box1');
var statusdiv = document.getElementById('statusdiv');
var startx = 0;
var starty = 0;
var dist = 0;
box1.addEventListener('touchstart', function(e){ e.preventDefault(); });
box1.addEventListener('touchmove', function(e){
var touchobj = e.changedTouches[0];
startx = parseInt(touchobj.clientX);
starty = parseInt(touchobj.clientY);
setTimeout(function(){
var dist = Math.sqrt(Math.pow(parseInt(touchobj.clientX) - startx,2) + Math.pow(parseInt(touchobj.clientY) - starty,2));
var angleDeg = 180 - Math.atan2((parseInt(touchobj.clientY) - starty) , (parseInt(touchobj.clientX) - startx)) * (180 / Math.PI);
statusdiv.innerHTML = 'Status: touchmove<br> Horizontal distance traveled: ' + dist + 'px';
} , 50);
e.preventDefault();
}, false)
}, false)发布于 2016-05-31 23:14:02
我没有像我想的那样打扫这个,但它起作用了。我把它换了,这样它就可以在一段时间内工作。
window.addEventListener('load', function(){
function calc(){
var dist = Math.sqrt(Math.pow(parseInt(touchobj.clientX) - startx,2) + Math.pow(parseInt(touchobj.clientY) - starty,2));
var angleDeg = 180 - Math.atan2((parseInt(touchobj.clientY) - starty) , (parseInt(touchobj.clientX) - startx)) * (180 / Math.PI);
statusdiv.innerHTML = 'Status: touchmove<br> Horizontal distance traveled: ' + dist + 'px';
startx = parseInt(touchobj.clientX);
starty = parseInt(touchobj.clientY);
}
var box1 = document.getElementById('box1');
var statusdiv = document.getElementById('statusdiv');
var startx = 0;
var starty = 0;
var dist = 0;
var intThing;
var touchobj;
box1.addEventListener('touchstart', function(e){
intThing = setInterval(calc, 50);
e.preventDefault();
});
box1.addEventListener('touchmove', function(e){
touchobj = e.changedTouches[0];
e.preventDefault();
}, false)
box1.addEventListener('touchend', function(e){
clearInterval(intThing);
e.preventDefault();
});
}, false)https://stackoverflow.com/questions/37537221
复制相似问题