css 3とjsは重力誘導を実現する(試験、andriod 4.2サポート)
4038 ワード
<img src="http://img1.wgsails.com/yuetaojie/slider/227/193/089/index/ .jpg" id="imgLogo" alt="">
<p id="moCalcTiltLR">p>
p>
<span style="color:rgb(0,0,255);line-height:1.5 !important;">function</span><span style="line-height:1.5 !important;"> deviceMotionHandler(eventData) {
</span><span style="color:rgb(0,128,0);line-height:1.5 !important;">//</span>
<span style="color:rgb(0,0,255);line-height:1.5 !important;">var</span> acceleration =<span style="line-height:1.5 !important;"> eventData.accelerationIncludingGravity;
</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">var</span> facingUp = -1<span style="line-height:1.5 !important;">;
</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">if</span> (acceleration.z > 0<span style="line-height:1.5 !important;">) {
facingUp </span>= +1<span style="line-height:1.5 !important;">;
}
</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">var</span> tiltLR = Math.round(((acceleration.x) / 9.81) * -90<span style="line-height:1.5 !important;">);
</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">var</span> tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 *<span style="line-height:1.5 !important;"> facingUp);
document.getElementById(</span>"moCalcTiltLR").innerHTML =<span style="line-height:1.5 !important;"> tiltLR;
document.getElementById(</span>"moCalcTiltFB").innerHTML =<span style="line-height:1.5 !important;"> tiltFB;
</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">var</span> rotation = "rotate(" + tiltLR + "deg) rotate3d(1,0,0, " + (tiltFB) + "deg)"<span style="line-height:1.5 !important;">;
document.getElementById(</span>"imgLogo").style.webkitTransform =<span style="line-height:1.5 !important;"> rotation;
}
</span><span style="color:rgb(0,128,0);line-height:1.5 !important;">//</span>
<span style="color:rgb(0,0,255);line-height:1.5 !important;">if</span><span style="line-height:1.5 !important;"> (window.DeviceMotionEvent) {
window.addEventListener(</span>'devicemotion',deviceMotionHandler, <span style="color:rgb(0,0,255);line-height:1.5 !important;">false</span><span style="line-height:1.5 !important;">);
}</span><span style="color:rgb(0,0,255);line-height:1.5 !important;">else</span><span style="line-height:1.5 !important;">{
alert(</span>' , DeviceMotionEvent ~'<span style="line-height:1.5 !important;">);
}
</span>