我写了一些代码,用于提醒手机上的用户改变为横向,基本上是肖像.这很有效,但如果用户在横向开始,它显然没有显示,因为没有方向改变.有没有办法让你得到的方向?
$(window).on("orientationchange",function(){
if( (window.orientation == 90 && $( window ).width() < 780) || (window.orientation == -90 && $( window ).width() < 780) ) // Landscape
{
$('body').append('<div class="phoneLandscape"><h4>Sorry, this site is best viewed in Portrait Mode</h4></div>');
}
else // Portrait
{
$('.phoneLandscape').remove();
}
});
Run Code Online (Sandbox Code Playgroud)
jQuery mobile有这个方法$(window).on("orientationchange")但是如果你想在任何给定的时间检测方向,试试这个
if(window.innerHeight > window.innerWidth){
//portrait
}
if(window.innerWidth > window.innerHeight){
//landscape
}
Run Code Online (Sandbox Code Playgroud)