个人收藏的移动端网页布局rem解决方案
2016-12-15 17:36
543 查看
写移动端项目时,总是会纠结是用css3 media query 还是用rem。移动端框架挺多,但是因为项目都比较小,不考虑使用。
无意在网上找到一个移动端rem布局的解决方案,经个人实践,目前未出现什么大问题,收藏备用。
无意在网上找到一个移动端rem布局的解决方案,经个人实践,目前未出现什么大问题,收藏备用。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>移动端rem布局</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" /> <style> .test{ width: 1rem; height: 1rem; background-color: #fa5275; } </style> </head> <body> <div class="test"></div> <p>设置前html的fontsize:16px;</p> <p>设置前html的fontsize:16px;</p> <script> function adapt(designWidth, rem2px){ var d = window.document.createElement('div'); d.style.width = '1rem'; d.style.display = "none"; var head = window.document.getElementsByTagName('head')[0]; head.appendChild(d); var defaultFontSize = parseFloat(window.getComputedStyle(d, null).getPropertyValue('width')); d.remove(); document.documentElement.style.fontSize = window.innerWidth / designWidth * rem2px / defaultFontSize * 100 + '%'; var st = document.createElement('style'); var portrait = "@media screen and (min-width: "+window.innerWidth+"px) {html{font-size:"+ ((window.innerWidth/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}"; var landscape = "@media screen and (min-width: "+window.innerHeight+"px) {html{font-size:"+ ((window.innerHeight/(designWidth/rem2px)/defaultFontSize)*100) +"%;}}" st.innerHTML = portrait + landscape; head.appendChild(st); return defaultFontSize; } var defaultFontSize = adapt(640, 100); </script> </body> </html>
相关文章推荐
- rem布局--移动端页面自适应解决方案
- 移动端自适应布局解决方案——rem
- 移动端网页 rem 自适应布局
- 移动端网页 rem 自适应布局
- 移动端网页fixed布局问题解决方案
- 移动端页面自适应解决方案 rem布局
- 移动端rem布局雪碧图解决方案 以及分享腾讯团队的在线雪碧图工具
- 手机端页面自适应解决方案-rem布局
- 移动端网页布局中需要注意事项以及解决方法总结
- html rem布局,在移动端超级好用,自适应效果非常好!
- 关于移动端rem 布局的一些总结
- webapp新体验Rem实现移动端网页适配详解资源
- 基于rem的移动端自适应解决方案
- 基于rem的移动端自适应解决方案
- 超好用的移动端布局自适应大小rem判断js文件及超过一定高度回到顶部按钮代码
- 始于移动端网页布局
- Web移动端Fixed布局的解决方案(原文出处:http://efe.baidu.com/blog/mobile-fixed-layout)
- 移动端rem布局
- Web移动端Fixed布局的解决方案
- 移动端rem布局的理解