Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of rem adaptation and viewport adaptation in html5

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍"如何解决html5中rem适配与viewport适配问题"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"如何解决html5中rem适配与viewport适配问题"文章能帮助大家解决问题。

H5 端 rem 适配方案与 viewport 适配rem

rem 是 CSS3 新增的一个相对单位(root em,根 em)

只根据当前页面 HTML 页面的 font-size 设置,如果根目录的 font-size 为 18px,则 1rem=18px

媒体查询设置@media screen and (min-width: 320px) { html { font-size: 32px; }}@media screen and (min-width: 375px) { html { font-size: 37.5px; }}@media screen and (min-width: 414px) { html { font-size: 41.4px; }}@media screen and (min-width: 750px) { html { font-size: 75px; }}使用 JS 动态修改 // 根据屏幕尺寸大小调整html的fontsize function setHtmlFontSize() { const width = document.documentElement.clientWidth; document.documentElement.style.fontSize = width / 10 + "px"; } // 初始化 setHtmlFontSize(); // 监听屏幕尺寸变化事件 window.addEventListener("resize", setHtmlFontSize); // 监听屏幕翻转事件 window.addEventListener("orientationchange", setHtmlFontSize);viewport

通过缩放来实现移动端各个尺寸的适配

适配方案 动态创建 mate viewport 属性,根据当前屏幕尺寸动态设置缩放值

Viewport 属性属性说明备注width以 px 为单位定义 viewport 的宽度一个正整数或者额字符串 device-widthheight以 px 为单位定义 viewport 的高度一个正整数或者额字符串 device-heightinitial-scale定义设备的 dips 宽度与 viewport 尺寸之间的比例一个 0.0 到 10.0 之间的正数maximum-scale定义最大缩放值,他的值必需大于等于 minimum-scale 的值一个 0.0 到 10.0 之间的正数minimum-scale定义最小缩放值,他的值必需小于等于 maximum-scale 的值一个 0.0 到 10.0 之间的正数user-scalable一个布尔值,用户是否可以缩放页面yes 或 no

使用 js 动态设置 viewport 属性

原理:通过设置 viewport 的 initial-scale 相关属性 , 将所有设备布局视口的宽度调整为设计图的宽度

//定义设计稿宽度为375const DESIGN_WIDTH = 375;//通过设置meta元素中content的initial-scale值达到移动端适配const setViewport = function () { //计算当前屏幕的宽度与设计稿比例 let scale = window.screen.width / DESIGN_WIDTH; // 获取元素 let meta = document.querySelector("meta[name=viewport]"); let content = `width=${DESIGN_WIDTH}, initial-scale=${scale}, maximum-scale=${scale}, minimum-scale=${scale}`; // 判断是否已存在 if (!meta) { meta = document.createElement("meta"); meta.setAttribute("name", "viewport"); document.head.appendChild(meta); } meta.setAttribute("content", content);};setViewport();// 监听屏幕变化事件window.addEventListener("resize", setViewport);// 监听屏幕翻转事件window.addEventListener("orientationchange", setViewport);关于"如何解决html5中rem适配与viewport适配问题"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report