移动WEB开发之flex布局—— 携程网首页案例制作

移动WEB开发之flex布局

特点:

  • 操作方便,布局极为简单,移动端应用很广泛
  • PC端浏览器支持情况较差
  • IE11 或 更低版本,不支持或仅部分支持
  • flexbox(flexible box)

1. flex布局原理

总结: 通过给父盒子添加flex属性来控制子盒子的位置和排列方式

  • 当我们为父盒子设为flex布局以后,子元素的float、clear 和 vertical-align属性会失效
    (且,flex布局后,内联元素会变成块)

2. flex容器(flex container)——父级元素

2.1 属性
  • flex-derection:row | row-reverse | column | column-reverse
  • flex-wrap:nowrap(默认值) | wrap | wrap-reverse
  • flex-flow:flex-derection | flex-wrap
  • justify-content (主轴的对齐方向):flex-start | flex-end | center | space-between(两端对齐) | space-arond (flex item两侧距离间隔相等)
  • align-items (交叉轴的对齐方向):flex-start | flex-end | center | baseline | stretch
  • align-content(多根轴的对齐方式):flex-start | flex-end | center | space-between(两端对齐) | space-arond (flex item两侧距离间隔相等)

3. flex项目(flex item)子集元素

3.1 属性
  • flex-grow(放大的比例):0(默认) | number
    得着当前的剩余空间,如果没有剩余空间这个就失效了
  • flex-shrink 缩小的比例:1(默认可以缩小) | 0(不可以缩小) | number
    只有空间不足的时候才有必要缩小
  • flex-basis(伸缩基准值):设置flex item 的"宽度",但一般不会这么干
  • flex:flex-grow flex-shrink flex-basis
  • order(排列顺序):值越小越排在前面
  • align-self 单个项目的对齐方式
flex: 定义子项目 分配 剩余的空间,用flex来表示占多少份数

(发蛋糕,几分之几)

4. 携程网首页案例制作

在这里插入图片描述

4.1 技术选型

方案:采取单独制作移动页面方案
技术:布局采取flex布局

4.2 搭建相关文件夹结构
4.3 设置视口标签以及引入初始化样式
4.4 常用初始化样式

(前面的课程有说)

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;
}
4.5 布局注意点
  1. 高度定死
  2. 很多内容都是用伪元素来做的,不是添加盒子
  3. 属性选择器如何使用? 外层盒子 + 你要挑选的具有的属性 (要空格) / 你要选取的标签 + 该标签具有的属性(不要空格)
  4. 隐藏文字
  5. flex:20%
  6. 精灵图的使用,2x是指放大2倍的
    background-size:宽度缩小一倍 auto(高度自动)
  7. 渐变: 前面要添加浏览器私有前缀, -webkit-
    background: -webkit-linear-gradient(起始方向,颜色1,颜色2…)

a. 伪类选择器派上用场 :nth-child
在这里插入图片描述

b.
在这里插入图片描述
像这样

c.
在这里插入图片描述
在这里插入图片描述

.sales-hd h2 {
    position: relative;
    text-indent: -999px;
    overflow: hidden;
}

在这里插入图片描述

5. 代码

1,
2,index.css

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;
}
/* 搜索模块 */

.search-index{
    display: flex;
    /* 固定定位跟父级没有关系,它以屏幕为准 */
    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: #f6f6f6;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    /* background-color: pink; */
}

.search-index .search{
    position: relative;
    height: 26px;
    border: 1px solid #ccc;
    flex: 1;
    color: #666;
    margin: 7px 10px;
    padding-left: 25px;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0,0,0,.2);
}

.search-index .search::before{
    content: "";
    position: absolute;
    top: 5px;
    left: 5px;
    width: 15px;
    height: 15px;
    background: url(../images/sprite.png) no-repeat -59px -279px;
    background-size: 104px auto;
}
.search-index .user{
    width: 44px;
    height: 44px;
    /* background-color: purple; */
    text-align: center;
}

.search-index .user::before{
    content:"";
    display: block;
    width: 23px;
    height: 23px;
    background: url(../images/sprite.png) no-repeat -59px -194px;
    background-size: 104px auto;
    margin: 4px auto -2px;
}

/* focus */
.focus{
    padding-top: 44px;
    /* visibility: hidden; */
}

.focus img{
    width: 100%;
}
/* local nav */

.local-nav{
    display: flex;
    height: 64px;
    margin: 3px 4px;
    background-color: #fff;
    border-radius: 8px;
}

.local-nav-items{
    flex: 1;
    
}

.local-nav-items a{
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 12px;
}

span[class^="local-nav-icon"]{
    width: 32px;
    height: 32px;
    background-color: pink;
    margin-top: 8px;
    background: url(../images/localnav_bg.png) no-repeat 0 0;
    background-size: 32px auto;
}

