移动端rem适配布局
dome如下:
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>移动h5自适应布局</title>
<style>
/*320px布局*/
html{font-size: 100px;}
body{font-size: 0.14rem;/*实际相当于14px*/}
body{
padding:0;
margin:0;
}
.div2{
font-size:0.14rem;
}
.div3{
width:3rem;
height:3rem;
border:1px solid #000;
box-sizing:border-box;
}
.div4{
margin-top:0.1rem;
width:2rem;
height:2rem;
border:1px solid #000;
box-sizing:border-box;
}
.img1{
width:3.2rem;
}
</style>
</head>
<body>
<div class="div2">动态更改html元素大小</div>
<div class="div3"></div>
<div class="div4"></div>
<img class="img1" src="http://www.baidu.com/img/bdlogo.png" alt="" />
</body>
<script>
// 该代码来自http://www.ghugo.com/mobile-h5-fluid-layout-for-iphone6/
(function (doc, win) {
// 分辨率Resolution适配
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
docEl.style.fontSize = 100 * (clientWidth / 320) + 'px';
};
// Abort if browser does not support addEventListener
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
// 一物理像素在不同屏幕的显示效果不一样。要根据devicePixelRatio来修改meta标签的scale,要注释上面的meta标签
(function(){
return;
var dpr = scale =1;
var isIPhone = win.navigator.appVersion.match(/iphone/gi);
var devicePixelRatio = win.devicePixelRatio;
if (isIPhone) {
// iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
dpr = 3;
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
dpr = 2;
} else {
dpr = 1;
}
} else {
// 其他设备下,仍旧使用1倍的方案
dpr = 1;
}
scale = 1 / dpr;
//
var metaEl = "";
metaEl = doc.createElement('meta');
metaEl.setAttribute('name', 'viewport');
metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
if (docEl.firstElementChild) {
docEl.firstElementChild.appendChild(metaEl);
} else {
var wrap = doc.createElement('div');
wrap.appendChild(metaEl);
doc.write(wrap.innerHTML);
}
})();
})(document, window);
</script>
</html>
使用说明: 拿到320的设计图按照标注尺寸大小除以100,即可得到rem值,如果是640的设计图则除以200,即可得到rem值;
docEl.style.fontSize = 100 * (clientWidth / 320) + 'px';
你可以把上面的dome拷下来,完了自己改动改动 上述代码中的 320 和100 两个值试试,
前者是换算比,后者是设计图尺寸;
要用的话就把这个js拷下来,作为一个单独的js插件引入到你的html中,按着比例换算即可
css 里面一些好的东西:
字体不换行 - white-space:nowrap;
超出部分以省略号显示 - text-overflow:ellipsis; 配合 overflow: hidden;使用
字体和图片在一行的时候字体和图片居中对接 - vertical-align:middle;
原文献:http://www.cnblogs.com/samwu/p/4285748.html
移动端rem适配布局的更多相关文章
- H5 端 rem 适配方案与 viewport 适配
H5 端 rem 适配方案与 viewport 适配 rem rem 是 CSS3 新增的一个相对单位(root em,根 em) 只根据当前页面 HTML 页面的 font-size 设置,如果根目 ...
- 谈谈我的移动端rem适配方案
最近有点怀疑人生,毕竟一个人写前端,有时候会怀疑自己理解的一些东西包括用法有没有符合标准.趁着这阵子闲下来,翻了翻别人的rem适配博客,发现有点绕口,怪自己是个强迫症,啥都要自己去试试结果并从中理解, ...
- 07. 如何实现移动端rem适配
如何实现移动端rem适配 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 移动端rem适应布局
移动端rem适应布局 rem rem(root em)是一个相对单位,类似于em,em是父元素字体大小. 不同的是rem的基准是相对于html元素的字体大小. 比如,根元素(html)设置font-s ...
- 移动端rem适配&iOS兼容
移动端rem适配js // 默认375,750设计稿请将375替换为750 (function (doc, win) { // 移动端适配 var docEl = doc.documentElemen ...
- 移动web开发之rem适配布局
移动web开发之rem适配布局 方案: 页面布局文字能否随着屏幕大小变化而变化 流式布局和flex布局主要针对于宽度布局,那高度如何布局? 怎样让屏幕发生变化的时候元素高度和宽度等比例缩放? 1. r ...
- H5移动端rem适配
/** * 移动端自适应 */ <meta name="viewport" content="width=device-width,user-scalable=no ...
- 关于移动端rem适配
var num = 1 / window.devicePixelRatio; var fontSize = document.documentElement.clientWidth / 10; doc ...
- 移动端rem适配屏幕
九月已成历史,十月如期而至...可能是九月工作比较清闲,周记就没怎么写,十月决不能这么堕落,立贴为证,至少保证5篇博客!!!如果没学到什么新知识,就对以往的那些工作中常用到的知识点做个总结...话不多 ...
随机推荐
- mysql Column count doesn't match value count at row 1
今天执行批量插入的操作,发现报了错 mysql Column count doesn't match value count at row 1. 后来发现原因:是由于写的SQL语句里列的数目和后面的值 ...
- springboot 事务回滚
在springboot中,使用事务回滚时,添加@Transactional注解,然后在try-catch块中,发生异常时,在catch中 添加 TransactionAspectSupport.cur ...
- You have new mail in /var/spool/mail/root
centos7.5 查看邮件的方式: [root@web01 code]# yum -y install mailx #安装mailx [root@web01 code]# mail #执行mail命 ...
- Selenium Java Selection的使用
用于向具有drop-down的选择框中输入内容 new Select(new ChromeDriver().findElement(By.cssSelector(" ..."))) ...
- topcoder srm 540 div1
problem1 link 设第一个数字为$x$,那么第2到第$n$个数字都可以表示成$a+bx$的形式,其中$b=1$或者$b=-1$.然后可以求出关于$x$的一些范围,求交集即可. problem ...
- Python3基础 dict 创建字典 空字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 浅谈Log4j2日志框架及使用
目录 1.日志框架 2.为什么需要日志接口,直接使用具体的实现不就行了吗? 3.log4j2日志级别 4.log4j2配置文件的优先级 5.对于log4j2配置文件的理解 6.对于Appender的理 ...
- shell中使用echo输出时如何指定颜色
答: 请看下图:
- openwrt支持哪些c库?
答: 至2019/3/22,支持两种,一种是glibc,另一种是musl-libc(openwrt默认使用musl-libc)
- P2221 [HAOI2012]高速公路
思路 考虑每一条边的贡献,然后推式子 \[ \begin{align}&\sum_{i}V_i\times(R-i+1)\times(i-L+1)\\=&\sum_{i}V_i\lef ...