文章目录
前言
Flex对于网页的布局相对于许多布局方法明显有着简便之处。所以在这篇博客文章中希望大家能够从携程网这个案例中有所收获!
一、携程移动页面端
当我们想要做一个产品的页面就要从整体到局部来进行搭建,所以先让我们来看一下携程移动页面端的大致样子吧!
由于页面较长分成两张图来展示(页面1如下):
我们将页面1分成了5大部分:search-index、focus、local-nav、nav、subnav-entry。
由于页面较长分成两张图来展示(页面2如下):
我们将页面2整个页面放在sales-box一整个大盒子里,然后里面分成两部分,sales-hd和sales-bd。
二、页面的搭建
制作页面前的准备工作
制作移动页面前我们需要将整个页面的大小控制好,以及一些网页中的默认效果清除,方便实现我们所想要的具体效果。
代码如下:
body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, "Lucida Grande",
Verdana, "Microsoft Yahei", STXihei, hei;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
}
a {
text-decoration: none;
color: #222;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
div {
box-sizing: border-box;
/* 由于我们在h5c3中大量使用div盒模型
所以我们直接就准备写好box-sizing */
}
1.search-index顶部导航栏
效果如下:
- 首先这个顶部导航栏是右边固定定位,左边撑满使用flex
tips:
固定定位是以浏览器为主,所以我们要给这个元素限定宽度
max-width:540px; & min-width:320px;
记得给父盒子加上display:flex;然后width:100%;
让固定定位的盒子居中显示的页面的中间方法(margin无法实现):
way1:
position: fixed;
top: 0;
left: 50%;
-webkit-transform: translateX(-50%);/*为了适应不同浏览器*/
transform: translateX(-50%);
way2:
position: fixed;
top: 0;
右边的用户这一块用a链接,a链接为行元素没有大小,但我们一旦父盒子使用了flex那里面的子盒子就类似也都有了flex的效果,那a链接就可以直接给大小了。
效果如下:
这种布局我们就用a链接和伪类来写:
.user {
width: 44px;
height: 44px;
/* a链接给了大小值 */
font-size: 12px;
text-align: center;
color: #2eaae0;
}
.user::before {
content: '';
display: block;
width: 24px;
height: 24px;
background: url(../images/sprite.png) no-repeat -58px -193px;
background-size: 103px auto;
margin: 5px auto -2px;
}
效果如下:
由于放大镜这个图片是行内块元素我们为了让它固定在搜索栏里所以我们就用定位来解决
代码如下:
.search {
position: relative;
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
flex: 1;
margin: 6px 10px;
border-radius: 5px;
font-size: 12px;
color: #666;
padding-left: 25px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
.search::before {
content: '';
/* display: block; */
position: absolute;
top: 5px;
left: 5px;
width: 15px;
height: 15px;
background: url(../images/sprite.png) no-repeat -59px -279px;
background-size: 104px auto;
/* margin: 5px; */
}
在这一部分还有一个小细节,就是我们在制作中会发现放大镜的图片和那一行提示问题没有对齐
问题如下:
那我们的解决思想就是height和line-height之间的问题
代码如下:
.search {
position: relative;
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
flex: 1;
margin: 6px 10px;
border-radius: 5px;
font-size: 12px;
color: #666;
padding-left: 25px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
2.local-nav局部导航栏
效果如下:
在这部分我们发现大盒子local-nav里的小盒子大小相同,所以我们选择使用ul和li来表示;里面的li是由整个a链接来包含一张图片和一行字。
HTML框架如下:
CSS代码如下:
.local-nav {
display: flex;
height: 64px;
background-color: #fff;
margin: 3px 4px;
border-radius: 8px;
}
.local-nav li {
flex: 1;
/* 盒子里面的li平均分布 */
}
.local-nav a {
display: flex;
flex-direction: column;
/* 侧轴居中对齐 因为是单行 */
align-items: center;
font-size: 12px;
}
效果如下:
对于这种效果的,我们使用flex来解决:
.local-nav li {
flex: 1;
/* 盒子里面的li平均分布 */
}
.local-nav a {
display: flex;
flex-direction: column;
/* 侧轴居中对齐 因为是单行 */
align-items: center;
font-size: 12px;
}
3.nav主导航栏
效果如下:
这部分的制作思路,先分成三行用3个div来表示出来,每一行中再分成3列。
代码如下:
/* 先分成3行 */
nav {
overflow: hidden;
border-radius: 8px;
margin: 0 4px 3px;
}
.nav-common {
display: flex;
height: 88px;
background-color: pink;
}
.nav-common:nth-child(2) {
margin: 3px 0;
}
/* 再分成3列 */
.nav-item {
flex: 1;
display: flex;
flex-direction: column;
}
.nav-item a {
flex: 1;
text-align: center;
line-height: 44px;
color: #fff;
font-size: 14px;
/* 文字阴影 */
text-shadow: 1px 1px rgba(0, 0, 0, .2);
}
4.subnav-entry侧导航栏
这一个布局和local-nav布局类似所以大致思路和代码都是一样。
5.sales-box销售栏
效果如下:
我们先用一个大盒子sales-box装sales-hd和sales-bd,对于sales-hd左右两部分用的是定位来解决。
HTML框架如下:
<div class="sales-hd">
<h2>热门活动</h2>
<a href="#" class="more">获取更多福利</a>
</div>
CSS代码如下:
.sales-hd h2 {
position: relative;
text-indent: -999px;
overflow: hidden;
/* 让字体隐藏 */
}
.sales-hd h2::after {
position: absolute;
top: 8px;
left: 20px;
content: '';
width: 79px;
height: 15px;
background-color: pink;
background: url(../images/hot.png) no-repeat 0 -20px;
background-size: 79px auto;
}
总结
携程网的页面制作案例基本就是如上所述内容,再制作过程中我运用到的大部分是flex布局,以及我在制作中出现的问题和一些需要注意的细节都有罗列出来,不仅是为了让我能在下次避免到类似问题,还能够让大家在制作中有所避免和注意。