以下是我的代码的链接:www.parallaxplayground.com/flipside.html 我在尝试使用手机陀螺仪创建的变量翻转用户手机时尝试打开新网页。
<script>
function init() {
//Find our div containers in the DOM
var dataContainerOrientation = document.getElementById('dataContainerOrientation');
var dataContainerMotion = document.getElementById('dataContainerMotion');
//Check for support for DeviceOrientation event
if(window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', function(event) {
var alpha = event.alpha;
var beta = event.beta;
var gamma = event.gamma;
if(alpha!=null || beta!=null || gamma!=null)
dataContainerOrientation.innerHTML = 'alpha: ' + alpha + '<br/>beta: ' + beta + '<br />gamma: ' + gamma;
}, false);
}
// Check for support for DeviceMotion events
if(window.DeviceMotionEvent) {
window.addEventListener('devicemotion', function(event) {
var x = event.accelerationIncludingGravity.x;
var y = event.accelerationIncludingGravity.y;
var z = event.accelerationIncludingGravity.z;
var r = event.rotationRate;
var html = 'Acceleration:<br />';
html += 'x: ' + x +'<br />y: ' + y + '<br/>z: ' + z+ '<br />';
html += 'Rotation rate:<br />';
if(r!=null) html += 'alpha: ' + r.alpha +'<br />beta: ' + r.beta + '<br/>gamma: ' + r.gamma + '<br />';
if (y > 7)
{
window.open("http://www.w3schools.com");
};
dataContainerMotion.innerHTML = html;
});
}
}
</script>
&#13;
您必须在手机上进行测试。感谢您提供的任何帮助!