单位px 转换成 rem
<script type="text/javascript">
var oHtml = document.documentElement;
getSize();
window.onresize = function(){
getSize();
};
function getSize(){
var screenWidth = oHtml.clientWidth;
if (screenWidth < 320) {
oHtml.style.fontSize = '42.6667px';
} else if(screenWidth > 750){
oHtml.style.fontSize = '100px';
}else{
oHtml.style.fontSize = screenWidth/(750/100) + 'px';
}
}
</script> 100px = 1.0rem;
上面这种情况会出现文字太小显示浏览器默认文本大小的情况,以下这种处理方式个人感觉更好一点
# 关于px转rem ## 实现功能
根据UED给出的不同分辨率设计稿,将px转成rem ## 原理 1. 自定义html基数: font-size: 10px, 获取clientWidth 和 分辨率
2. clientWidth < 320 || clientWidth > 750 自定义大小
其他: 根据公式:clientWidth / (分辨率 / (分辨率/html基数)) ## 说明
font-size: 不推荐使用rem,会存在一定问题,可以使用@media方式解决;其他情况均可使用rem,如:width,height,line-height,margin,padding等 ## 代码 ### style.css html {
/*
10 / 16 * 100% = 62.5%
*/
font-size: 62.5%; // font-size: 10px;
} ### index.html (function() {
var oHtml = document.documentElement;
getSize();
// 当页面没有刷新单屏幕尺寸发生变化时,实时自适应。例:竖屏 转 横屏
window.onresize = function () {
getSize();
}
function getSize() {
var screenWidth = oHtml.clientWidth; // screenWidth < 320 || screenWidth > 750 时
/* 方法一: 通过媒体查询 (不是一直都需要的,处理特殊情况时需要)
html{font-size: 10px}
@media screen and (max-width:320px) {html{font-size:10px}} // 设置文本font-size时,要具体到class
@media screen and (min-width:321px) and (max-width:375px){html{font-size:11px}}
@media screen and (min-width:376px) and (max-width:414px){html{font-size:12px}}
@media screen and (min-width:415px) and (max-width:639px){html{font-size:15px}}
@media screen and (min-width:640px) and (max-width:719px){html{font-size:20px}}
@media screen and (min-width:720px) and (max-width:749px){html{font-size:22.5px}}
@media screen and (min-width:750px) and (max-width:799px){html{font-size:23.5px}}
@media screen and (min-width:800px){html{font-size:25px}}
*/
// 方法二:给出不同的值
if (screenWidth < 320) {
oHtml.style.fontSize = '10px';
} else if (screenWidth > 750) {
oHtml.style.fontSize = '20px';
} else {
// 由于设计稿分辨率的不同,这里screenWidth 所除的值也会随之修改,
// 例:当设计稿给出750像素时,oHtml.style.fontSize = screenWidth / 75 + 'px';
// 当设计稿给出375像素时
oHtml.style.fontSize = screenWidth / 37.5 + 'px';
};
}
}())
单位px 转换成 rem的更多相关文章
- hbuilder设置自动px 转换成rem
hbuilder里面有自动换算的 需要设置一下:工具–>选项–>Hbuilder–>代码助手设置.里面有个px自动转rem设置,按自己的实际情况设置就可以在每次输入px的时候有提示了 ...
- postcss 将px转换成rem vuecli3+vant+vue+postcss
1.安装 npm install postcss-pxtorem --save 2.找到postcss.config.js 默认是这样 module.exports = { "plugins ...
- 自动把网页px单位转换成rem
自动把网页px单位转换成rem 首先在你的项目开发环境中安装2个插件 然后在vue.config.js文件引入并重新启动服务器 这样就配置成功了,一起看看效果
- Vue开发中的移动端适配(px转换成vw)
1.项目根目录下,创建 .postcssrc.js 文件. 2.安装插件. -D (开发依赖) postcss-import postcss-url cssnano-preset-advanced - ...
- px转换成bp单位的工具函数
import {Dimensions} from 'react-native' //当前屏幕的高度 const deviceH = Dimensions.get('window').height // ...
- 彻底弄懂css中单位px和em,rem的区别
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 -1. IE无法调整那些使用px作为单位的字体大小: -2. 国外的大部分网站能够调 ...
- 彻底弄懂css中单位px和em,rem的区别 转的自己看
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...
- 弄懂css中单位px和em,rem的区别
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小 ...
- 【转载】彻底弄懂css中单位px和em,rem的区别
原文链接:http://www.cnblogs.com/leejersey/p/3662612.html 国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什 ...
随机推荐
- 个人翻译的cedec2010基于物理的光照
作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广 本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...
- Unity3d中C#使用指针(Unsafe)的办法(转)
近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...
- 使用 Linux 搭建 VPN
http://blog.csdn.net/catoop/article/details/7537012 VPN服务器的配置与应用 实验场景 通过将Linux配置VPN服务器允许远程计算机能够访问内网. ...
- Strong AI Versus Weak AI
Computer Science An Overview _J. Glenn Brookshear _11th Edition The conjecture that machines can be ...
- 线性表集合A=A B
大话数据结构 void union(List *a, List Lb) { int La_len, Lb_len, i; ElemType e; La_len = ListLength(La); Lb ...
- ArcGIS Server 缓存服务切图范围
win10 + Server 10.4 + ArcMap 10.4 ArcGIS Server 缓存服务分为创建服务后手动建立缓存和创建服务时同时自动建立缓存两种. 10.2帮助文档:http:/ ...
- 面向对象之struct
struct PointStruct { int pointx = 1; int pointy = 2; public PointStruct(int x, int y) { this.pointx ...
- C#窗体:关于DataGridView的数据源绑定字符串两个值得注意的问题
无意间遇到的问题,然后就GOOGLE了下,搜到些资料,总结整理如下(注:是转载的) 1. LINQ的查询结果无法直接作为DataGridView的数据源 DataGridView的DataSource ...
- Bootstrap 表格和按钮
一.表格 1.条纹状表格 行产生一行隔一行加单色背景效果 注:表格效果需要基于基本格式.table <table class="table table-striped"> ...
- Bootstrap 一. 排版样式(内联文本元素,对齐,大小写,缩略语,地址文本,引用文本,列表排版 ,代码 )
第 2 章 排版样式 在 h1 ~ h6 元素之间,还可以嵌入一个 small 元素作为副标题 <h1>Bootstrap 框架 <small>Bootstrap 小标题< ...