js检查屏幕是否旋转

<br />
// Detect whether device supports orientationchange event, otherwise fall back to the resize event.<br />
var supportsOrientationChange = "onorientationchange" in window,<br />
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";</p>
<p>window.addEventListener(orientationEvent, function() {<br />
alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + " " + screen.width);<br />
}, false);<br />

<br />
(function(){<br />
var supportsOrientation = (typeof window.orientation == 'number' && typeof window.onorientationchange == 'object');<br />
var HTMLNode = document.body.parentNode;<br />
var updateOrientation = function() {<br />
// rewrite the function depending on what's supported<br />
if(supportsOrientation) {<br />
updateOrientation = function() {<br />
var orientation = window.orientation;</code></p>
<p>switch(orientation) {<br />
case 90: case -90:<br />
orientation = 'landscape';<br />
break;<br />
default:<br />
orientation = 'portrait';<br />
}</p>
<p>// set the class on the HTML element (i.e. )<br />
HTMLNode.setAttribute('class', orientation);<br />
}<br />
} else {<br />
updateOrientation = function() {<br />
// landscape when width is biggest, otherwise portrait<br />
var orientation = (window.innerWidth > window.innerHeight) ? 'landscape': 'portrait';</p>
<p>// set the class on the HTML element (i.e. )<br />
HTMLNode.setAttribute('class', orientation);<br />
}<br />
}</p>
<p>updateOrientation();<br />
}<br />
var init = function() {<br />
// initialize the orientation<br />
updateOrientation();</p>
<p>if(supportsOrientation) {<br />
window.addEventListener('orientationchange', updateOrientation, false);<br />
} else {<br />
// fallback: update every 5 seconds<br />
setInterval(updateOrientation, 5000);<br />
}</p>
<p>}<br />
window.addEventListener('DOMContentLoaded', init, false);<br />
})();<br />

<br />
(function(){var e=typeof window.orientation=="number"&&typeof window.onorientationchange=="object",f=document.body.parentNode;function c(){c=e?function(){var d=window.orientation;switch(d){case 90:case -90:d="landscape";break;default:d="portrait"}f.setAttribute("class",d)}:function(){f.setAttribute("class",window.innerWidth>window.innerHeight?"landscape":"portrait")};c()}window.addEventListener("DOMContentLoaded",function(){c();e?window.addEventListener("orientationchange",c,false):setInterval(c,5E3)},false)})();<br />

摘自: http://davidbcalhoun.com/2010/dealing-with-device-orientation

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.