本文摘自PHP中文网,作者php中世界最好的语言,侵删。
这次给大家带来手机端怎样用rem+scss做适配,手机端做rem+scss适配的注意事项有哪些,下面就是实战案例,一起来看一下。rem介绍
rem(font size of the root element)是指相对于根元素(即html元素)的字体大小的单位。
假设根元素的字体大小是10px, 则5rem的大小为 5*10=50px,例如
1 2 3 4 5 6 7 | html{
font-size: 10px;
}
p{
width: 2rem;
margin: 1rem;
}
|
rem来做适配
以前我们往往这样做页面:viewport width 设置为 device-width,然后选我们需要兼容设备的最小宽度(一般是320px)。根据这最小宽度来做页面。单位使用px和百分比。在宽度不同的设备上,页面的字体大小,内容尺寸都是一样的,不同的是,大屏的内容间的空隙比小屏的大。所以这样做的缺点就是,页面在某些尺寸的设备上显示的效果不好。
如果用rem来页面,我们会根据不同的设备宽度在根元素上设置不同的字体大小。宽度越宽,字体越大。然后对原本使用px的地方使用rem来替换。这样,字体大小,内容尺寸,对随着屏幕宽度的变大而变大。
首先js设置html的默认字体大小(写在html头部)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script type= "text/javascript" >
var bodyElement = document.documentElement || document.body,
RC = {
w: 750,
h: 1206
},
GC = {
w: document.documentElement.clientWidth || window.innerWidth || screen.width,
h: document.documentElement.clientHeight || window.innerHeight || screen.height
};
function setFontSize(){
var rightSize = parseFloat((RC.w / RC.h).toFixed(1)),
currentSize = parseFloat((GC.w / GC.h).toFixed(1)),
lastHTMLSize = 16,
html = document.getElementsByTagName( "html" )[0];
if (rightSize > currentSize){
lastHTMLSize = 16;
} else if (rightSize < currentSize){
lastHTMLSize = (RC.h / GC.h * GC.w) / RC.w * 16;
}
html.style.fontSize = GC.w / lastHTMLSize + 'px' ;
}
setFontSize();
</script>
|
设置scss文件px转rem
1 2 3 4 5 6 7 8 |
@charset "UTF-8" ;
@ function rem( $n ) {
@ return $n / (750 / 16)+rem;
}
|
编辑方便调用的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | @ function getTop( $n ) {
@ return ( $n - 1206 / 2) / (750 / 16)+rem;
}
@ function getLeft( $n ) {
@ return ( $n - 750 / 2) / (750 / 16)+rem;
}
@ function getRight( $n ) {
@ return (( $n - 750) / 2) / (750 / 16)+rem;
}
@mixin center( $left , $top ) {
position: absolute;
left: 50%;
top: rem( $top );
margin: 0 0 0 getLeft( $left );
}
@mixin centerlt( $left , $top ) {
position: absolute;
left: 50%;
top: 50%;
margin: getTop( $top ) 0 0 getLeft( $left );
}
@mixin centerrt( $right , $top ) {
position: absolute;
right: 50%;
top: 50%;
margin: getTop( $top ) getRight( $right ) 0 0;
}
@mixin middlert( $right , $top ) {
position: absolute;
right: rem( $right );
top: 50%;
margin: getTop( $top ) 0 0 0;
}
@mixin centerb( $left , $bottom ) {
position: absolute;
left: 50%;
bottom: rem( $bottom );
margin: 0 0 0 getLeft( $left );
}
@mixin leftTop( $left , $top ) {
position: absolute;
left: rem( $left );
top: rem( $top );
}
@mixin rightTop( $right , $top ) {
position: absolute;
right: rem( $right );
top: rem( $top );
}
@mixin leftBottom( $left , $bottom ) {
position: absolute;
left: rem( $left );
bottom: rem( $bottom );
}
|
调用上面的函数(宽高距离用ps量实际距离即可,默认设计稿宽750):
1 2 3 4 5 | page1-img1{
width: rem(473);
height: rem(173);
@ include centerlt(139, 767);
}
|
相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
html5怎样做出图片转圈的动画效果
用H5的WebGL如何在同一个界面做出json和echarts图表
H5的语义化标签新特性应该如何使用
以上就是手机端怎样用rem+scss做适配的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
css怎么给图片添加两个边框
jq怎么恢复原来的css样式
css改变滚动条样式
css三个字如何和两个字对齐
html与css中的3d转换模块
给元素设置圆角半径的css属性是什么
css怎么设置字体大小
值得收藏的26个css面试题,增强你的css基础!
详解angular中为html元素添加css类的几种方式
使用纯css画一个圆环(代码示例)
更多相关阅读请进入《rem+scss》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 手机端怎样用rem+scss做适配