携程移动端静态首页flex布局(弹性布局)
主要知识点:flex布局&属性选择器&阴影&背景渐变
HTML结构
<body>
<!-- 顶部搜索 -->
<div class="search-index">
<div class="search">搜索:目的地/酒店/景点/航班号</div>
<a href="#" class="user">我 的</a>
</div>
<!-- focus模块 -->
<div class="focus">
<img src="./imgs/focus.jpg" >
</div>
<!-- 局部导航模块 -->
<div class="local-nav">
<a href="#">
<span class="local-nav-icon1" title="景点·玩乐"></span>
<span>景点·玩乐</span>
</a>
<a href="#" title="周边游">
<span class="local-nav-icon2"></span>
<span>周边游</span>
</a>
<a href="#" title="美食林">
<span class="local-nav-icon3"></span>
<span>美食林</span>
</a>
<a href="#" title="一日游">
<span class="local-nav-icon4"></span>
<span>一日游</span>
</a>
<a href="#" title="当地攻略">
<span class="local-nav-icon5"></span>
<span>当地攻略</span>
</a>
</div>
<!-- 主导航栏模块 -->
<nav>
<div class="nav-common">
<li><a href="#">酒店</a></li>
<li class="two">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</li>
<li class="two">
<a href="#">团购</a>
<a href="#">民宿·客栈</a>
</li>
</div>
<div class="nav-common">
<li><a href="#">机票</a></li>
<li class="two">
<a href="#">火车票</a>
<a href="#">特价机票</a>
</li>
<li class="two">
<a href="#">汽车票·船票</a>
<a href="#">专车·租车</a>
</li>
</div>
<div class="nav-common">
<li><a href="#">旅游</a></li>
<li class="two">
<a href="#">门票</a>
<a href="#">目的地攻略</a>
</li>
<li class="two">
<a href="#">邮轮旅行</a>
<a href="#">定制旅行</a>
</li>
</div>
</nav>
<!-- 生活服务模块 -->
<div class="life-sevice">
<div class="rol1">
<a href="#">
<span></span>
<span>WiFi电话卡</span>
</a>
<a href="#">
<span></span>
<span>保险签证</span>
</a>
<a href="#">
<span></span>
<span>外币兑换</span>
</a>
<a href="#">
<span></span>
<span>购物</span>
</a>
<a href="#">
<span></span>
<span>当地向导</span>
</a>
</div>
<div class="rol2">
<a href="#">
<span></span>
<span>*行</span>
</a>
<a href="#">
<span></span>
<span>*玩乐</span>
</a>
<a href="#">
<span></span>
<span>礼品卡</span>
</a>
<a href="#">
<span></span>
<span>信用卡</span>
</a>
<a href="#">
<span></span>
<span>更多</span>
</a>
</div>
</div>
<!-- 热门活动模块 -->
<div class="hot">
<h2>
<div class="left">热门活动</div>
<a class="right" href="#">获取更多福利></a>
</h2>
<div class="rol1">
<a href="#"><img src="./imgs/pic1.jpg" ></a>
<a href="#"><img src="./imgs/pic2.jpg" ></a>
</div>
<div class="rol2">
<a href="#"><img src="./imgs/pic3.jpg" ></a>
<a href="#"><img src="./imgs/pic4.jpg" ></a>
</div>
<div class="rol3">
<a href="#"><img src="./imgs/pic5.jpg" ></a>
<a href="#"><img src="./imgs/pic6.jpg" ></a>
</div>
</div>
<!-- 底部模块 -->
<footer>
<div class="banner">
<a href="#">
<span></span>
<span>电话预订</span>
</a>
<a href="#">
<span></span>
<span>下载客户端</span>
</a>
<a href="#">
<span></span>
<span>我的</span>
</a>
</div>
<div class="detail">
<a href="#">网站地图 | Language | 电脑版</a>
<p>@2021携程旅行 | 沪ICP备08023580号</p>
</div>
</footer>
</body>
思路复盘
顶部搜索模块:一个.search-index大盒子里有两个小盒子(.search&.user)
*大盒子设置display:flex;.search盒子设置flex:1;保证页面拉大时搜索框跟着变大
*小盒子里的图标使用伪元素选择器制作,简化HTML结构
局部导航栏模块:一个.local-nav的大盒子里放五个a,每个a里面装两个span盒子
*大盒子设置display:flex;每个a盒子设置flex:1;使大盒子被平均分成五份,每个a各占一份
*每个a盒子也设置display:flex;&flex-direction:column;&align-items:center;使a盒子里面的内容水平居中
其他的几个主要的盒子思路都和这个差不多了
CSS代码
body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, Verdana;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
box-sizing: border-box;
}
a {
text-decoration: none;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.search-index {
display: flex;
width: 100%;
/*固定的盒子要有宽度 */
height: 44px;
position: fixed;
top: 0;
left: auto;
max-width: 540px;
min-width: 320px;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
background-color: #f6f6f6;
}
.search-index .search {
flex: 1;
margin: 5px 5px;
height: 28px;
border: 1px solid #ccc;
border-radius: 5px;
margin: 7px 10px;
line-height: 26px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.45);
font-size: 12px;
color: #666;
}
.search-index .search::before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
background: url(../imgs/sprite.png) no-repeat -59px -278px;
background-size: 104px auto;
margin: 5px 8px;
vertical-align: middle;
}
.search-index .user {
width: 44px;
height: 44px;
color: #2eaae0;
font-size: 12px;
text-align: center;
}
.search-index .user::before {
content: "";
display: block;
width: 24px;
height: 26px;
background: url(../imgs/sprite.png) no-repeat -59px -193px;
background-size: 104px auto;
margin: 3px auto 0 auto;
}
/* focus模块 */
.focus {
margin-top: 47px;
}
.focus img {
width: 100%;
}
/* 局部导航栏 */
.local-nav {
display: flex;
height: 64px;
background-color: #fff;
border-radius: 8px;
margin: 3px 5px;
}
.local-nav a {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
color: #222;
font-size: 12px;
}
.local-nav span[class^="local-nav-icon"] {
width: 32px;
height: 32px;
background: url(../imgs/localnav_bg.png) no-repeat 0 0;
background-size: 32px auto;
margin: 9px 0 5px 0;
}
.local-nav a .local-nav-icon2 {
background-position: 0 -32px;
}
.local-nav a .local-nav-icon3 {
background-position-y: -64px;
}
.local-nav a .local-nav-icon4 {
background-position-y: -96px;
}
.local-nav a .local-nav-icon5 {
background-position-y: -128px;
}
/* 主导航栏模块 */
nav {
overflow: hidden;
height: 270px;
margin: 0 4px 5px 4px;
border-radius: 8px;
}
nav .nav-common {
display: flex;
height: 90px;
background: -webkit-linear-gradient(left, #FA5A55, #FA994D);
margin-bottom: 5px;
}
nav .nav-common:nth-child(2) {
background: -webkit-linear-gradient(left, #4B90ED, #53BCED);
}
nav .nav-common:nth-child(3) {
background: -webkit-linear-gradient(left, #34C2A9, #6CD559);
}
nav .nav-common li {
flex: 1;
display: flex;
border-left: 1px solid #fff;
border-bottom: 1px solid #fff;
}
nav .nav-common li a {
flex: 1;
text-align: center;
line-height: 45px;
text-shadow: 1px 1px rgba(0, 0, 0, 0.45);
font-size: 12px;
color: #fff;
}
nav .nav-common li:nth-child(1) a {
background: url(../imgs/hotel.png) no-repeat bottom center;
background-size: 121px auto;
}
nav .nav-common:nth-child(2) li:nth-child(1) a {
background: url(../imgs/plane.png) no-repeat bottom center;
background-size: 121px auto;
}
nav .nav-common:nth-child(3) li:nth-child(1) a {
background: url(../imgs/vacation.png) no-repeat bottom center;
background-size: 121px auto;
}
nav .nav-common li:nth-child(n+2) {
display: flex;
flex-direction: column;
}
nav .nav-common li:nth-child(n+2) a {
flex: 1;
height: 45px;
}
nav .nav-common li:nth-child(n+2) a:nth-child(1) {
border-bottom: 1px solid #fff;
}
/* 生活服务模块 */
.life-sevice {
display: flex;
flex-direction: column;
border-radius: 8px;
background-color: #fff;
margin: 0 4px 5px 4px;
height: 115px;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
}
.life-sevice .rol1,
.rol2 {
flex: 1;
display: flex;
}
.life-sevice a {
flex: 20%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 8px 0 0 0;
color: #000;
font-size: 12px;
}
.life-sevice div a span {
flex: 1;
}
.life-sevice div a span:nth-child(1) {
width: 28px;
height: 28px;
background: url(../imgs/subnav-bg.png) no-repeat;
background-size: 28px auto;
}
.life-sevice .rol1 a:nth-child(2) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -30px;
background-size: 28px auto;
}
.life-sevice .rol1 a:nth-child(3) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -64px;
background-size: 28px auto;
}
.life-sevice .rol1 a:nth-child(4) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -100px;
background-size: 28px auto;
}
.life-sevice .rol1 a:nth-child(5) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -133px;
background-size: 28px auto;
}
.life-sevice .rol2 a:nth-child(1) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -163px;
background-size: 28px auto;
}
.life-sevice .rol2 a:nth-child(2) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -198px;
background-size: 28px auto;
}
.life-sevice .rol2 a:nth-child(3) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -228px;
background-size: 28px auto;
}
.life-sevice .rol2 a:nth-child(4) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -262px;
background-size: 28px auto;
}
.life-sevice .rol2 a:nth-child(5) span:nth-child(1) {
background: url(../imgs/subnav-bg.png) no-repeat 0 -290px;
background-size: 28px auto;
}
/* 热门活动模块 */
.hot {
border-top: 1px solid #ddd;
margin-bottom: 5px;
background-color: #fff;
}
h2 {
height: 31px;
font-size: 12px;
border-bottom: 1px solid #ededed;
}
h2 .left {
text-indent: -999px;
overflow: hidden;
/* 搜索引擎优化 */
float: left;
width: 79px;
height: 18px;
margin-left: 5px;
background: url(../imgs/hot.png) no-repeat 0 -18px;
background-size: 79px auto;
}
h2 a {
float: right;
padding: 3px 5px;
margin: -3px 8px 0 0;
border-radius: 15px;
color: #fff;
font-weight: 400;
background: -webkit-linear-gradient(left, #FF506C, #FF6BC6);
}
.hot>div {
display: flex;
}
.hot>div a {
flex: 1;
border-bottom: 1px solid #ededed;
}
.hot>div a:nth-child(1) {
border-right: 1px solid #ededed;
}
.hot>div a img {
width: 100%;
}
/* 底部模块 */
.banner {
display: flex;
background-color: #fff;
height: 50px;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
text-align: center;
box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.2)
}
.banner a {
flex: 1;
display: flex;
justify-items: center;
align-items: center;
flex-direction: column;
font-size: 12px;
color: #222;
}
.banner a span {
flex: 1;
}
.banner a span:nth-child(1) {
width: 25px;
height: 24px;
margin-top: 5px;
background: url(../imgs/sprite.png) no-repeat -59px -170px;
background-size: 104px auto;
}
.banner a:nth-child(2) span:nth-child(1) {
background: url(../imgs/sprite.png) no-repeat -59px -150px;
background-size: 104px auto;
}
.banner a:nth-child(3) span:nth-child(1) {
background: url(../imgs/sprite.png) no-repeat -59px -307px;
background-size: 104px auto;
}
.detail {
text-align: center;
font-size: 12px;
margin-top: 10px;
}
.detail a {
color: #222;
}
.detail p {
color: #666;
margin: 0;
}
知识点浅解
背景线性渐变:background:linear-gradient(起始方向,颜色1,颜色2,颜色3,…);
盒子阴影:box-shadow:水平阴影的位置,垂直阴影的位置,阴影的模糊范围,阴影颜色;
eg:box-shadow:1px,1px,4px,rgba(0,0,0,0.2);
效果展示
携程移动端静态首页flex布局