当前位置:网站首页>移动端避免使用100vh[通俗易懂]
移动端避免使用100vh[通俗易懂]
2022-06-27 19:56:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
CSS中的视口单位听起来很棒。如果要设置元素的样式以占据整个屏幕的高度,则可以设置height: 100vh,您拥有一个完美的全屏元素,该元素会随着视口的变化而调整大小!可悲的是,事实并非如此。100vh在移动浏览器中以微妙但基本的方式被破坏,这使其几乎无用。最好避免100vh ,而是依靠javascript设置高度以获得完整的视口体验。
核心问题是移动浏览器(我正在为您浏览,Chrome和Safari)具有“帮助”功能,其中地址栏有时可见,有时隐藏,从而改变了视口的可见大小。这些浏览器没有将100vh高度调整为视口高度变化时屏幕的可见部分,而是将100vh设置为浏览器的高度,并隐藏了地址栏。结果是,当地址栏可见时,屏幕的底部将被切除。
如下所示:
当显示地址栏时,由于移动浏览器错误地将100vh设置为屏幕高度而没有显示地址栏,因此屏幕底部被切断。在上图中,应隐藏在屏幕底部的按钮。更糟糕的是,当用户首次访问移动设备上的网站时,地址栏将在顶部可见,因此默认的体验是破碎的体验。
更好的解决方案:window.innerHeight
解决此问题的一种方法是依靠javascript而不是CSS。页面加载时,将高度设置为window.innerHeight可以将高度正确设置为窗口的可见部分。如果地址栏可见,则window.innerHeight将为全屏的高度。如果地址栏是隐藏的,则window.innerHeight将是屏幕上可见部分的高度,这正是您所期望的。
在Wordsheet.io上学习时,您可以看到这一点。例如,尝试在移动浏览器上打开wordsheet.io/demo/V3Y 。无论地址栏是否可见,屏幕都将是视口的高度。此外,通过在页面首次加载时将高度锁定在适当的位置,可以防止地址栏隐藏在使用该网站的过程中,从而带来尴尬的屏幕调整大小体验。
遗憾的是,在不依赖JavaScript的情况下,仍然没有一种简单的方法来使元素占据整个视口高度。 height: 100vh 非常接近 ,但鉴于其在移动设备上的局限性,最好避免使用它。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133174.html原文链接:https://javaforall.cn
边栏推荐
- 對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
- Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
- Process judgment - ternary operation - for loop
- 【微服务】(十六)—— 分布式事务Seata
- Stunned! The original drawing function of markdown is so powerful!
- 爬虫笔记(2)- 解析
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- Day8 - cloud information project introduction and creation
- Memoirs of actual combat: breaking the border from webshell
- regular expression
猜你喜欢

医美大刀,砍向00后

PE买下一家内衣公司

对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华

對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華

Codeforces Round #719 (Div. 3)

Solve the problem that the virtual machine cannot be connected locally

Remote invocation of microservices

Memoirs of actual combat: breaking the border from webshell
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?

资深猎头团队管理者:面试3000顾问,总结组织出8大共性(茅生)
随机推荐
渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function
Penetration learning - shooting range chapter - detailed introduction to Pikachu shooting range (under continuous update - currently only the SQL injection part is updated)
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
99 multiplication table - C language
管理系统-ITclub(上)
Learn to go concurrent programming in 7 days go language sync Application and implementation of cond
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
解决本地连接不上虚拟机的问题
爬虫笔记(1)- urllib
北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
Solve the problem that the virtual machine cannot be connected locally
CUDA error:out of memory caused by insufficient video memory of 6G graphics card
go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
同花顺炒股软件可靠吗??安全嘛?
One to many association in MySQL to obtain the latest data in multiple tables
Gartner focuses on low code development in China how UNIPRO practices "differentiation"
C # QR code generation and recognition, removing white edges and any color
Transformation from student to engineer
Improving deep neural networks: hyperparametric debugging, regularization and optimization (III) - hyperparametric debugging, batch regularization and program framework