iOS上陀螺仪移动和触摸移动的JavaScript存在冲突,当第二部分正在使用时,我如何暂停其中一部分
Conflicting JavaScript for gyroscope move and touchmove on iOS, how do I pause one part when the second is in use?
我在iOS网络应用程序上使用了一些JavaScript代码,这有点超出了我的体重。
我正在做的是使用将一层移动到另一层之上
- 陀螺仪和加速度计数据以及
- 触摸输入
这意味着我有两个javascript,它们都试图移动同一个东西。当你在iOS设备上查看时,你可以从当前版本中看到,这效果不太好(提示:透明度是用右边的按钮打开的,不,它现在不能用鼠标移动,除非你使用带陀螺仪的iOS设备,否则不会移动)。
所以我已经完成了一些困难的部分,但我坚持认为,基于我对JavaScript的新手熟练程度,我会找到一个解决方案。
触摸移动代码激活时,如何停止陀螺仪移动代码?此外,我想我需要更新x+y值,这样一旦触摸移动结束,透明度就不会跳到位置。
我试着在代码的顶部添加一个if,else语句,但这似乎打破了很多。
顺便说一句,感谢StackOverflow上的每一个人,之前的Q和A为我走到这一步提供了大量帮助。
我们将不胜感激。
Stefan
到目前为止,这是我的代码,它生活在身体元素中。。。
var x = 0, y = 0,
vx = 0, vy = 0,
ax = 0, ay = 0;
var transp = document.getElementById("transp");
if (window.DeviceMotionEvent != undefined) {
window.ondevicemotion = function(e) {
ax = event.accelerationIncludingGravity.x * 5;
ay = event.accelerationIncludingGravity.y * 5 + 19;
document.getElementById("accelerationX").innerHTML = e.accelerationIncludingGravity.x;
document.getElementById("accelerationY").innerHTML = e.accelerationIncludingGravity.y;
document.getElementById("accelerationZ").innerHTML = e.accelerationIncludingGravity.z;
if ( e.rotationRate ) {
document.getElementById("rotationAlpha").innerHTML = e.rotationRate.alpha;
document.getElementById("rotationBeta").innerHTML = e.rotationRate.beta;
document.getElementById("rotationGamma").innerHTML = e.rotationRate.gamma;
}
}
setInterval( function() {
var landscapeOrientation = window.innerWidth/window.innerHeight > 1;
if ( landscapeOrientation) {
vx = vx + ay;
vy = vy + ax;
} else {
vy = vy - ay;
vx = vx + ax;
}
vx = vx * 0.98;
vy = vy * 0.98;
y = parseInt(y + vy / 70);
x = parseInt(x + vx / 70);
boundingBoxCheck();
transp.style.top = y + "px";
transp.style.left = x + "px";
}, 25);
}
function boundingBoxCheck(){
if (x<-310) { x = -310; vx = -vx; }
if (y<-300) { y = -300; vy = -vy; }
if (x>document.documentElement.clientWidth) { x = document.documentElement.clientWidth; vx = -vx; }
if (y>document.documentElement.clientHeight+400) { y = document.documentElement.clientHeight+400; vy = -vy; }
}
$.fn.moveable = function() {
var offset = null;
var start = function(e) {
var orig = e.originalEvent;
var pos = $(this).position();
offset = {
x: orig.changedTouches[0].pageX - pos.left,
y: orig.changedTouches[0].pageY - pos.top
};
};
var moveMe = function(e) {
e.preventDefault();
var orig = e.originalEvent;
$(this).css({
top: orig.changedTouches[0].pageY - offset.y,
left: orig.changedTouches[0].pageX - offset.x
});
};
this.bind("touchstart", start);
this.bind("touchmove", moveMe);
};
$(".moveable").moveable();
我的第一个想法是让一个isTouching变量在touchstart时设置为true,在touchEnd时设置回false。然后使用条件,只有当isTouching变量为false时,陀螺仪代码才会运行。希望能有所帮助。
相关文章:
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- FabricJs-限制主对象内添加对象的移动区域
- 如何从画布上的某个移动事件中获取X和Y
- 触摸移动时切换到新元素
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- JS-移动图像,暂停设置的时间,然后将图像向后移动
- 当用户将鼠标移到屏幕/图表上时,自动滚动将暂停.如果鼠标移动停止,自动滚动将再次恢复
- 无法在移动设备中通过 jquery 暂停视频
- 当用户在移动 Safari 浏览器上离开页面时暂停声音
- 如果鼠标移动到图像上,我如何添加鼠标事件,它将显示播放/停止/暂停按钮
- iOS上陀螺仪移动和触摸移动的JavaScript存在冲突,当第二部分正在使用时,我如何暂停其中一部分
- 检测HTML5视频暂停/播放的移动源
- 当用户从移动通知控件中按下播放/暂停键时,我如何重写默认操作
- 鼠标移动事件发生时暂停动画
- 视频.js,点按移动设备上的视频以播放/暂停
- 在移动设备上滚动时倒计时暂停,但在桌面设备上不滚动
- Youtube API暂停时,我按下播放在一些移动浏览器