结合sass实现动态转换rem
scss
安装:npm install node-sass
编译:node-sass a.scss b.css
生产环境当中,一般使用压缩选项。
sass --style compressed nav.sass nav.css
也可以让SASS监听某个文件或目录,一旦源文件有变动,就自动生成编译后的版本。
sass --watch nav.scss:nav.css
rem的基准值就是html的fontSize
以iphone6为标准,width为375px,那么rem基准值就是37.5px,用想要的像素值除以这个标准就可以了 。
rem页面适配实战
npm install node-scss node-sacc a.scss b.css
@function px3rem($px){
$rem : 37.5px;
@return ($px / $rem) +rem;
}
var htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
var htmlDom = docuement.getElementsByTagName("html")[0];
htmlDom.style.fontSize = htmlWidth / 10 +"px";
rem适配页面
与css结合使用
rem进阶
引入scss工具,编写样式,实现动态计算font-size;
其中,&表示父级元素

1,启动自适应浏览器宽度
<meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
2,设置默认rem的字体大小
<html> font-size:16px;</html>
3,设置宽度规则
@media screen and (max-width:320px;) {
/* css */
}
@media screen and (min-width:321px;) {
/* css */
}
4,动态设置高度
@media only screen and (min-width:321px) {html {font-size:62.5%!important}}
@media only screen and (min-width:361px) {html {font-size:70.31%!important}}
@media only screen and (min-width:376px) {html {font-size:73.24%!important}}
@media only screen and (min-width:481px) {html {font-size:94%!important}}
@media only screen and (min-width:561px) {html {font-size:109%!important}}
@media only screen and (min-width:641px) {html {font-size:125%!important}}
html{font-size:62.5%; font-size:10px;}
@function px2rem($px){
$rem:37.5px;
@return($px/$rem)+rem;
}
.hello{
width:px2rem(100px);
height:px2rem(100px);
&.b{
width:px2rem(50px);
height:px2rem(50px);
}
}
基准值就是html根元素的font-size
rem的数值就是换算出的px的rem值,1rem如果为16px,那么170px就等于170/16rem
rem进阶知识大纲
//获取视窗宽度
let htmlWidth = document.documentElement.clienWidth || document.baody.clienWidth;
//获取高度高度
let htmlDom = document.getElementsByTagName('html')[0];
htmlDom.style.fontsize = htmlDom / 10 +'px';
sass动态计算rem数值
进阶:
rem 基准值的计算
基准值指的就是html的font-size
rem 数值计算与构建
元素实际值除以基准值就等于rem数值
rem 与 scss结合使用
rem 适配实战
scss计算编译出rem