一、珠峰课堂页面及导航的配置
1.1创建三个页面,index,user,classify
1.2官网-框架-pages.json-pages
uni-app 通过
pages
节点配置应用由哪些页面组成
,pages 节点接收一个数组,数组每个项都是一个对象
"pages": [
//pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "首页", //设置页面标题文字
"navigationStyle": "custom"
//导航栏样式,仅支持 default/custom。custom即取消默认的原生导航栏,
}
},
{
"path": "pages/classify/classify",
"style": {
"navigationBarTitleText": "课程中心" //设置页面标题文字
}
},
{
"path": "pages/user/user",
"style": {
"navigationBarTitleText": "用户中心" //设置页面标题文字
}
}
],
1.3官网-框架-pages.json-tabBar
如果应用是一个多 tab 应用,可以通过
tabBar
配置项指定 tab 栏的表现
,以及tab 切换时显示的对应页
。
"tabBar": {
"color": "#333", //tab 上的文字默认颜色
"selectedColor": "#30d4e9", //tab 上的文字选中时的颜色
"backgroundColor": "#ffffff",
"list": [{
"pagePath": "pages/index/index",
"iconPath": "static/home.png",
"selectedIconPath": "static/home-select.png",
"text": "首页"
},
{
"pagePath": "pages/user/user",
"iconPath": "static/list.png",
"selectedIconPath": "static/list-select.png",
"text": "课程中心"
},
{
"pagePath": "pages/classify/classify",
"iconPath": "static/user.png",
"selectedIconPath": "static/user-select.png",
"text": "用户中心"
}
]
}
1.4页面表现
二、自定义导航栏组件及页面的常用钩子函数
2.1取消默认的原生导航栏
2.2创建组件
2.3引入组件
通过2.2创建组件,不用import引入,可以直接使用
2.4H5效果
2.5小程序效果
2.6在小程序中,navBar向下移动一个状态栏的高度
官网-API-设备-系统信息-uni.getSystemInfoSync()
获取系统信息同步接口。
statusBarHeight 状态栏的高度
结果:
H5页面:0
小程序:20
2.7新建一个view,高度为statusBarHeight,撑开状态栏的高度
<template>
<view class="top_nav_box">
<!-- 新建view,撑开状态栏的高度 -->
<view :style="{height:statusBarHeight+'px'}"></view>
<view class="nav_box">
导航
</view>
</view>
</template>
<script>
export default {
data() {
return {
statusBarHeight:0
};
},
created() {
console.log(uni.getSystemInfoSync());
this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
}
}
</script>
<style lang="less" scoped>
.top_nav_box{
background: #ccc;
}
</style>
2.8添加back按钮
2.9图标使用
2.9.1百度iconfont,找到阿里巴巴矢量图标库官网,然后注册登录,或者用github登录也行,此步骤跳过
2.9.2:找到图标管理->我的项目->然后新建项目
2.9.3右边点击新建项目,用于保存自己常用的图标
2.9.3项目新建完成后,往项目里添加我们要想使用的图标,找到图标库,搜索一个想要的图标,然后添加到购物车
2.9.4点击加入购物车
2.9.5添加到购物车完成后,购物车徽章数字应该显示1了,点击右上角的购物车图标,选择添加至项目,选择我们刚刚创建的项目,确定
2.9.6自动跳转到对应的项目里了,如图
2.9.7复制代码
2.9.8使用:
<text class="iconfont iconback"></text>
2.10navBar中间的文字由navBar组件传入
2.11插槽
2.11.1使用slot
2.11.2 slot里面是默认值
2.12控制navBar是否需要左侧的返回按钮
2.13index页面滚动的时候,navBar的背景色改变
官网-框架-框架接口-生命周期-页面生命周期-onPageScroll
2.13.2代码
<template>
<view class="content">
<navBar :opa="opa" :left="true">首页</navBar>
</view>
</template>
<script>
export default {
data() {
return {
opa: 0,
flag: false
}
},
//页面生命周期,需要写在页面
// onPageScroll: function(e) { //nvue暂不支持滚动监听,可用bindingx代替
//console.log("滚动距离为:" + e.scrollTop);
onPageScroll: function({scrollTop}) {
// console.log("滚动距离为:" + scrollTop);
if (scrollTop > 5) {
this.opa = 1;
} else {
this.opa = 0;
}
// if (this.flag) {
// this.flag = false;
// setTimeout(() => {
// this.flag = true;
// if (scrollTop > 5) {
// this.opa = 1;
// } else {
// this.opa = 0;
// }
// }, 200)
// }
},
methods: {
}
}
</script>
<style lang="less" scoped>
.content {
height: 2000rpx;
background: pink;
}
</style>
<template>
<view class="top_nav_box">
<!-- 新建view,撑开状态栏的高度 -->
<view :class="{opa:opa}" :style="{'height':statusBarHeight+'px'}"></view>
<view :class="{opa:opa}" class="nav_box">
<text class="iconfont " :class="{ iconback: left }"></text>
<view class="text_box">
<slot>默认的头部</slot>
</view>
<text></text>
</view>
</view>
</template>
<script>
export default {
props: {
left: {
type: Boolean,
default: false,
},
opa:{
type:Number,
default:0
}
},
data() {
return {
statusBarHeight: 0
};
},
created() {
console.log(uni.getSystemInfoSync());
// this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
}
}
</script>
<style lang="less" scoped>
.top_nav_box {
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 100;
.nav_box {
height: 45px;
padding: 5px;
background: rgba(0, 0, 0, 0);
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
}
}
.opa {
background: #eee!important;
}
</style>
三、uni-app常用组件及组件的二次封装
3.1使用swiper組件
官网-组件-视图容器-swiper
3.2swiper组件需要往下移动
navBar是fixed状态,整个swiper往上错,所以现在整个要往下错
3.3在fixed的view外面加一个view
<template>
<view :style="{'height':statusBarHeight + 45+ 'px'}">
<view class="top_nav_box">
<!-- 新建view,撑开状态栏的高度 -->
<view :class="{opa:opa}" :style="{height:statusBarHeight+'px'}"></view>
<view :class="{opa:opa}" class="nav_box">
<text class="iconfont " :class="{'iconback': left }"></text>
<view class="text_box">
<slot>默认的头部</slot>
</view>
<text></text>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
left: {
type: Boolean,
default: false,
},
opa:{
type:Number,
default:0
}
},
data() {
return {
statusBarHeight: 0
};
},
created() {
console.log(uni.getSystemInfoSync());
// this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
}
}
</script>
<style lang="less" scoped>
.top_nav_box {
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 100;
.nav_box {
height: 45px;
padding: 5px;
background: rgba(0, 0, 0, 0);
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
}
}
.opa {
background: #eee!important;
}
</style>
3.4轮播图数据
3.5扩展组件
官网-组件-拓展组件
账号:qq邮箱
密码:dandan020800
四、uni-app中使用条件编译实现多端匹配
官网-介绍-条件编译
html、js、css都可以使用
<!-- #ifdef H5 -->
仅出现在 H5 平台下的代码
<view class="hah">
哈哈哈哈哈
</view>
<!-- #endif -->
<!-- #ifndef H5 -->
除了 H5 平台,其它平台均存在的代码
<view class="hah">
非H5
</view>
<!-- #endif -->
H5的结果:
小程序的结果