移动端流式布局练习

案例:京东移动端首页

内容为黑马程序员教学

1.技术选型

方案:采取单独制作移动页面方案

技术:采取流式布局 不再使用固定像素

2.搭建文件结构

在这里插入图片描述

3.设置视口标签以及引入初始化样式

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,
     initial-scale=1.0 user-scalable=no ,minimum-scale=1.0">
    <!-- 禁止用户缩放 -->
    <title>Document</title>
    <!-- 引入首页css -->
    <link rel="stylesheet" href="css/index.css">
    <!-- 引入css初始化文件 -->
    <link rel="stylesheet" href="css/normalize.css">
</head>

4.常用初始化页面样式

经过分析给body宽度为100% 但限制最大最小宽度

body {
    width: 100%;
    min-width: 320px;
    max-width: 640px;
    margin: 0 auto;
    font-size: 14px;
    font-family: -apple-system, Helvetica, sans-serif;
    color: #666;
    line-height: 1.5;
}

4.1.分析导航栏样式

四个部分占比分别为8/10/57/25

请添加图片描述

ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

.app {
    height: 45px;
}

.app ul li {
    float: left;
    height: 45px;
    background-color: #333;
}

.app ul li:nth-child(1) {
    width: 8%;
}

.app ul li:nth-child(2) {
    width: 10%;
}

.app ul li:nth-child(3) {
    width: 57%;
}

.app ul li:nth-child(4) {
    width: 25%;
    background-color: #F63515; 
}

.app {
    height: 45px;
}

.app ul li {
    float: left;
    height: 45px;
    line-height: 45px;
    background-color: #333;
    text-align: center;
    color: #fff;
}

.app ul li:nth-child(1) {
    width: 8%;
}

.app ul li:nth-child(1) img {
    width: 16px;
    vertical-align: middle;
    /* margin: 0 auto; */
    /* height: auto; */
}

.app ul li:nth-child(2) {
    width: 10%;
}

.app ul li:nth-child(2) img {
    width: 30px;
    vertical-align: middle;
}

.app ul li:nth-child(3) {
    width: 57%;
}

.app ul li:nth-child(4) {
    width: 25%;
    background-color: #F63515;
}

以下为效果图

在这里插入图片描述

4.2.搜索模块布局

分析 屏幕宽度变化是只有中间搜索框伸缩

将两边内容使用定位 搜索框使用标准流并使用margin值空出两边内容的部分

在这里插入图片描述

.search-wrap {
    position: relative;
    overflow: hidden;
    height: 44px;
}

.search-btn {
    position: absolute;
    top: 0;
    left: 0;
    background-color: pink;
    width: 40px;
    height: 44px;
}

.search-login {
    position: absolute;
    top: 0;
    right: 0;
    background-color: purple;
    width: 40px;
    height: 44px;
}

.search {
    /* 不能给宽度 否则会将btn和login内容撑出 */
    height: 30px;
    /* 给margin值 */
    margin: 0 50px;
    /* 只给mt值时会发生外边距合并   解决方法给父盒子添加overflow:hidden */
    margin-top: 7px;
    background-color: #fff;
    border-radius: 25px;
}

填充内容 使用伪标签

.search-btn::before {
    content: "";
    display: block;
    width: 20px;
    height: 18px;
    background: url(../images/s_btn.png) no-repeat;
    /* 原图太大 需要更改大小 */
    background-size: 20px 18px;
    margin: 14px 0 0 15px;
}

.jd-icon::after {
    content: "";
    position: absolute;
    right: -8px;
    top: 0;
    display: block;
    width: 1px;
    height: 15px;
    background-color: #ccc;
}

5.二倍精灵图做法

  • 在图片软件里将精灵图等比例缩放为原来的一般
  • 之后根据大小测量坐标
  • 注意代码里面background-size也要写:精灵图原来宽度的一半

注意不要在ps等软件里点击保存 会更改原图大小

只需要测量后在background-size里设置图片大小

.sou {
    position: absolute;
    top: 8px;
    left: 50px;
    width: 18px;
    height: 15px;
    background-color: pink;
    /* 此处需要将精灵图制作为二倍精灵图 */
    background: url(../images/jd-sprites.png) no-repeat -81px 0;
    /* 原图大小为400px */
    background-size: 200px auto;
}

在这里插入图片描述

添加banner图 并更改搜索框定位为fixed

在这里插入图片描述

修改搜索框定位使其在banner图上方

.search-wrap {
    /* 固定盒子需要有100%宽  */
    position: fixed;
    overflow: hidden;
    width: 100%;
    height: 44px;
    min-width: 320px;
    max-width: 640px;
}

在这里插入图片描述

去除图片底下的空白缝隙

img {
    vertical-align: top;
}

品牌日板块

在这里插入图片描述

/* 品牌日 */
.brand {
    overflow: hidden;
    border-radius: 10px 10px 0 0;
}
.brand div {
    float: left;
    width: 33.33%;
}
.brand div img {
    width: 100%;
}

6.图片格式

在这里插入图片描述

导航栏板块

移动端 流式布局只考虑宽度

nav {
    padding: 5px;
}

nav a {
    float: left;
    width: 20%;
    text-align: center;
}

nav a img {
    width: 40px;
    margin: 10px 0;
}

nav a span {
    display: block;
}

为了方便模拟拖动给body加2000px高度

此时会发现搜索框由于固定定位显示在屏幕中 后期用js解决

在这里插入图片描述

在这里插入图片描述

新闻板块

注意事项 加竖线时添加的边框属性会撑开原来的百分比宽度

但使用CSS中的box-sizing : border-content; 就不会出现这种情况

/* 新闻板块 */

.news {
    margin-top: 20px;
}

.news img {
    width: 100%;
}

.news a {
    float: left;
    box-sizing: border-box;
}

.news a:nth-child(1) {
    width: 50%;
}


/* n+2 就是从第2个往后面选 */

.news a:nth-child(n+2) {
    width: 25%;
    /* 直接加边框的话会打破百分比  但使用CSS3的盒子模型就不会出现撑开盒子的问题*/
    border-left: 1px solid #ccc;
}

案例要点 :nth-child(n+x) 选中x及之后的

CSS3盒子模型 box-sizing :border-content; 不会撑开盒子

特殊样式

/* 清楚点击高亮效果    设置为transparent 透明*/
* {
    -webkit-tap-highlight-color: transparent;
}
/* 在移动端浏览器默认的外观在ios上加上更改输入按钮和输入框自定义样式 */
input {
    -webkit-appearance: none;
    appearance: none;
}
/* 禁用长按页面时的弹出菜单 */
img,
a {
    -webkit-touch-callout: none;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值