五、微信小程序组件
微信小程序框架为开发者提供了一系列组件,开发者可以通过组合这些组件进行快速开发。
(1)微信小程序组件基础概念
- 什么是组件?
-
组件是视图层的基本组成单元。
-
组件自带一些功能与微信风格的样式。
-
一个组件通常包括
开始标签
和结束标签
,属性
用来修饰这个组件,内容
在两个标签之内。<tagname property="value"> Content goes here ... </tagename>
注意:所有组件与属性都是小写,以连字符
-
连接
- 基础组件都有哪些分类?
主要包含:视图容器、基础内容、表单组件、导航、媒体组件、地图、画布、开放能力等使用场景
- 组件共同属性有哪些?
属性名 | 类型 | 描述 | 注解 |
---|---|---|---|
id | String | 组件的唯一标示 | 保持整个页面唯一 |
class | String | 组件的样式类 | 在对应的wxss中定义的样式类 |
style | String | 组件的内联样式 | 可以动态设置的内联样式 |
hidden | Boolean | 组件是否显示 | 所有组件默认显示 |
data-* | Any | 自定义属性 | 组件上触发的事件时,会发送给事件处理函数 |
bind* / catch* | EventHandler | 组件的事件 |
- 单个组件自定义特殊属性
几乎所有组件都有各自定义的属性,可以对该组件的功能或样式进行修饰。
<view class="container">
<scroll-view
class="scroll-boxa"
scroll-y="true"
upper-threshold="50"
lower-threshold="50"
scroll-top="50"
bindscrolltoupper="scrollToUpper"
bindscrolltolower="scrollToLower"
bindscroll="scrollhandle"
refresher-enabled="true"
refresher-triggered="true"
refresher-background="#cba007"
bindrefresherpulling="refresherPulling"
>
<view class="boxa">第 1 个盒子</view>
<view class="boxa">第 2 个盒子</view>
<view class="boxa">第 3 个盒子</view>
<view class="boxa">第 4 个盒子</view>
<view class="boxa">第 5 个盒子</view>
<view class="boxa">第 6 个盒子</view>
</scroll-view>
<view style="padding:80rpx 30rpx;">{
{msg}}</view>
</view>
data: {
msg:'滚动状态提示信息'
},
scrollToUpper(){
console.log("滚动到顶部了")
this.setData({
msg:'滚动到顶部了'
})
},
scrollToLower(){
console.log("滚动到底部了")
this.setData({
msg:'滚动到底部了'
})
},
scrollhandle(event){
console.log(event.detail.scrollTop)
this.setData({
msg:'滚动中,滚动距离为:'+event.detail.scrollTop
})
},
refresherPulling(){
console.log("顶部下拉刷新中...")
this.setData({
msg:'顶部下拉刷新中...'
})
},
.container{
background: #abcdef;
}
.scroll-boxa{
width: 100%;
height: 800rpx;
background: #007cba;
color: #fff;
}
.boxa{
width: 80%;
height: 300rpx;
line-height: 300rpx;
text-align: center;
margin: 4rpx 10%;
background: red;
}
(2)使用 cover-view 解决原生组件层级过高问题
覆盖在原生组件之上的文本视图。
可覆盖的原生组件包括 map、video、canvas、camera、live-player、live-pusher
只支持嵌套 cover-view、cover-image,可在 cover-view 中使用 button。
<view class="container">
<view class="page-body">
<view class="page-section page-section-gap">
<map
style="width: 100%; height: 300px;"
latitude="{
{latitude}}"
longitude="{
{longitude}}"
>
<cover-view class="cover-view">
<cover-view class="container">
<cover-view class="flex-wrp" style="flex-direction:row;">
<cover-view class="flex-item demo-text-1"></cover-view>
<cover-view class="flex-item demo-text-2"></cover-view>
<cover-view class="flex-item demo-text-3"></cover-view>
</cover-view>
</cover-view>
</cover-view>
</map>
<video
id="myVideo"
src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"
enable-danmu
danmu-btn
show-center-play-btn='{
{false}}'
show-play-btn="{
{true}}"
controls
style="width:80%;margin:80rpx 10%;"
></video>
<mfooter num="888" min="8" />
</view>
</view>
</view>
Page({
onShareAppMessage() {
return {
title: '自定义分享',
path: 'comp/cover-view/index'
}
},
data: {
latitude: 23.099994,
longitude: 113.324520,
}
})
.cover-view {
position: absolute;
top: calc(50% - 150rpx);
left: calc(50% - 300rpx);
/* opacity: .7; */
}
.flex-wrp{
display:flex;
}
.flex-item{
width: 200rpx;
height: 300rpx;
font-size: 26rpx;
}
.demo-text-1 {
background: rgba(26, 173, 25, 0.7);
}
.demo-text-2 {
background: rgba(39, 130, 215, 0.7);
}
.demo-text-3 {
background: rgba(255, 255, 255, 0.7);
}
{
"usingComponents": {
"mfooter": "/components/mfooter/index"
}
}
(3)使用 swiper 组件实现轮播图及左右滑动切换效果
<view>
<swiper
class="mswiper"
indicator-dots="{
{true}}"
indicator-color="#fff"
indicator-active-color="#666"
autoplay="true"
current="0"
interval="2000"
duration="300"
display-multiple-items="1"
bindchange="swiperChange"
>
<block wx:for="{
{swiperList}}" wx:for-item="item" wx:for-index="index" wx:key="*this">
<swiper-item class="mswiper-item mswiper-item-{
{index}}">
{
{item}}
</swiper-item>
</block>
</swiper>
<view style="text-align:center;">当前显示的索引模块是:{
{currentIndex}}</view>
</view>
Page({
data:{
currentIndex: 0,
swiperList:[
'Page 1',
'Page 2',
'Page 3'
]
},
swiperChange(event){
console.log(event.detail.current)
this.setData({
currentIndex: event.detail.current
})
}
})
.mswiper{
height: 400rpx;
width: 600rpx;
margin:60rpx 72rpx;
border: 3rpx solid #ccc;
text-align: center;
color: #fff;
}
.mswiper-item{
line-height: 400rpx;
}
.mswiper-item-0{
background-color: red;
}
.mswiper-item-1{
background-color: green;
}
.mswiper-item-2{
background-color: blue;
}