当前位置:网站首页>移动端避免使用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
边栏推荐
- Ellipsis after SQLite3 statement Solutions for
- 元气森林的5元有矿之死
- Macro task and micro task understanding
- 99 multiplication table - C language
- The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
- Introduction to MySQL operation (IV) -- data sorting (ascending, descending, and multi field sorting)
- Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
- DCC888 :Register Allocation
- go语言切片Slice和数组Array对比panic: runtime error: index out of range问题解决
- 初识C语言 第二弹
猜你喜欢
扁平数组和JSON树的转换

渗透学习-靶场篇-dvwa靶场详细攻略(持续更新中-目前只更新sql注入部分)

关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证

Crawler notes (2) - parse

Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string

管理系统-ITclub(上)

Management system itclub (Part 2)

结构化机器学习项目(二)- 机器学习策略(2)

About the SQL injection of davwa, errors are reported: analysis and verification of the causes of legal mix of settlements for operation 'Union'

7 jours d'apprentissage de la programmation simultanée go 7 jours de programmation simultanée go Language Atomic Atomic Atomic actual Operation contains ABA Problems
随机推荐
Remote invocation of microservices
Go from introduction to practice -- definition and implementation of behavior (notes)
Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string
记一次List对象遍历及float类型判断大小
Go from introduction to practice -- shared memory concurrency mechanism (notes)
Conversion between flat array and JSON tree
Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury
MySQL greater than less than or equal to symbol representation
Common APIs (Methods) for scope -number and string
Re recognize G1 garbage collector through G1 GC log
Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
[suggested collection] ABAP essays-excel-4-batch import recommended
Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
Codeforces Round #721 (Div. 2)
医美大刀,砍向00后
使用sqlite3语句后出现省略号 ... 的解决方法
管理系統-ITclub(下)
雪糕还是雪“高”?
《7天学会Go并发编程》第六天 go语言Sync.cond的应用和实现 go实现多线程联合执行