欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > H5页面在移动端自动横屏

H5页面在移动端自动横屏

2025/5/23 3:01:40 来源:https://blog.csdn.net/qq_38810813/article/details/146098047  浏览:    关键词:H5页面在移动端自动横屏

首先需要再head标签添加这样一段代码

<meta name="viewport" content="width=device-width,height=device-width,initial-scale=1.0,user-scalable=no">

因为需求是为了满足WEB端和手机端都可以查看整体效果 但由于UI没有设计移动端的样式 所以我想说把这个页面横屏 这样的话效果一样可以在手机端展示 所以我们需要判断是否是在移动端

function isMobileDevice() {var userAgent = navigator.userAgent || navigator.vendor || window.opera;// 常见的移动设备标识var isAndroid = /android/i.test(userAgent);var isiOS = /iphone|ipad|ipod/i.test(userAgent);var isOpera = /opera mini/i.test(userAgent);var isWindows = /IEMobile/i.test(userAgent);var isBlackBerry = /BB10/i.test(userAgent);var isMobileWebkit = /Mobile/i.test(userAgent);if (/Mobile|Android|Silk|Kindle|BlackBerry|Opera Mini|Opera Mobi|OPiOS|FxiOS|IEMobile/i.test(userAgent)) {return true;}return false;
}

如果判断是移动端 那么我再去让整个页面横屏

if (isMobileDevice()) {let width = window.screen.width;let height = window.screen.height;let x = (width/1080).toString().substring(0,4)let y = null;y = (height/1920).toString().substring(0,3);let rotate = (height/1920).toString().substring(0,3);let topPx = height - width;document.getElementById('rotatePage').style.width = '1920px';document.getElementById('rotatePage').style.height = '1080px';document.getElementById('rotatePage').style.position = 'fixed';document.getElementById('rotatePage').style.top = '-'+(width/2)+'px';document.getElementById('rotatePage').style.left = '50%';document.getElementById('rotatePage').style.zIndex = '999';document.getElementById('rotatePage').style.transform = 'scaleX('+ x +') scaleY('+ y +')';document.getElementById('rotatePage').style.transformOrigin = 'left top';document.body.style.transform = 'rotate(90deg)';
} else {console.log("当前设备为非移动端");
}

整体的页面布局需要在整个div外面再套一层div 为了方便整个页面旋转 在id为rotatePage内去渲染页面需要展示的内容。

<body>
<div id="outerBox"><div id="rotatePage"></div>
</div>
</body>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词