我一直在寻找类似的解决方案:
$(window).on("orientationchange",function(){
if(window.orientation == 90) {
console.log('This is 90 degrees');
} else if(window.orientation == -90) {
console.log('This is -90 degrees');
} else {
console.log('This is portrait');
}
});
但问题是我想要检测方向是哪种方式而不必转动屏幕。我想要加载屏幕,并且已经知道它可以在90度或设置为-90度。有什么想法吗?
答案 0 :(得分:1)
请检查以下代码,该代码适用于页面加载和方向更改,
<script>
function changeOrientation()
{
switch(window.orientation)
{
case -90:
alert('landscape -90');
break;
case 90:
alert('landscape 90');
break;
default:
alert('portrait');
break;
}
}
window.addEventListener('orientationchange', changeOrientation);
// on page load call
changeOrientation();
</script>