移动端判断手机横竖屏

CSS判断横屏还是竖屏

1.写在同一个css文件中

@media screen and (orientation: portrait) {
/*竖屏 css*/
}
@media screen and (orientation: landscape) {
/*横屏 css*/
}

注意:@media screen不能打印,@media print是只能打印,@media就都可以

2.分开写在2个CSS中

竖屏:

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">

横屏:

<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">

js判断横屏还是竖屏

//判断手机横竖屏状态:
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
	if (window.orientation === 180 || window.orientation === 0) { 
		// alert('竖屏状态!');
	} 
	if (window.orientation === 90 || window.orientation === -90 ){ 
		// alert('横屏状态!');
	}  
}, false); 

//移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态。

案例demo

  • 效果: 横屏的时候会出现提示的信息。
  • 注意:使用时需要关闭手机设置的竖屏锁定才能看到效果
  • 注意2: 在css里面使用@media的话是获取刷新后是横屏还是竖屏进行改变,不监听。在js里面使用不用刷新,只要横竖屏改变了就会触发。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>css横竖屏</title>
    <style>
        .landscapeTip {
			z-index: 100;
			position: fixed;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background: rgba(0,0,0,.6);
			color: #fff;
			text-align: center;
		} 
		.landscapeTip span {
			display: block;
			position: absolute;
			top: 0;
			bottom: 0;
			width: 100%;
			height: 50px;
			margin: auto 0;
			line-height: 50px;
		}
		
		 /* 横屏 */
		/* @media all and (orientation: landscape) {
		 ​	.landscapeTip {
				display: block;
			}
		 } *//* 竖屏 */
		 /* @media all and (orientation: portrait) {
		    .landscapeTip {
		     	display: none;
		    }
		 } */
        
    </style>
</head>
<body>
    <div class="landscapeTip" style="display: none;">
		<span>请将手机锁定成竖屏</span>
	</div>
</body>
<script>
	window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
        if (window.orientation === 180 || window.orientation === 0) { 
            // alert('竖屏状态!');
			document.getElementsByClassName('landscapeTip')[0].style.display = 'none';
        } 
        if (window.orientation === 90 || window.orientation === -90 ){ 
            // alert('横屏状态!');
			document.getElementsByClassName('landscapeTip')[0].style.display = 'block';
        }  
    }, false); 
</script>
</html>

注意

如果遇到竖屏变横屏后回到竖屏,页面大二倍的情况,请用以下代码

window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", () => {
            // 正常方向或屏幕旋转180度
            // 屏幕顺时钟旋转90度或屏幕逆时针旋转90度
            if (window.orientation === 90 || window.orientation === -90) {
            	//横屏
                var i = document.getElementsByTagName('meta');
                i[1]["content"] = "width=750,initial-scale=1,maximum-scale=1,minimum-scale=1";
    
            }
            if (window.orientation === 180 || window.orientation === 0) {
            	//竖屏
                var i = document.getElementsByTagName('meta');
                i[1]["content"] = "width=750,user-scalable=no,target-densitydpi=device-dpi,minimal-ui";
  
            }


        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值