使用 flex布局 制作携程网首页
1. 技术选型
2. 搭建相关文件夹结构
3. 引入视口标签以及初始化样式
4. 常用初始化样式
5. 首页布局分析以及搜索模块布局
index.css
/*搜索模块*/
.search-index{
/*固定定位跟父级没有关系,它以屏幕为准*/
position: fixed;
top: 0;
left: 50%;
/*固定的盒子应该有宽度*/
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
background-color: pink;
}
index.html
<!--顶部搜索-->
<div class="search-index">
</div>
6. 搜索模块划分以及 user模块制作
index.html
<div class="search-index">
<div class="search"></div>
<a class="user">我 的</a>
</div>
index.css
/*搜索模块*/
.search{
flex: 1;
/*background-color: red;*/
}
/*用户模块*/
.user {
background-color: purple;
width: 44px;
height: 44px;
text-align: center;
font-size: 12px;
color: #2eaae0;
}
/*用户头像*/
.user::before{
content: "";
background: url("../images/sprite.png") no-repeat -59px -194px;
background-size: 104px auto;
width: 23px;
height: 23px;
display: block;
margin: 5px auto 0;
}
7. 搜索模块制作
index.html
<div class="search-index">
<div class="search">搜索:目的地/酒店/景点/航班号</div>
<a class="user">我 的</a>
</div>
index.css
/*使用 css3 盒子模型来做*/
div {
box-sizing: border-box;
}
/*search-index模块*/
.search-index{
/*固定定位跟父级没有关系,它以屏幕为准*/
position: fixed;
top: 0;
left: 50%;
/*固定的盒子应该有宽度*/
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
/*background-color: pink;*/
background-color: #F6F6F6;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
display: flex;
}
/*search模块*/
.search{
flex: 1;
/*background-color: red;*/
height: 26px;
line-height: 24px;
font-size: 12px;
border-radius: 5px;
margin: 7px 10px;
padding-left: 25px;
box-shadow: 0 2px 4px rgba(0,0,0, .2);
position: relative;
}
.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;
}
8.焦点图 focus 的制作
index.html
<!--焦点图模块-->
<div class="focus">
<img src="upload/focus.jpg">
</div>
index.css
/*焦点图模块*/
.focus {
padding-top: 44px;
}
.focus img {
width: 100%;
}
9. local-nav布局
index.html
<!--局部导航栏-->
<div class="local-nav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</div>
index.css
li {
margin: 0;
padding: 0;
list-style: none;
}
/*局部导航栏模块*/
.local-nav{
background-color: #fff;
height: 64px;
border-radius: 5px;
margin: 3px 4px;
display: flex;
}
.local-nav li {
flex: 1;
}
10. 常见flex 布局思路
栗子:
第一步:默认主轴是 x 轴
index.html
<li>
<a>
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
index.css
.local-nav a {
display: flex;
}
.local-nav-icon{
width: 32px;
height: 32px;
background-color: pink;
}
第二步:把主轴设置为 y 轴
.local-nav a {
display: flex;
flex-direction: column;
}
第三步:把侧轴设置为居中对齐
.local-nav a {
display: flex;
flex-direction: column;
/*这里是单行的,没有出现换行的情况,所以使用 align-items*/
align-items: center;
}
11. local-nav 内容制作
index.html
<!--局部导航栏-->
<div class="local-nav">
<li>
<a title="景点·玩乐">
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon"></span>
<span>景点·玩乐</span>
</a>
</li>
</div>
index.css
/*local-nav内容制作*/
.local-nav a {
display: flex;
flex-direction: column;
/*这里是单行的,没有出现换行的情况,所以使用 align-items*/
align-items: center;
font-size: 12px;
}
.local-nav-icon{
width: 32px;
height: 32px;
/*background-color: pink;*/
background: url("../images/localnav_bg.png") no-repeat 0 0;
background-size: 32px auto;
}
12. 利用属性选择器更换背景图片
index.html
<div class="local-nav">
<li>
<a title="景点·玩乐">
<span class="local-nav-icon-icon1"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon-icon2"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon-icon3"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon-icon4"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a title="景点·玩乐">
<span class="local-nav-icon-icon5"></span>
<span>景点·玩乐</span>
</a>
</li>
</div>
index.css
.local-nav li [class^="local-nav-icon"]{
width: 32px;
height: 32px;
/*background-color: pink;*/
background: url("../images/localnav_bg.png") no-repeat 0 0;
background-size: 32px auto;
}
.local-nav li .local-nav-icon-icon2{
background-position: 0 -32px;
}
.local-nav li .local-nav-icon-icon3{
background-position: 0 -64px;
}
.local-nav li .local-nav-icon-icon4{
background-position: 0 -96px;
}
.local-nav li .local-nav-icon-icon5{
background-position: 0 -128px;
}
13. nav 外观布局
index.html
<!--菜单栏部分-->
<nav>
<div class="nav-common">
<div class="nav-items">a</div>
<div class="nav-items">b</div>
<div class="nav-items">c</div>
</div>
<div class="nav-common">2</div>
<div class="nav-common">3</div>
</nav>
index.css
/*菜单栏部分*/
nav {
border-radius: 8px;
margin: 0 4px 3px;
}
nav .nav-common{
/*width: 100%;*/
height: 88px;
background-color: pink;
border-radius: 3px;
display: flex;
}
/* -n+2就是选择前面两个元素 */
nav .nav-common:nth-child(2){
margin: 3px 0;
}
/*.nav-items部分*/
.nav-items{
flex: 1;
}
.nav-items:nth-child(-n+2){
border-right: 1px solid #ffffff;
}
14. nav 内容制作
index.html
<!--菜单栏部分-->
<nav>
<div class="nav-common">
<div class="nav-items">
<a>海外酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
</div>
<div class="nav-common">
<div class="nav-items">
<a>海外酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
</div>
<div class="nav-common">
<div class="nav-items">
<a>海外酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
<div class="nav-items">
<a>海外酒店</a>
<a>特价酒店</a>
</div>
</div>
</nav>
index.css
/*.nav-items部分*/
.nav-items{
flex: 1;
display: flex;
/*这两个是不冲突的*/
flex-direction: column;
}
.nav-items a {
flex: 1;
text-align: center;
line-height: 44px;
color: #ffffff;
}
.nav-common .nav-items:nth-child(1) a{
border-bottom: 0;
background: url("../images/hotel.png") no-repeat bottom center;
background-size: 121px auto;
}
.nav-items a:nth-child(1){
border-bottom: 1px solid #ffffff;
}
15. 背景线性渐变
栗子:
<style>
div{
width: 600px;
height: 200px;
background:-webkit-linear-gradient(left,red,blue);
}
</style>
<body>
<div></div>
</body>
注意: 背景渐变必须添加浏览器私有前缀
-
从左到右的渐变:
background:-webkit-linear-gradient(left,red,blue);
-
不加方向 默认是从上往下的方向:
background:-webkit-linear-gradient(red,blue);
-
左上角到右下角的渐变:
background:-webkit-linear-gradient(top left,red,blue);
16. 设置nav 内容背景渐变
index.css
/*背景渐变*/
.nav-common:nth-child(1) {
background: -webkit-linear-gradient(left, #FA5A55, #FA994D);
}
.nav-common:nth-child(2) {
background: -webkit-linear-gradient(left, #4B90ED, #53BCED);
}
.nav-common:nth-child(3) {
background: -webkit-linear-gradient(left, #34C2A9, #6CD559);
}
17. subnav-entry 模块制作
index.html
<div class="subnav-entry">
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
<li>
<a>
<span class="subnav-entry-icon"></span>
<span>自由行</span>
</a>
</li>
</div>
index.css
/*subnav-entry模块制作*/
.subnav-entry{
background: #ffffff;
margin: 0 4px;
border-radius: 8px;
display: flex;
flex-wrap: wrap;
padding: 5px 0;
}
.subnav-entry li {
flex: 20%;
}
.subnav-entry li a{
display: flex;
flex-direction: column;
align-items: center;
}
.subnav-entry-icon{
width:28px;
height: 28px;
background-color: pink;
background: url("../images/subnav-bg.png") no-repeat;
background-size: 28px auto;
margin-top: 4px;
}
18. 热门活动模块制作
index.html
<!--销售模块-->
<div class="sales-box">
<div class="sales-hd">
<h2>热门活动</h2>
<a>更多</a>
</div>
</div>
index.css
/*销售模块*/
.sales-box {
border-top: 1px solid #bbb;
background-color: #fff;
margin: 4px;
}
.sales-hd {
height: 44px;
border-bottom: 1px solid #ccc;
position: relative;
}
.sales-hd h2 {
text-indent: -9999px;
position: relative;
}
.sales-hd h2::after{
content: "";
position: absolute;
top: 5px;
left: 8px;
width: 79px;
height: 15px;
/*background-color: pink;*/
background: url("../images/hot.png") no-repeat 0 -20px;
background-size: 79px auto;
}
19. 获取更多模块制作
index.html
<!--销售模块-->
<div class="sales-box">
<div class="sales-hd">
<h2>热门活动</h2>
<a class="more">获取更多信息</a>
</div>
</div>
index.css
/*获取更多信息*/
.more {
position: absolute;
right: 5px;
top: 0px;
background: -webkit-linear-gradient(left, #FF506C, #FF6BC6);
border-radius: 15px;
padding: 3px 20px 3px 10px;
color: #fff;
}
.more::after {
content: "";
position: absolute;
top: 9px;
right: 9px;
width: 7px;
height: 7px;
border-top: 2px solid #fff;
border-right: 2px solid #fff;
transform: rotate(45deg);
}
20. sales-bd模块内容制作
index.html
<div class="sales-bd">
<div class="row">
<a href="#">
<img src="upload/pic1.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic2.jpg" alt="">
</a>
</div>
<div class="row">
<a href="#">
<img src="upload/pic3.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic4.jpg" alt="">
</a>
</div>
<div class="row">
<a href="#">
<img src="upload/pic5.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic6.jpg" alt="">
</a>
</div>
</div>
index.css
/*sales-bd模块*/
.row {
display: flex;
}
.row a {
flex: 1;
border-bottom: 1px solid #eee;
}
.row a:nth-child(1) {
border-right: 1px solid #eee;
}
.row a img {
width: 100%;
}