【vue3.0】4.0 某东到家(四)——搜索栏、广告位横幅和分类菜单
目前src/App.vue
代码如下:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
</div>
<div class="docker">
<span class="docker__item docker__item--active">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-home"></i>
</div>
<div class="docker__item__title">首页</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-shopping"></i>
</div>
<div class="docker__item__title">购物车</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-order"></i>
</div>
<div class="docker__item__title">订单</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-my"></i>
</div>
<div class="docker__item__title">我的</div>
</span>
</div>
<router-view />
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
position: absolute;
left: 0;
top: 0;
bottom: 0.5rem;
right: 0;
padding: 0 0.18rem;
}
.position {
//文字显示省略号配置
@include ellipsis;
// 其他一般设定
position: relative;
padding: 0.16rem 0.24rem 0.16rem 0;
line-height: 0.22rem;
font-size: 0.16rem;
color: $content-font-color;
.position__icon {
position: relative;
top: 0.01rem;
font-size: 0.2rem;
margin-right: 0.08rem;
}
.position__notice {
top: 0.17rem;
right: 0;
position: absolute;
font-size: 0.2rem;
}
}
.docker {
color: $content-font-color;
display: flex; //自适应均赠,弹性盒子
position: absolute; //绝对定位
box-sizing: border-box; //这个会以body的最外层作为容器的最外层
padding: 0 0.18rem;
left: 0;
bottom: 0;
width: 100%;
height: 0.49rem;
border-top: 1px solid #f1f1f1;
&__item {
flex: 1;
text-align: center;
&--active {
color: #1fa4fc;
}
&_icon {
margin: 0.05rem 0 0.02rem 0; //图标距离上边距7px左右;
font-size: 0.18rem;
}
&__title {
//浏览器最小像素是12px,如果想表达12px以下大小得如下编写
font-size: 0.12rem;
transform: scale(0.5 0.5); //横向缩小50% 纵向搜小50%
transform-origin: center top; //缩放的中心点
}
}
}
</style>
搜索栏
增加一个装搜索栏的盒子
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
黄焖鸡米饭
</div>
</div>
......
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
......
}
......
.position {
......
}
.search {
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
}
.docker {
......
}
</style>
继续优化,去iconfont准备一个搜索按钮的图标:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span> <i class="custom-icon custom-icon-search"></i> </span>
黄焖鸡米饭
</div>
</div>
......
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
......
}
.position {
......
}
.search {
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
border-radius: 0.16rem;
font-size: 0.14rem;
}
......
</style>
微调css样式:
.search {
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
border-radius: 0.16rem;
font-size: 0.14rem;
.custom-icon{
display: inline-block;
padding: 0 0.05rem 0 0.16rem;
font-size: 0.16rem;
}
}
目前来看效果达到了,但其实文字
黄焖鸡米饭
是没有做限制的,比如垂直居中(现在的效果只是刚刚好,没有走样,但无法保证)。优化一下:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon"> <i class="custom-icon custom-icon-search"></i> </span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
</div>
......
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
......
}
.position {
......
}
.search {
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
border-radius: 0.16rem;
font-size: 0.14rem;
&__icon {
display: inline-block;
padding: 0 0.05rem 0 0.16rem;
font-size: 0.15rem;
}
&__text {
display: inline-block;
font-size: 0.14rem;
}
}
......
</style>
广告位横幅
准备一张图片,放到public\i18n\9_16\img\banner.jpg
修改 src/App.vue
:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon"> <i class="custom-icon custom-icon-search"></i> </span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<div class="banner">
<img src="/i18n/9_16/img/banner.jpg"/>
</div>
</div>
<style>
......
</template>
<style lang="scss">
......
.search {
margin-bottom: 0.12rem;
......
}
......
</style>
很明显图片太大,优化:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon">
<i class="custom-icon custom-icon-search"></i>
</span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<div class="banner">
<img class="banner__img" src="/i18n/9_16/img/banner.jpg" />
</div>
</div>
......
</template>
<style lang="scss">
......
.search {
......
}
.banner {
&__img {
width: 100%;
}
}
......
</style>
这样写效果就达到了,但是这里图片加载的时候会有一个抖动,可以模拟观察一下,
fast-3G
是一个模拟慢网速的情况下网页的访问:这里还不明显,当图片加载前后,图片下面的内容会被挤下去,这就是
抖动
。解决技巧:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon">
<i class="custom-icon custom-icon-search"></i>
</span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<div class="banner">
<img class="banner__img" src="/i18n/9_16/img/banner.jpg" />
</div>
<div>123123123</div>
</div>
......
</template>
<style lang="scss">
......
.search {
......
}
.banner {
height: 0%;
overflow: hidden;
padding-bottom: 25%; //屏幕宽度的25%
&__img {
width: 100%;
}
}
......
</style>
到此为止,
src/App.vue
的完整代码如下:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon">
<i class="custom-icon custom-icon-search"></i>
</span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<div class="banner">
<img class="banner__img" src="/i18n/9_16/img/banner.jpg" />
</div>
</div>
<div class="docker">
<span class="docker__item docker__item--active">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-home"></i>
</div>
<div class="docker__item__title">首页</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-shopping"></i>
</div>
<div class="docker__item__title">购物车</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-order"></i>
</div>
<div class="docker__item__title">订单</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-my"></i>
</div>
<div class="docker__item__title">我的</div>
</span>
</div>
<router-view />
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
position: absolute;
left: 0;
top: 0;
bottom: 0.5rem;
right: 0;
padding: 0 0.18rem;
}
.position {
//文字显示省略号配置
@include ellipsis;
// 其他一般设定
position: relative;
padding: 0.16rem 0.24rem 0.16rem 0;
line-height: 0.22rem;
font-size: 0.16rem;
color: $content-font-color;
.position__icon {
position: relative;
top: 0.01rem;
font-size: 0.2rem;
margin-right: 0.08rem;
}
.position__notice {
top: 0.17rem;
right: 0;
position: absolute;
font-size: 0.2rem;
}
}
.search {
margin-bottom: 0.12rem;
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
border-radius: 0.16rem;
font-size: 0.14rem;
&__icon {
display: inline-block;
padding: 0 0.05rem 0 0.16rem;
font-size: 0.15rem;
}
&__text {
display: inline-block;
font-size: 0.14rem;
}
}
.banner {
height: 0%;
overflow: hidden;
padding-bottom: 25%; //屏幕宽度的25%
&__img {
width: 100%;
}
}
.docker {
color: $content-font-color;
display: flex; //自适应均赠,弹性盒子
position: absolute; //绝对定位
box-sizing: border-box; //这个会以body的最外层作为容器的最外层
padding: 0 0.18rem;
left: 0;
bottom: 0;
width: 100%;
height: 0.49rem;
border-top: 1px solid #f1f1f1;
&__item {
flex: 1;
text-align: center;
&--active {
color: #1fa4fc;
}
&_icon {
margin: 0.05rem 0 0.02rem 0; //图标距离上边距7px左右;
font-size: 0.18rem;
}
&__title {
//浏览器最小像素是12px,如果想表达12px以下大小得如下编写
font-size: 0.12rem;
transform: scale(0.5 0.5); //横向缩小50% 纵向搜小50%
transform-origin: center top; //缩放的中心点
}
}
}
</style>
分类菜单
首先准备一下图片:
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
6 | 7 | 8 | 9 | 10 |
将图片放至/i18n/9_16/img/category_list
文件夹下:
<template>
<div class="wrapper">
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<div class="search">
<span class="search__icon">
<i class="custom-icon custom-icon-search"></i>
</span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<div class="banner">
<img class="banner__img" src="/i18n/9_16/img/banner.jpg" />
</div>
<div class="category_list">
<!-- 10个 -->
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/超市.png"/>
<p class="category_list__item__desc">超市便利</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/菜市场.png" />
<p class="category_list__item__desc">菜市场</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/水果店.png" />
<p class="category_list__item__desc">水果店</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/鲜花.png" />
<p class="category_list__item__desc">鲜花绿植</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/医药健康.png" />
<p class="category_list__item__desc">医药健康</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/家居.png" />
<p class="category_list__item__desc">家居时尚</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/蛋糕.png" />
<p class="category_list__item__desc">烘焙蛋糕</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/签到.png" />
<p class="category_list__item__desc">签到</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/大牌免运.png" />
<p class="category_list__item__desc">大牌免运</p>
</div>
<div class="category_list__item">
<img class="category_list__item__img" src="/i18n/9_16/img/category_list/红包.png" />
<p class="category_list__item__desc">红包套餐</p>
</div>
</div>
</div>
......
</template>
效果如下:
调整一下css:
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
......
}
.position {
......
}
.search {
......
}
.banner {
......
}
.category_list {
display: flex; //flex布局
&__item {
width: 20%;
}
}
.docker {
......
}
</style>
修改css属性:
.category_list {
display: flex; //flex布局:弹性盒
flex-wrap: wrap;//让弹性盒元素在必要的时候拆行
&__item {
width: 20%;
}
}
细节优化:
.category_list {
display: flex; //flex布局:弹性盒
flex-wrap: wrap;//让弹性盒元素在必要的时候拆行
&__item {
width: 20%;
&__img{
display: block;
width: 0.4rem;
height: 0.4rem;
margin: 0 auto;
}
}
}
再进行位置微调细化:
.category_list {
display: flex; //flex布局:弹性盒
flex-wrap: wrap; //让弹性盒元素在必要的时候拆行
margin-top: 0.16rem;
&__item {
width: 20%;
&__img {
display: block;
width: 0.4rem;
height: 0.4rem;
margin: 0 auto;
}
&__desc {
margin: 0.06rem 0 0.16rem 0;
text-align: center;
color: $content-font-color;
}
}
}
底部增加一个灰色空白区域:
<template>
<!-- 主体内容容器 -->
<div class="wrapper">
......
</div>
<!-- 搜索栏 -->
<div class="search">
......
</div>
<!-- 横幅广告推广栏 -->
<div class="banner">
......
</div>
<!-- 分类列表栏 -->
<div class="category_list">
......
</div>
<!-- 灰色留白分隔栏 -->
<div class="gap"></div>
</div>
.....
</template>
<style lang="scss">
.gap {
height: 0.1rem;
background:#f1f1f1;
}
......
</style>
现在这样的灰色留办由于
wrapper
的padding留白,没有顶格,而我们需要顶格的效果,修改如下:
.gap {
height: 0.1rem;
background: #f1f1f1;
margin: 0 -0.18rem;
}
给margin往外去撑即可解决。
到此为止
#f1f1f1
这个颜色出现2次了,而且可见的知道这个灰色会很常用,所以可以把这个颜色抽取出来,方便复用。修改
src\style\viriables.scss
/**
* 内容主体文字颜色
**/
$content-font-color: #333;
/**
* 无内容、背景灰、留白灰的颜色
**/
$content-bg-color: #f1f1f1;
到此为止,完整代码如下:
src/App.vue
:
<template>
<!-- 主体内容容器 -->
<div class="wrapper">
<!-- 顶部位置信息、通知栏 -->
<div class="position">
<span>
<i class="position__icon custom-icon custom-icon-position"></i>
</span>
西安交通大学兴庆校区梧桐苑食堂北侧小院子放台阶上
<i class="position__notice custom-icon custom-icon-bell"></i>
</div>
<!-- 搜索栏 -->
<div class="search">
<span class="search__icon">
<i class="custom-icon custom-icon-search"></i>
</span>
<span class="search__text"> 黄焖鸡米饭</span>
</div>
<!-- 横幅广告推广栏 -->
<div class="banner">
<img class="banner__img" src="/i18n/9_16/img/banner.jpg" />
</div>
<!-- 分类列表栏 -->
<div class="category_list">
<!-- 10个 -->
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/超市.png"
/>
<p class="category_list__item__desc">超市便利</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/菜市场.png"
/>
<p class="category_list__item__desc">菜市场</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/水果店.png"
/>
<p class="category_list__item__desc">水果店</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/鲜花.png"
/>
<p class="category_list__item__desc">鲜花绿植</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/医药健康.png"
/>
<p class="category_list__item__desc">医药健康</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/家居.png"
/>
<p class="category_list__item__desc">家居时尚</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/蛋糕.png"
/>
<p class="category_list__item__desc">烘焙蛋糕</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/签到.png"
/>
<p class="category_list__item__desc">签到</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/大牌免运.png"
/>
<p class="category_list__item__desc">大牌免运</p>
</div>
<div class="category_list__item">
<img
class="category_list__item__img"
src="/i18n/9_16/img/category_list/红包.png"
/>
<p class="category_list__item__desc">红包套餐</p>
</div>
</div>
<!-- 灰色留白分隔栏 -->
<div class="gap"></div>
</div>
<!-- 底部主菜单容器 -->
<div class="docker">
<span class="docker__item docker__item--active">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-home"></i>
</div>
<div class="docker__item__title">首页</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-shopping"></i>
</div>
<div class="docker__item__title">购物车</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-order"></i>
</div>
<div class="docker__item__title">订单</div>
</span>
<span class="docker__item ">
<div class="docker__item_icon">
<i class="custom-icon custom-icon-my"></i>
</div>
<div class="docker__item__title">我的</div>
</span>
</div>
<router-view />
</template>
<style lang="scss">
@import './style/viriables';
@import './style/mixins';
.wrapper {
position: absolute;
left: 0;
top: 0;
bottom: 0.5rem;
right: 0;
padding: 0 0.18rem;
}
.position {
//文字显示省略号配置
@include ellipsis;
// 其他一般设定
position: relative;
padding: 0.16rem 0.24rem 0.16rem 0;
line-height: 0.22rem;
font-size: 0.16rem;
color: $content-font-color;
.position__icon {
position: relative;
top: 0.01rem;
font-size: 0.2rem;
margin-right: 0.08rem;
}
.position__notice {
top: 0.17rem;
right: 0;
position: absolute;
font-size: 0.2rem;
}
}
.search {
margin-bottom: 0.12rem;
line-height: 0.32rem; //行高:将会自动撑开
background: #f5f5f5;
color: #b7b7b7;
border-radius: 0.16rem;
font-size: 0.14rem;
&__icon {
display: inline-block;
padding: 0 0.05rem 0 0.16rem;
font-size: 0.15rem;
}
&__text {
display: inline-block;
font-size: 0.14rem;
}
}
.banner {
height: 0%;
overflow: hidden;
padding-bottom: 25%; //屏幕宽度的25%
&__img {
width: 100%;
}
}
.category_list {
display: flex; //flex布局:弹性盒
flex-wrap: wrap; //让弹性盒元素在必要的时候拆行
margin-top: 0.16rem;
&__item {
width: 20%;
&__img {
display: block;
width: 0.4rem;
height: 0.4rem;
margin: 0 auto;
}
&__desc {
margin: 0.06rem 0 0.16rem 0;
text-align: center;
color: $content-font-color;
}
}
}
.gap {
height: 0.1rem;
background: $content-bg-color;
margin: 0 -0.18rem;
}
.docker {
color: $content-font-color;
display: flex; //自适应均赠,弹性盒子
position: absolute; //绝对定位
box-sizing: border-box; //这个会以body的最外层作为容器的最外层
padding: 0 0.18rem;
left: 0;
bottom: 0;
width: 100%;
height: 0.49rem;
border-top: 1px solid $content-bg-color;
&__item {
flex: 1;
text-align: center;
&--active {
color: #1fa4fc;
}
&_icon {
margin: 0.05rem 0 0.02rem 0; //图标距离上边距7px左右;
font-size: 0.18rem;
}
&__title {
//浏览器最小像素是12px,如果想表达12px以下大小得如下编写
font-size: 0.12rem;
transform: scale(0.5 0.5); //横向缩小50% 纵向搜小50%
transform-origin: center top; //缩放的中心点
}
}
}
</style>
最终效果如下:
发表评论 (审核通过后显示评论):