.local-nav-items a .local-nav-icon-icon2 {
    background-position: 0 -32px;
}

.local-nav-items a .local-nav-icon-icon3 {
    background-position: 0 -64px;
}

.local-nav-items a.local-nav-icon-icon4 {
    background-position: 0 -96px;
}

.local-nav-items a .local-nav-icon-icon5 {
    background-position: 0 -128px;
}


/* nav */

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;
}

.nav-items{
    flex: 1;
    display: flex;
    flex-direction: column;
}

.nav-items a {
    flex: 1;
    text-align: center;
    line-height: 44px;
    color: #fff;
    font-size: 14px;
    /* 文字阴影 */
    text-shadow: 1px 1px rgba(0, 0, 0, .2);
}
/* -n+2 就是选择前面两个元素 */
.nav-items:nth-child(-n+2) {
    border-right: 1px solid #fff;
}

.nav-items a:nth-child(1){
    border-bottom: 1px solid #fff;
}

.nav-items:nth-child(1) a {
    border: 0;
    background: url(../images/hotel.png) no-repeat bottom center;
    background-size: 121px auto;
}

.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);
}

/* subnav-entry */

.subnav-entry {
    display: flex;
    border-radius: 8px;
    background-color: #fff;
    margin: 0 4px;
    flex-wrap: wrap;
    padding: 5px 0;
}

.subnav-entry li{
    flex: 20%;
    /* background-color: purple; */
}

.subnav-entry a {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.subnav-entry-icon {
    width: 28px;
    height: 28px;
    background-color: pink;
    margin-top: 4px;
    background: url(../images/subnav-bg.png) no-repeat;
    background-size: 28px auto;
}

/* sales-box */

.sales-box {
    border-top: 1px solid #bbb;
    background-color: #fff;
    margin: 4px;
}

.sales-hd {
    position: relative;
    height: 44px;
    border-bottom: 1px solid #ccc;
}

.sales-hd h2 {
    position: relative;
    text-indent: -999px;
    overflow: hidden;
}

.sales-hd h2::after {
    position: absolute;
    top: 5px;
    left: 8px;
    content: "";
    width: 79px;
    height: 15px;
    background: url(../images/hot.png) no-repeat 0 -20px;
    background-size: 79px auto;
}

.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);
}

.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%;
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>携程在手,说走就走</title>
    <link rel="stylesheet" href="css/normalize.css" />
    <link rel="stylesheet" href="css/index.css">

</head>
<body>
    <div class="search-index">
        <div class="search">搜索:目的地/酒店/景点/航班号</div>
        <a href="#" class="user">我 的</a>
    </div>
    <div class="focus">
        <img src="upload/focus.jpg" alt=""/>
    </div>
    <div class="local-nav">
        <div class="local-nav-items">
            <a href="#" title="景点·玩乐">
                <span class="local-nav-icon-icon1"></span>
                <span>景点·玩乐</span>
            </a>
        </div>
        <div class="local-nav-items">
            <a href="#" title="景点·玩乐">
                <span class="local-nav-icon-icon2"></span>
                <span>景点·玩乐</span>
            </a>
        </div>
        <div class="local-nav-items">
            <a href="#" title="景点·玩乐">
                <span class="local-nav-icon-icon3"></span>
                <span>景点·玩乐</span>
            </a>
        </div>
        <div class="local-nav-items">
            <a href="#" title="景点·玩乐">
                <span class="local-nav-icon-icon4"></span>
                <span>景点·玩乐</span>
            </a>
        </div>
        <div class="local-nav-items">
            <a href="#" title="景点·玩乐">
                <span class="local-nav-icon-icon5"></span>
                <span>景点·玩乐</span>
            </a>
        </div>
    </div>
    <!-- nav -->
    <nav>
        <div class="nav-common">
            <div class="nav-items">
                <a href="#">海外酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
        </div>
        <div class="nav-common">
            <div class="nav-items">
                <a href="#">海外酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
        </div>
        <div class="nav-common">
            <div class="nav-items">
                <a href="#">海外酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
            <div class="nav-items">
                <a href="#">海外酒店</a>
                <a href="#">特价酒店</a>
            </div>
        </div>
    </nav>

    <!--  subnav-entry  -->
    <div>
        <ul  class="subnav-entry">
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="subnav-entry-icon"></span>
                    <span>电话费</span>
                </a>
            </li>
        </ul>
    </div>

    <!-- 销售模块 -->
    <div class="sales-box">
        <div class="sales-hd">
            <h2>热门活动</h2>
            <a href="#" class="more">获取更多福利</a>
        </div>
        <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>
    </div>
</body>
</html>
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值