移动端页面强制横屏

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" wrapper="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<style type="text/css">
			* { margin: 0; }
			.wrapper { position: absolute; top: 0; left: 0; width: 100%; overflow-y: scroll; }
			.content{width: 100%;}
			img{width: 100%;}
		</style>
	</head>

	<body>
		<div class="wrapper">
			<div class="content">
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
				<img src="http://attach.bbs.miui.com/forum/201301/27/132323rsp4ts9jzxpyuf3x.jpg"/>
			</div>
		</div>
		<script src="js/jquery1.12.4.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			$(function() {
				var width = document.documentElement.clientWidth;
				var height = document.documentElement.clientHeight;
				$w = $('.wrapper');
				$w.width(width);
				$w.height(height);

				if(width < height) {
					// 竖屏
					changeToPortrait($w, width, height);
				}

				window.onresize = changeOrientation;
			});

			function changeOrientation() {
				var width = document.documentElement.clientWidth;
				var height = document.documentElement.clientHeight;
				$w = $('.wrapper');
				if(width > height) {
					// 横屏
					changeToLandscape($w, width, height);
				} else {
					// 竖屏
					changeToPortrait($w, width, height);
				}

			}
			// 横屏
			function changeToLandscape($w, width, height){
				$w.width(width);
				$w.height(height);
				$w.css('top', 0);
				$w.css('left', 0);
				$w.css('transform', 'none');
				$w.css('transform-origin', '50% 50%');
			}
			// 竖屏
			function changeToPortrait($w, width, height){
				$w.width(height);
				$w.height(width);
				$w.css('top', (height - width) / 2);
				$w.css('left', 0 - (height - width) / 2);
				$w.css('transform', 'rotate(90deg)');
				$w.css('transform-origin', '50% 50%');
			}
		</script>
	</body>

</html>

 


版权声明:本文为a1974117511原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。