检测视口方向,如果方向是纵向显示警告消息,建议用户说明

Dan*_*Dan 181 javascript mobile jquery viewport device-orientation

我正在建立专门针对移动设备的网站.特别是有一个页面,最好以横向模式查看.

有没有办法检测访问该页面的用户是否在纵向模式下查看它,如果是,则显示一条消息,通知用户该页面最好以横向模式查看?如果用户已在横向模式下查看,则不会显示任何消息.

所以基本上,我希望网站检测视口方向,如果方向是纵向,则显示警告消息,告知用户该页面在横向模式下最佳查看.

非常感谢,Dan

tob*_*ies 256

if(window.innerHeight > window.innerWidth){
    alert("Please use Landscape!");
}
Run Code Online (Sandbox Code Playgroud)

jQuery Mobile有一个事件来处理这个属性的更改...如果你想警告以后有人转动 - orientationchange

此外,经过一些谷歌搜索,退房orientationchange(我相信以度数衡量...

  • 在许多设备上,当显示键盘时,availableWidth将大于高度,错误地给出横向方向. (68认同)
  • 我过度复杂了这个问题.这很棒.很简单. (7认同)
  • 不,不幸的是没有.但是,以下脚本确实有效:if(window.innerHeight> window.innerWidth){alert("请在风景中查看"); } (6认同)
  • window.orientation 已弃用 https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation (4认同)

crm*_*cco 144

您也可以使用window.matchMedia,我使用和喜欢它,因为它非常类似于CSS语法:

if (window.matchMedia("(orientation: portrait)").matches) {
   // you're in PORTRAIT mode
}

if (window.matchMedia("(orientation: landscape)").matches) {
   // you're in LANDSCAPE mode
}
Run Code Online (Sandbox Code Playgroud)

在iPad 2上测试过.

  • 对此的支持现在要强得多.这似乎是一个更加健全的答案,现在接受了. (13认同)
  • 虽然支持该功能有点弱:http://caniuse.com/#feat=matchmedia (3认同)
  • 在Firefox for Android中,只有在我准备好DOM准备事件后才能正常工作. (2认同)
  • 使用此(链接)解决方案当心此事/sf/ask/621821441/ (2认同)
  • 现在,这是正确的答案。此功能完全支持所有相关浏览器。 (2认同)

小智 92

大卫沃尔什有一个更好,更直接的方法.

// Listen for orientation changes
window.addEventListener("orientationchange", function() {
  // Announce the new orientation number
  alert(window.orientation);
}, false);
Run Code Online (Sandbox Code Playgroud)

在这些更改期间,window.orientation属性可能会更改.值0表示纵向视图,-90表示设备横向旋转到右侧,90表示设备横向旋转到左侧.

http://davidwalsh.name/orientation-change

  • 方向0被赋予设备的"自然"方向,因此取决于供应商.它可能是肖像或风景...... (8认同)
  • 在这个链接上:https://notes.matthewgifford.com/a-misconception-about-window-orientation-7231235a94c2#.llqebngde作者展示了来自不同制造商的不同设备对景观和肖像的不同解释.因此,不同的报告值,你不能真正依赖. (3认同)
  • 在Nexus 10上,我不知道其他的Android 10"平板电脑,价值是倒退的.即当设备处于横向而不是肖像时返回0.tobyodavies上面的答案似乎在所有设备上运行良好我'经过测试 (2认同)
  • 对于设备的检测方向mod,这不是一个好的解决方案。因为它们的返回值取决于自然的设备方向。在三星7'平板电脑纵向视图上的示例返回90,但在纵向联系的4上返回0。 (2认同)
  • orientationchange 已弃用 (2认同)

Tho*_*aux 35

你可以使用CSS3:

@media screen and (orientation:landscape)
{
   body
   {
      background: red;
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 不,他想显示一条消息,你可以使用display:none然后display:block来显示一些东西. (10认同)
  • 与javascript无关.OP需要javascript解决方案. (4认同)
  • 此CSS检查提供什么样的支持? (2认同)

mar*_*nas 19

有几种方法可以做到,例如:

  • 检查window.orientation价值
  • 比较innerHeightinnerWidth

您可以采用以下方法之一.


检查设备是否处于纵向模式

function isPortrait() {
    return window.innerHeight > window.innerWidth;
}
Run Code Online (Sandbox Code Playgroud)

检查设备是否处于横向模式

function isLandscape() {
    return (window.orientation === 90 || window.orientation === -90);
}
Run Code Online (Sandbox Code Playgroud)

用法示例

if (isPortrait()) {
    alert("This page is best viewed in landscape mode");
}
Run Code Online (Sandbox Code Playgroud)

如何检测方向变化?

$(document).ready(function() {
    $(window).on('orientationchange', function(event) {
        console.log(orientation);
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这完美!ios 7 iphone4,ios 8 ipad mini (3认同)

art*_*pro 10

我认为更稳定的解决方案是使用屏幕而不是窗口,因为如果您要在台式计算机上调整浏览器窗口的大小,它可以是横向或纵向.

if (screen.height > screen.width){
    alert("Please use Landscape!");
}
Run Code Online (Sandbox Code Playgroud)

  • @artnikpro 我昨天测试了它,似乎 Safari 返回物理屏幕尺寸而忽略了方向。所以 screen.width 始终是屏幕的物理宽度。 (2认同)

小智 8

为了将所有这些伟大的注释应用到我的日常编码中,为了我所有应用程序之间的连续性,我决定在我的jquery和jquery移动代码中使用以下内容.

window.onresize = function (event) {
  applyOrientation();
}

function applyOrientation() {
  if (window.innerHeight > window.innerWidth) {
    alert("You are now in portrait");
  } else {
    alert("You are now in landscape");
  }
}
Run Code Online (Sandbox Code Playgroud)


cpt*_*ing 7

仅 CCS

@media (max-width: 1024px) and (orientation: portrait){ /* tablet and smaller */
  body:after{
    position: absolute;
    z-index: 9999;
    width: 100%;
    top: 0;
    bottom: 0;
    content: "";
    background: #212121 url(http://i.stack.imgur.com/sValK.png) 0 0 no-repeat; /* replace with an image that tells the visitor to rotate the device to landscape mode */
    background-size: 100% auto;
    opacity: 0.95;
  }
}
Run Code Online (Sandbox Code Playgroud)

在某些情况下,您可能希望在访问者旋转设备后添加一小段代码以重新加载到页面,以便正确呈现 CSS:

window.onorientationchange = function() { 
    var orientation = window.orientation; 
        switch(orientation) { 
            case 0:
            case 90:
            case -90: window.location.reload(); 
            break; } 
};
Run Code Online (Sandbox Code Playgroud)


小智 6

不要尝试修复window.orientation查询(0,90等并不意味着肖像,风景等):

http://www.matthewgifford.com/blog/2011/12/22/a-misconception-about-window-orientation/

即使在iOS7上,取决于你如何进入浏览器0并不总是肖像


Ste*_*lec 6

我不同意投票最多的答案。使用screen与不使用window

    if(screen.innerHeight > screen.innerWidth){
    alert("Please use Landscape!");
}
Run Code Online (Sandbox Code Playgroud)

是正确的做法。如果你用 计算window.height,你会在 Android 上遇到麻烦。当键盘打开时,窗口会缩小。所以使用屏幕而不是窗口。

screen.orientation.type是一个很好的答案,但对于 IE。 https://caniuse.com/#search=screen.orientation


Dav*_*Sag 5

经过一些实验,我发现旋转方向感知设备将始终触发浏览器窗口的resize事件.所以在你的resize处理程序中只需调用一个函数:

function is_landscape() {
  return (window.innerWidth > window.innerHeight);
}
Run Code Online (Sandbox Code Playgroud)


ART*_*yet 5

我结合了两个解决方案,它对我来说很好.

window.addEventListener("orientationchange", function() {                   
    if (window.matchMedia("(orientation: portrait)").matches) {
       alert("PORTRAIT")
     }
    if (window.matchMedia("(orientation: landscape)").matches) {
      alert("LANSCAPE")
     }
}, false);
Run Code Online (Sandbox Code Playgroud)


Jom*_*aul 5

$(window).on("orientationchange",function( event ){
    alert(screen.orientation.type)
});
Run Code Online (Sandbox Code Playgroud)