移动端采用rem适配非常方便

比如在iphone6尺寸下,将html font-size 设置为 100px,那么写css时,只要将尺寸/100 + rem 即可。

在iphone6Plus尺寸下,html font-size会自动调节,兼容多种尺寸的手机

以下是js代码,复制到你的项目中即可使用

(function(win) {
var ratio, scaleValue, renderTime,
htmlEle = document.documentElement,
vpMeta = document.querySelector('meta[name="viewport"]'); if (vpMeta) {
var tempArr = vpMeta.getAttribute("content").match(/initial\-scale=(["']?)([\d\.]+)\1?/);
if (tempArr) {
scaleValue = parseFloat(tempArr[2]);
ratio = parseInt(1 / scaleValue);
}
} else {
vpMeta = document.createElement("meta");
vpMeta.setAttribute("name", "viewport");
vpMeta.setAttribute("content", "width=device-width, initial-scale=0.5, user-scalable=no, minimal-ui");
htmlEle.firstElementChild.appendChild(vpMeta);
ratio = 2;
} win.addEventListener("resize", function() {
clearTimeout(renderTime);
renderTime = setTimeout(initPage, 300);
}, false); win.addEventListener("pageshow", function(e) {
if(e.persisted){
clearTimeout(renderTime);
renderTime = setTimeout(initPage, 300)
}
}, false); if("complete" === document.readyState){
document.body.style.fontSize = 12 * ratio + "px";
}else{
document.addEventListener("DOMContentLoaded", function() {
document.body.style.fontSize = 12 * ratio + "px";
}, false);
} initPage(); function initPage() {
var htmlWidth = htmlEle.getBoundingClientRect().width;
htmlWidth / ratio > 768 && (htmlWidth = 768 * ratio);
win.rem = 100 * (htmlWidth / 375);
htmlEle.style.fontSize = win.rem + "px";
}
})(window);

代码分析

如果你设置了meta标签的视口属性,则获取initial-scale缩放比例,如果没设置,则自动添加。

一般initial-scale为1

  • line 2 获取屏幕宽度
  • line 3 如果宽度超过768(ipad平板宽度),则不再进行调节
  • line 4、5 设置rem,我以iphone6宽度375设置的,在该尺寸下,rem=100px,如果是其他尺寸,修改375即可

(htmlWidth/375)得到的是缩放比例,在IPHONE6下计算时,不用管html的font-size,直接px/100即算出rem

  • 当页面改变尺寸,或者初次显示的时候,执行方法
  • persisted是pageshow事件的属性,检测浏览器是否读取缓存,是的话为true

当页面渲染完后,设置body html-size,防止使用默认样式的元素出错

自动改变html font-size,实现移动端rem适配的更多相关文章

  1. 谈谈我的移动端rem适配方案

    最近有点怀疑人生,毕竟一个人写前端,有时候会怀疑自己理解的一些东西包括用法有没有符合标准.趁着这阵子闲下来,翻了翻别人的rem适配博客,发现有点绕口,怪自己是个强迫症,啥都要自己去试试结果并从中理解, ...

  2. 07. 如何实现移动端rem适配

    如何实现移动端rem适配 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  3. 移动端rem适配&iOS兼容

    移动端rem适配js // 默认375,750设计稿请将375替换为750 (function (doc, win) { // 移动端适配 var docEl = doc.documentElemen ...

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

    H5 端 rem 适配方案与 viewport 适配 rem rem 是 CSS3 新增的一个相对单位(root em,根 em) 只根据当前页面 HTML 页面的 font-size 设置,如果根目 ...

  5. 移动端rem适配屏幕

    九月已成历史,十月如期而至...可能是九月工作比较清闲,周记就没怎么写,十月决不能这么堕落,立贴为证,至少保证5篇博客!!!如果没学到什么新知识,就对以往的那些工作中常用到的知识点做个总结...话不多 ...

  6. 移动端 rem适配方法

    rem适配 一, 网易适配方法         屏幕宽度/设计稿rem宽度=页面动态font-size值(如:375/7.5=50)         document.documentElement. ...

  7. H5移动端rem适配

    /** * 移动端自适应 */ <meta name="viewport" content="width=device-width,user-scalable=no ...

  8. vue中使用第三方UI库的移动端rem适配方案

    需求:使用vue-cli脚手架搭建项目,并且使用第三方的UI库(比如vant,mint ui)的时候,因为第三方库用的都是用px单位,无法使用rem适配不同设备的屏幕. 解决办法:使用px2rem-l ...

  9. 关于移动端rem适配

    var num = 1 / window.devicePixelRatio; var fontSize = document.documentElement.clientWidth / 10; doc ...

随机推荐

  1. linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql.

    linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql. 2013-03-04 1 ...

  2. 警告Conversion specifies type'int' but the argument has type'size_t'

    代码: #import<Foundation/Foundation.h> int main(int argc,const char * argv[]){ const char *words ...

  3. 一种用XAML写Data Converter的方式

    在WPF程序中,数据绑定是非常常用的手段.伴随着数据绑定,我们通常还需要编写一些Converter.而编写Converter是一件非常枯燥的事情,并且大量的converter不容易组织和维护. 今天在 ...

  4. 设置并删除Dreamweaver自动生成的_notes文件夹

    在使用Dreamweaver做项目时站点下面的每个文件夹里面都会自动生成一个_notes文件夹,删除之后马上又会再次生成.最近做项目时,有童鞋一不小心把所有的_notes文件夹全部存回到SVN上面了, ...

  5. 【spring Boot】2.在Myecplise上把spring Boot项目打包 war包和jar包

    ========================================================第一部分======================================== ...

  6. ansible的inventory文件含义

    默认文件为/etc/ansible/hosts 例如 [test] web.yinzhipeng.com dhcp ansible_ssh_host=172.16.18.195 1.中括号中的名字代表 ...

  7. LNMP第一部分环境搭建

    1. MySQL安装(同LAMP里面的安装方法)2.  php安装wget  http://cn2.php.net/distributions/php-5.4.37.tar.bz2tar jxf ph ...

  8. 数据库学习--wildfly配置postgreSQL数据源

    前言 实验室最近在做一个物品管理系统的小项目,其中涉及到postgreSQL的使用,刚开始部署到wildfly服务器上时遇到了若干问题,终于在导师的讲解下大体上明白了 ,特此记录分享学习一下. 配置数 ...

  9. 拉格朗日对偶与kkt条件

  10. ASIHttpRequest请求时的默认编码

    在ASIHttpRequest.m文件 中的 - (id)initWithURL:(NSURL *)newURL方法中找到 [self setDefaultResponseEncoding:NSISO ...