Day05-移动适配
一、长度单位
1.1 rem
目标:能够使用rem单位设置网页元素的尺寸
网页效果:屏幕宽度不同,网页元素尺寸不同(等比缩放)
px单位或百分比布局可以实现吗?
1.px单位是绝对单位
2.百分比布局特点宽度自适应,高度固定
适配方案
1.rem
2.vw / vh
rem单位:
1.相对单位
2.rem单位是相对于HTML标签的字号计算结果
3.1rem = 1HTML字号大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rem基本使用</title>
<style>
* {
margin: 0;
padding: 0;
}
/* 1rem = 1html标签字号大小 */
html {
font-size: 20px;
}
.box {
width: 5rem;
height: 3rem;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
2.1 rem移动适配 - 媒体查询
目标:能够使用媒体查询设置差异化CSS样式
写法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 使用媒体查询, 根据不同的视口宽度, 设置不同的跟字号 */
@media (width:375px) {
html {
font-size: 40px;
}
}
@media (width:320px) {
html {
font-size: 30px;
}
}
</style>
</head>
<body>
</body>
</html>
3.1 rem布局方案
目标:能够使用rem单位设置网页元素的尺寸
设备宽度不同,HTML标签字号设置多少合适?
设备宽度大, 元素尺寸大
设备宽度小,元素尺寸小
目前rem布局方案中,将网页等分成10份, HTML标签的字号为视口宽度的 1/10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rem适配</title>
<style>
* {
margin: 0;
padding: 0;
}
/* 1. 不同的视口,HTML标签字号不同, 字号是视口宽度的1/10 */
@media (width:320px) {
html {
font-size: 32px;
}
}
@media (width:375px) {
html {
font-size: 37.5px;
}
}
@media (width:414px) {
html {
font-size: 41.4px;
}
}
/* 2. 书写盒子的尺寸,单位rem */
.box {
width:5rem;
height: 3rem;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
4.1 rem适配原理
目标:实现在不同宽度的设备中,网页元素尺寸等比缩放效果
查看设计稿宽度 → 确定参考设备宽度(视口宽度) → 确定基准根字号(1/10视口宽度)
rem单位的尺寸 = px单位数值 / 基准根字号
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rem适配</title>
<style>
* {
margin: 0;
padding: 0;
}
/* 1. 不同的视口,HTML标签字号不同, 字号是视口宽度的1/10 */
@media (width:320px) {
html {
font-size: 32px;
}
}
@media (width:375px) {
html {
font-size: 37.5px;
}
}
@media (width:414px) {
html {
font-size: 41.4px;
}
}
/* 2. 书写盒子的尺寸,单位rem */
.box {
width:5rem;
height: 3rem;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
5.1 flexible
目标:使用flexible js配合rem实现在不同宽度的设备中,网页元素尺寸等比缩放效果
思考:咱们检测了3个视口,分别设置了根字号,有什么弊端吗?
答:手机设备多,屏幕尺寸不一,视口不仅仅有这3个
flexible.js是手淘开发出的一个用来适配移动端的js框架。
核心原理就是根据不同的视口宽度给网页中html根节点设置不同的font-size。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>rem开发流程</title>
<!-- <link rel="stylesheet" href="./demo.css"> -->
</head>
<body>
<div class="box"></div>
<script src="../js/flexible.js"></script>
</body>
</html>
二、Less
1.1 目标:使用Less运算写法完成px单位到rem单位的转换
思考:在px单位转换到rem单位过程中,哪项工作是最麻烦的?
答:除法运算。CSS不支持计算写法。
解决方案:可以通过Less实现。
2.1 目标:使用Less语法快速编译生成CSS代码
Less是一个CSS预处理器, Less文件后缀是.less
扩充了 CSS 语言, 使 CSS 具备一定的逻辑性、计算能力。
注意:浏览器不识别Less代码,目前阶段,网页要引入对应的CSS文件。
Easy Less : vscode插件
作用:less文件保存自动生成css文件
注释:
1.单行注释
语法:// 注释内容
快捷键:ctrl + /
2.块注释
语法:/* 注释内容 */
快捷键: shift + alt + A
3.1 使用Less运算写法完成px单位到rem单位的转换
运算:
1.加、减、乘直接书写计算表达式
2.除法需要添加 小括号 或 .
注意:表达式存在多个单位以第一个单位为准
4.1 目标:能够使用Less嵌套写法生成后代选择器
作用:快速生成后代选择器。
语法:
注意:&不生成后代选择器,表示当前选择器,通常配合伪类或伪元素使用
5.1 目标:能够使用Less变量设置属性值
语法:
定义变量:@变量名: 值;
使用变量:CSS属性:@变量名;
6.1 目标:能够使用Less导入写法引用其他Less文件
导入: @import “文件路径”;
7.1 目标:使用Less语法导出CSS文件(改变css文件的位置)
方法一:配置EasyLess插件, 实现所有Less有相同的导出路径
配置插件: 设置 → 搜索EasyLess → 在setting.json中编辑 → 添加代码(注意,必须是双引号)
方法二:控制当前Less文件导出路径
Less文件第一行添加如下代码, 注意文件夹名称后面添加 /
加粗样式
8.1 禁止导出
在less文件第一行添加: // out: false
三、游乐园项目(代码未完全贴出)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FC游乐园</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="./lib/iconfont/iconfont.css">
<link rel="stylesheet" href="./css/index.css">
</head>
<body>
<!-- 主体内容 -->
<div class="main">
<!-- banner -->
<div class="banner">
<ul>
<li><a href="#"><img src="./uploads/banner_1.png" alt=""></a></li>
</ul>
</div>
<!-- 活动标题 -->
<div class="title">
<h4>乐园活动</h4>
</div>
<!-- 活动 -->
<section class="item">
<div class="pic">
<a href="#"><img src="./uploads/item_2.png" alt=""></a>
<!-- 收藏图标 -->
<i class="iconfont icon-shoucang1"></i>
<!-- 活动状态 -->
<div class="active off">进行中</div>
</div>
<div class="txt">
<div class="top">
<h5>疯狂的红包 不一样的撕名牌 大型家庭亲子户外活动</h5>
<p>免费</p>
</div>
<div class="bottom">
<p>
<i class="iconfont icon-qizhi"></i>
<span>200</span>人已报名
</p>
<p>
<i class="iconfont icon-shizhong"></i>
<span>本周六</span>开始
</p>
</div>
</div>
</section>
<section class="item">
<div class="pic">
<a href="#"><img src="./uploads/item_2.png" alt=""></a>
<!-- 收藏图标 -->
<i class="iconfont icon-shoucang1"></i>
<!-- 活动状态 -->
<div class="active off">进行中</div>
</div>
<div class="txt">
<div class="top">
<h5>疯狂的红包 不一样的撕名牌 大型家庭亲子户外活动</h5>
<p>免费</p>
</div>
<div class="bottom">
<p>
<i class="iconfont icon-qizhi"></i>
<span>200</span>人已报名
</p>
<p>
<i class="iconfont icon-shizhong"></i>
<span>本周六</span>开始
</p>
</div>
</div>
</section>
<section class="item">
<div class="pic">
<a href="#"><img src="./uploads/item_2.png" alt=""></a>
<!-- 收藏图标 -->
<i class="iconfont icon-shoucang1"></i>
<!-- 活动状态 -->
<div class="active off">进行中</div>
</div>
<div class="txt">
<div class="top">
<h5>疯狂的红包 不一样的撕名牌 大型家庭亲子户外活动</h5>
<p>免费</p>
</div>
<div class="bottom">
<p>
<i class="iconfont icon-qizhi"></i>
<span>200</span>人已报名
</p>
<p>
<i class="iconfont icon-shizhong"></i>
<span>本周六</span>开始
</p>
</div>
</div>
</section>
<section class="item">
<div class="pic">
<a href="#"><img src="./uploads/item_2.png" alt=""></a>
<!-- 收藏图标 -->
<i class="iconfont icon-shoucang1"></i>
<!-- 活动状态 -->
<div class="active off">进行中</div>
</div>
<div class="txt">
<div class="top">
<h5>疯狂的红包 不一样的撕名牌 大型家庭亲子户外活动</h5>
<p>免费</p>
</div>
<div class="bottom">
<p>
<i class="iconfont icon-qizhi"></i>
<span>200</span>人已报名
</p>
<p>
<i class="iconfont icon-shizhong"></i>
<span>本周六</span>开始
</p>
</div>
</div>
</section>
</div>
<!-- 主体内容 -->
<!-- 底部工具栏 -->
<footer>
<a href="#" class="current">
<i class="iconfont icon-index-copy"></i>
<p>首页</p>
</a>
<a href="#">
<i class="iconfont icon-index-copy"></i>
<p>首页</p>
</a>
<a href="#">
<i class="iconfont icon-index-copy"></i>
<p>首页</p>
</a>
</footer>
<!-- 底部工具栏 -->
<script src="./js/flexible.js"></script>
</body>
</html>
index.less
@import './base.less';
@import './normalize.less';
// 变量:存储37.5
@rootSize:37.5rem;
body {
background-color: #F0F0F0;
}
// 主体内容
.main {
// padding-bottom: (50 / 37.5rem);
padding-bottom: (50 / @rootSize);
// banner
.banner {
height: (160 / @rootSize);
}
// 活动标题
.title {
height: (40 / @rootSize);
line-height: (40 / @rootSize);
padding-left: (15 / @rootSize);
h4 {
font-size: (14 / @rootSize);
color: #3C3C3C;
}
}
// 活动
.item {
margin-bottom: (10 / @rootSize);
// 图
.pic {
position: relative;
height: (160 / @rootSize);
// 收藏图标
i {
position: absolute;
top: (15 / @rootSize);
right: (18 / @rootSize);
font-size: (24 / @rootSize);
color: #fff;
}
// 活动状态
.active {
position: absolute;
left: (15 / @rootSize);
top: (-4 / @rootSize);
width: (68 / @rootSize);
height: (29 / @rootSize);
background-image: url(../images/status_active.png);
background-size: contain;
font-size: (12 / @rootSize);
color: #fff;
text-align: center;
line-height: (25 / @rootSize);
// 灰色图片样式
// .active.off {} 交集选择器:必须是active类同时应用了off类
&.off {
background-image: url(../images/status_default.png);
}
}
}
// 文字
.txt {
padding: (10 / @rootSize) (15 / @rootSize);
background-color: #fff;
.top {
display: flex;
justify-content: space-between;
h5 {
width: (290 / @rootSize);
font-size: (15 / @rootSize);
color: #3C3C3C;
font-weight: normal;
}
p {
font-size: (15 / @rootSize);
color: #FE6243;
}
}
.bottom {
display: flex;
margin-top: (10 / @rootSize);
p {
margin-right: (15 / @rootSize);
font-size: (11 / @rootSize);
i {
font-size: (11 / @rootSize);
}
color: #B4B4B4;
}
}
}
}
}
// 底部工具栏
footer {
position: fixed;
left: 0;
bottom: 0;
display: flex;
justify-content: space-around;
align-items: center;
width: 100%;
// height: (50 / 37.5rem);
height: (50 / @rootSize);
background-color: #FECA49;
text-align: center;
a {
font-size: (11 / @rootSize);
color: #D78B09;
.iconfont {
font-size: (24 / @rootSize);
}
&.current {
color: #fff;
}
}
}