移动端判断横屏竖屏

1. CSS判断横屏竖屏

写在同一个CSS中

@media screen and (orientation: portrait) {
  /*竖屏 css*/
}
@media screen and (orientation: landscape) and (min-width:450px){
  /*横屏 css*/
}
分开写在2个CSS

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

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


2. JS判断横屏竖屏

复制代码
var sEvent = "onorientationchange" in window ? "orientationchange" : "resize";
window.addEventListener(sEvent, function() {
    if (window.orientation === 180 || window.orientation === 0) { 
        alert('竖屏状态!');
    } 
    if (window.orientation === 90 || window.orientation === -90 ){ 
        alert('横屏状态!');
    }  
}, false); 
复制代码

 

posted @   全玉  阅读(378)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示