经过一周的逆战学习,知道了移动端布局,下面是我的个人经验:
所谓rem布局,就是想办法去动态改变html标签的font-size大小,这样就可以适配不同的设备进行等比缩放,从而有效地适应各种移动端。
了解rem布局需要了解rem单位,我么可以从最开始的em单位来说,em单位是一个相对单位,1em等于当前元素或父元素的font-size值,而rem是相对于根元素的font-size值,
相当于1rem = 1个根元素的font-size大小 就是 1个html标签的font-size大小
知道上述概念后,我们就知道,需要让页面适配移动端,就应该添加页面html的font-size值,使其转换单位,适配移动端:
我们动态改变html的方法有两种:
1.通过JS (JavaScript)
<script>
var html = document.getElementsByTagName('html')[0];
var number = document.documentElement.clientWidth / 3.75;
html.style.fontSize = number + 'px';
</script>
因为iphone6,等移动端界面不容易换算,让其变为100,方便用户更容易换算
2.通过vw (vw是尺寸单位)相对单位:表示把屏幕自动分成了100vw宽和100vh高
vw -> view width
vh -> view height
vw / vh : 把屏幕分为100份,1vw等于屏幕宽的1%。
意思是当屏幕为375时,则100vw就等于375px
若屏幕为414时,则100vw就等于414px
那在屏幕为375px时,1vw就等于3.75px,那么100px就等于26.66667vw,
则在html处写上html{font-size:26.66667vw},然后就可以自己写页面了,将页面在375px屏幕下的距离以及字号,包括px相关单位,测量出来,然后通过100px=26.66667vw,换算出对应的vw单位值,简单方法:通过vscode编码工具中的一个插件 -> px to rem -> 在设置把对应的font-size设置为你的换算单位值,这就是100。-> alt + z进行px转rem的操作。
注:
1.当设计师给你的设计图在375px以上可以通过ps的画布大小调整为375px,如果设计给的时一套图,那么就根据设计师的图来做,做完之后在来调整换算单位,假如设计师给的时750px的,那么我们就可以通过375px为100vw,那么750px为多少?vw,然后将其作为工具的换算单位,按下alt+z就可以实现rem布局
2.在html设置了font-size的情况下,我们需要将html重置一下,防止页面布局混乱,我们可以在html下加一个body{font-size=16px}
通过以上内容,我做的一个小案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./font_1660555_9nezdjkvhf8/iconfont.css">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html{
font-size: 26.66667vw;
}
body{
font-size: 16px;
}
ul{
list-style: none;
}
img{display: block;}
a{
text-decoration: none;
}
header{
width: 3.75rem;
height: 0.44rem;
display: flex;
justify-content: center;
align-items: center;
color: #fefefe;
background-color: #02d1c5;
font-size: 0.17rem;
}
.nav{
width: 3.75rem;
height: 0.66rem;
background-color: #fff;
display: flex;
justify-content: center;
align-items: center;
}
.nav input{
width: 2.5rem;
height: 0.25rem;
display: block;
outline: none;
border: none;
border-radius: 0.025rem;
background-color: #f3f3f3;
text-indent: 0.35rem;
}
.nav .icon-fangdajing{
font-size: 0.15rem;
position: relative;
left: 0.25rem;
color: #ababab;
}
.banner{
width: 3.75rem;
height: 1.5rem;
overflow: hidden;
position: relative;
}
.banner .banner-pic{
width: 100%;
height: 100%;
}
.banner .banner-pic li{
width: 100%;
height: 100%;
float: left;
}
.banner .banner-pic img{
width: 100%;
height: 100%;
}
.banner .banner-dot{
height: 0.05rem;
font-size: 0rem;
margin-left: -0.145rem;
text-align: center;
position: absolute;
left: 50%;
bottom:0.165rem;
}
.banner .banner-dot li{
width: 0.03rem;
height: 0.03rem;
margin-right: 0.03rem;
border: 0.01rem solid #fff;
border-radius: 50%;
float: left;
}
.banner .banner-dot .active{
background-color: #fff;
}
.tab{
width: 3.75rem;
height: 0.87rem;
display: flex;
justify-content:space-evenly;
align-items: center;
}
.tab .tab_menu{
width: 1.65rem;
height: 0.5rem;
box-shadow: 0.025rem 0.025rem 0.025rem #e9e8e8;
border-radius: 0.025rem;
}
.tab .tab_menu a{
width: 100%;
height: 100%;
display: flex;
align-items: center;
}
.tab .tab_menu a span{
font-size: 0.25rem;
color: #23d0c4;
margin-left: 0.245rem;
margin-right: 0.18rem;
}
.tab .tab_menu a p{
color: #23d0c4;
font-size: 0.15rem;
margin-right: 0.245rem;
}
.list{
width: 3.45rem;
margin: 0rem 0.15rem;
margin-bottom: 0.66rem;
}
.list h2{
width: 100%;
height: 0.19rem;
line-height: 0.19rem;
font-size: 0.17rem;
color: #666666;
text-indent: 0.35rem;
}
.list .list-sublist li{
width: 100%;
height: 1.25rem;
margin: 0.08rem auto;
overflow: hidden;
background-position: center center;
background-size: cover;
position: relative;
border-radius: 0.05rem;
}
.list .list-sublist span{
display: block;
width: 100%;
height: 1.25rem;
background-color: #000;
opacity: 0.5;
line-height: 1.25rem;
text-align: center;
color: #fff;
font-size: 0.18rem;
position: absolute;
left: 0;
top: 0;
}
.footer{
width: 3.75rem;
height: 0.49rem;
background-image: linear-gradient(#f2f2f2 5%,#fff);
position: fixed;
bottom: 0;
}
.footer .footer-list{
width: 100%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
}
.footer .footer-list li:nth-of-type(3){
width: 0.495rem;
height: 0.495rem;
border-radius: 50%;
background-color: #02d1c5;
margin-bottom: 0.5rem;
display: flex;
justify-content: center;
align-items: center;
}
.footer .footer-list li:nth-of-type(3) span{
color: #fff;
}
.footer .footer-list li span{
font-size: 0.245rem;
color: #666666;
}
.footer .footer-list li p{
font-size: 0.13rem;
color: #666;
}
.footer .footer-list .active p{
color: #02d1c5;
}
.footer .footer-list .active span{
color: #02d1c5;
}
</style>
</head>
<body>
<header>
食谱推荐
</header>
<div class="nav">
<span class="iconfont icon-fangdajing"></span><input type="text" name="" id="">
</div>
<div class="banner">
<ul class="banner-pic">
<li><img src="./images/1.png" alt=""></li>
<li><img src="./images/1.png" alt=""></li>
<li><img src="./images/1.png" alt=""></li>
<li><img src="./images/1.png" alt=""></li>
</ul>
<ul class="banner-dot">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<div class="tab">
<div class="tab_menu">
<a href="./list1.html">
<span class="iconfont icon-jingyu7"></span>
<p>三餐推荐</p>
</a>
</div>
<div class="tab_menu">
<a href="./list.html">
<span class="iconfont icon-cailanzi"></span>
<p>食物库</p>
</a>
</div>
</div>
<div class="list">
<h2>精品好菜</h2>
<ul class="list-sublist">
<li style="background-image: url(./images/list1.png);">
<a href="./content.html">
<span>金枪鱼的幻想曲</span>
</a>
</li>
<li style="background-image: url(./images/list2.png);">
<a href="">
<span>早餐的诱惑</span>
</a>
</li>
<li style="background-image: url(./images/list2.png);">
<a href="">
<span>进击的鸡小胸</span>
</a>
</li>
<li style="background-image: url(./images/list2.png);">
<a href="">
<span>进击的鸡小胸</span>
</a>
</li>
<li style="background-image: url(./images/list2.png);">
<a href="">
<span>进击的鸡小胸</span>
</a>
</li>
</ul>
</div>
<div class="footer">
<ul class="footer-list">
<li>
<a href="">
<span class="iconfont icon-home"></span>
<p>首页</p>
</a>
</li>
<li class="active">
<a href="">
<span class="iconfont icon-shipu"></span>
<p>食谱</p>
</a>
</li>
<li>
<a href="">
<span class="iconfont icon-addTodo-nav"></span>
</a>
</li>
<li>
<a href="">
<span class="iconfont icon-faxian"></span>
<p>发现</p>
</a>
</li>
<li>
<a href="">
<span class="iconfont icon-wode"></span>
<p>我的</p>
</a>
</li>
</ul>
</div>
</body>
</html>