jquery - 检测哪个 View 在移动设备上处于事件状态

标签 jquery css mobile

我需要知道用户在其手机或 Pad 上是垂直还是水平查看网站以切换我的图像比例。

例子:

如果用户水平查看页面,我的图片样式将是全尺寸

img{width:100%; height:auto;}

但如果它是垂直的,则需要将 css 切换为

img{width:auto; height:100%;}

那么我如何才能在 jQuery 或 Javascript 或媒体查询等中实现类似的东西,这样我就可以根据用户在移动设备或 iPad 上查看页面的 View 来采用好的风格?

谢谢

最佳答案

您可以使用一些 javascript 检测方向,例如:

detectOrientation();
    window.onorientationchange = detectOrientation;
    function detectOrientation(){
        if(typeof window.onorientationchange != 'undefined'){
            if ( orientation == 0 ) {
                 //Do Something In Portrait Mode
            }
            else if ( orientation == 90 ) {
                 //Do Something In Landscape Mode
            }
            else if ( orientation == -90 ) {
                 //Do Something In Landscape Mode
            }
            else if ( orientation == 180 ) {
                 //Do Something In Landscape Mode
            }
        }
    }

来自 http://www.devinrolsen.com/javascript-mobile-orientation-detection/

关于jquery - 检测哪个 View 在移动设备上处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462977/

相关文章:

html - 我的 <p> 正在使用 <Div> 的 CSS?为什么?

在移动设备上分享 Facebook

css - 关于响应式网站、像素和密度

javascript - 我无法将 ajax 数据保存到变量中

jquery - javascript平滑后滚动效果

javascript - multipart/formdata 不使用 jQuery.ajax 发送文件数据

javascript - 使用 Jquery 动态添加类?

css - reCAPTCHA 位置无法更改(从右下角开始)

javascript - react native SectionList 布局

java - 在 ivy.xml 中为 Qmetry 框架项目添加依赖项的逻辑,以自动化移动 native 应用程序