从0到一开发微信小程序(4)—小程序组件

其他相关文章

从0到一开发微信小程序(1)——申请账号并安装开发环境
从0到一开发微信小程序(2)——开发第一个小程序
从0到一开发微信小程序(3)—小程序框架配置
从0到一开发微信小程序(4)—小程序组件
从0到一开发微信小程序(5)—小程序WXML
从0到一开发微信小程序(6)—小程序常用API
从0到一开发微信小程序(7)—小程序组件库(提高开发效率)
从0到一开发微信小程序(8)—实战一个商城项目——正在书写中

1、组件

1.1、基础视图

小程序中常用的基础视图,有三种,分别是view,text,image
view:视图容器
text:文本
image:图片。支持 JPG、PNG、SVG、WEBP、GIF 等格式,2.3.0 起支持云文件ID。

1.1.1、view

view的显示效果跟div是一样的,我们可以理解为它就是一个html中的div,它属于块级元素

1.1.1.1、属性说明
属性类型默认值必填说明
hover-classstringnone指定按下去的样式类。当 hover-class=“none” 时,没有点击态效果
hover-stop-propagationbooleanfalse指定是否阻止本节点的祖先节点出现点击态
hover-start-timenumber50按住后多久出现点击态,单位毫秒
hover-stay-timenumber400手指松开后点击态保留时间,单位毫秒

注意:如果需要滚动视图,请使用scroll-view
测试代码:
新建一个views页面,在views.wxml中添加:

<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="page-section-title">
        <text>flex-direction: row\n横向布局</text>
      </view>
      <view class="page-section-spacing">
        <view class="flex-wrp" style="flex-direction:row;">
          <view class="flex-item demo-text-1">A</view>
          <view class="flex-item demo-text-2">B</view>
          <view class="flex-item demo-text-3">C</view>
        </view>
      </view>
    </view>
  </view>
</view>

展示效果:
在这里插入图片描述

1.1.2、text

文本,承载页面文本信息,类似span的功能

<text>文本1</text>
<text>文本2</text>

text 是行内元素
温馨提示:

  1. text 组件内只支持 text 嵌套
  2. 除了文本节点以外的其他节点都无法长按选中

1.1.3、image

图片。支持 JPG、PNG、SVG、WEBP、GIF 等格式

属性类型默认值必填说明
srcstring图片资源地址
modestringscaleToFill图片裁剪、缩放的模式
webpbooleanfalse默认不解析 webP 格式,只支持网络资源
lazy-loadbooleanfalse图片懒加载,在即将进入一定范围(上下三屏)时才开始加载
show-menu-by-longpressbooleanfalse长按图片显示发送给朋友、收藏、保存图片、搜一搜、打开名片/前往群聊/打开小程序(若图片中包含对应二维码或小程序码)的菜单。
binderroreventhandle当错误发生时触发,event.detail = {errMsg}
bindloadeventhandle当图片载入完毕时触发,event.detail = {height, width}

在上面中,model的属性值又有很多种

合法值说明
scaleToFill缩放模式,不保持纵横比缩放图片,使图片的宽高完全拉伸至填满 image 元素
aspectFit缩放模式,保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
aspectFill缩放模式,保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。
widthFix缩放模式,宽度不变,高度自动变化,保持原图宽高比不变
heightFix缩放模式,高度不变,宽度自动变化,保持原图宽高比不变
top裁剪模式,不缩放图片,只显示图片的顶部区域
bottom裁剪模式,不缩放图片,只显示图片的底部区域
center裁剪模式,不缩放图片,只显示图片的中间区域
left裁剪模式,不缩放图片,只显示图片的左边区域
right裁剪模式,不缩放图片,只显示图片的右边区域
top left裁剪模式,不缩放图片,只显示图片的左上边区域
top right裁剪模式,不缩放图片,只显示图片的右上边区域
bottom left裁剪模式,不缩放图片,只显示图片的左下边区域
bottom right裁剪模式,不缩放图片,只显示图片的右下边区域

show-menu-by-longpress的属性值对应的字段信息为:

支持长按识别的码

类型说明
小程序码
微信个人码
企业微信个人码
普通群码指仅包含微信用户的群
互通群码指既有微信用户也有企业微信用户的群
公众号二维码

提示:

  1. image组件默认宽度320px、高度240px
  2. image组件中二维码/小程序码图片不支持长按识别。仅在 wx.previewImage 中支持长按识别
  3. image组件进行缩放时,计算出来的宽高可能带有小数,在不同 webview 内核下渲染可能会被抹去小数部分

测试代码:
新增一个页面:image-view,然后在image-view.js中添加

Page({
  data: {
    array: [{
      mode: 'scaleToFill',
      text: 'scaleToFill:不保持纵横比缩放图片,使图片完全适应'
    }, {
      mode: 'aspectFit',
      text: 'aspectFit:保持纵横比缩放图片,使图片的长边能完全显示出来'
    }, {
      mode: 'aspectFill',
      text: 'aspectFill:保持纵横比缩放图片,只保证图片的短边能完全显示出来'
    }, {
      mode: 'top',
      text: 'top:不缩放图片,只显示图片的顶部区域'
    }, {
      mode: 'bottom',
      text: 'bottom:不缩放图片,只显示图片的底部区域'
    }, {
      mode: 'center',
      text: 'center:不缩放图片,只显示图片的中间区域'
    }, {
      mode: 'left',
      text: 'left:不缩放图片,只显示图片的左边区域'
    }, {
      mode: 'right',
      text: 'right:不缩放图片,只显示图片的右边边区域'
    }, {
      mode: 'top left',
      text: 'top left:不缩放图片,只显示图片的左上边区域'
    }, {
      mode: 'top right',
      text: 'top right:不缩放图片,只显示图片的右上边区域'
    }, {
      mode: 'bottom left',
      text: 'bottom left:不缩放图片,只显示图片的左下边区域'
    }, {
      mode: 'bottom right',
      text: 'bottom right:不缩放图片,只显示图片的右下边区域'
    }],
    src: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg'
  },
  imageError: function(e) {
    console.log('image3发生error事件,携带值为', e.detail.errMsg)
  }
})

在image-view.wxml中添加

<view class="page">
  <view class="page__hd">
    <text class="page__title">image</text>
    <text class="page__desc">图片</text>
  </view>
  <view class="page__bd">
    <view class="section section_gap" wx:for="{{array}}" wx:for-item="item">
      <view class="section__title">{{item.text}}</view>
      <view class="section__ctn">
        <image style="width: 200px; height: 200px; background-color: #eeeeee;" mode="{{item.mode}}" src="{{src}}"></image>
      </view>
    </view>
  </view>
</view>

展示效果:
在这里插入图片描述

1.2、滑件视图容器Swiper(焦点轮播图)

Swiper常用属性说明:

属性类型默认值必填说明
indicator-dotsbooleanfalse是否显示面板指示点
indicator-colorcolorrgba(0, 0, 0, .3)指示点颜色
indicator-active-colorcolor#000000当前选中的指示点颜色
autoplaybooleanfalse是否自动切换
intervalnumber5000自动切换时间间隔
durationnumber500滑动动画时长
circularbooleanfalse是否采用衔接滑动
verticalbooleanfalse

滑动视图相信大家应该看过,往左右拉的那种图吧,我们来实现一版,
简单版:新建一个swiper页面,再swiper.wxml中添加

<!-- swiper.wxml -->
<view>
  <swiper class="swiper">
    <swiper-item>
      <image mode="widthFix" src="../../images/1.jpg"></image>
    </swiper-item>
    <swiper-item>
      <image mode="widthFix" src="../../images/2.jpg"></image>
    </swiper-item>
    <swiper-item>
      <image mode="widthFix" src="../../images/3.jpg"></image>
    </swiper-item>
  </swiper>
</view>

在swiper.wxss中添加:(同时设置图片样式充满全屏,因为图片默认大小:宽度320px、高度240px)

/* swiper.wxss */
image{
  width: 100%;
}

展示效果:
在这里插入图片描述
在这里插入图片描述

一直向右拉,依次会出现1.jpg,2.jpg,3.jpg的图
复杂版:自动每隔5秒向下换图,新建页面swiper-complex,其中swiper-complex.wxml中添加

<!-- swiper-complex.wxml -->
<view>
  <swiper 
  class="swiper"
  indicator-dots="{{ swiperOptions.indicatorDots }}"
  indicator-color="{{ swiperOptions.indicatorColor }}"
  indicator-active-color="{{ swiperOptions.indicatorActiveColor }}"
  autoplay="{{ swiperOptions.autoplay }}"
  interval="{{ swiperOptions.interval }}"
  duration="{{ swiperOptions.duration }}"
  circular="{{ swiperOptions.circular }}"
  vertical="{{ swiperOptions.vertical }}"
  >
    <swiper-item>
      <image mode="widthFix" src="../../images/1.jpg"></image>
    </swiper-item>
    <swiper-item>
      <image mode="widthFix" src="../../images/2.jpg"></image>
    </swiper-item>
    <swiper-item>
      <image mode="widthFix" src="../../images/3.jpg"></image>
    </swiper-item>
  </swiper>
</view>

swiper-complex.js中添加

// swiper-complex.js
Page({
  data: {
    swiperOptions:{
      indicatorDots:true,
      indicatorColor:"#fff",
      indicatorActiveColor:"#f00",
      autoplay:true,
      interval:5000,
      duration:1000,
      circular:true,
      vertical:true
     }
   }
})

1.3、滚动视图区域(scroll-view)

功能描述

  • 可滚动视图区域。使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 height。组件属性的长度单位默认为px,2.4.0起支持传入单位(rpx/px)。

属性说明:

属性类型默认值必填说明
scroll-xbooleanfalse允许横向滚动
scroll-ybooleanfalse允许纵向滚动
upper-thresholdnumber/string50距顶部/左边多远时,触发 scrolltoupper 事件
lower-thresholdnumber/string50距底部/右边多远时,触发 scrolltolower 事件
scroll-topnumber/string设置竖向滚动条位置
scroll-leftnumber/string设置横向滚动条位置
scroll-into-viewstring值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
scroll-with-animationbooleanfalse在设置滚动条位置时使用动画过渡
enable-back-to-topbooleanfalseiOS点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向
enable-flexbooleanfalse启用 flexbox 布局。开启后,当前节点声明了 display: flex 就会成为 flex container,并作用于其孩子节点。
scroll-anchoringbooleanfalse开启 scroll anchoring 特性,即控制滚动位置不随内容变化而抖动,仅在 iOS 下生效,安卓下可参考 CSS overflow-anchor 属性。
enable-passivebooleanfalse开启 passive 特性,能优化一定的滚动性能
refresher-enabledbooleanfalse开启自定义下拉刷新
refresher-thresholdnumber45设置自定义下拉刷新阈值
refresher-default-stylestring“black”设置自定义下拉刷新默认样式,支持设置 black &#124; white &#124; none, none 表示不使用默认样式
refresher-backgroundstring“#FFF”设置自定义下拉刷新区域背景颜色
refresher-triggeredbooleanfalse设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发
enhancedbooleanfalse启用 scroll-view 增强特性,启用后可通过 ScrollViewContext
操作 scroll-view
bouncesbooleantrueiOS 下 scroll-view 边界弹性控制 (同时开启 enhanced 属性后生效)
show-scrollbarbooleantrue滚动条显隐控制 (同时开启 enhanced 属性后生效)
paging-enabledbooleanfalse分页滑动效果 (同时开启 enhanced 属性后生效)
fast-decelerationbooleanfalse滑动减速速率控制 (同时开启 enhanced 属性后生效)
binddragstarteventhandle滑动开始事件 (同时开启 enhanced 属性后生效) detail { scrollTop, scrollLeft }
binddraggingeventhandle滑动事件 (同时开启 enhanced 属性后生效) detail { scrollTop, scrollLeft }
binddragendeventhandle滑动结束事件 (同时开启 enhanced 属性后生效) detail { scrollTop, scrollLeft, velocity }
bindscrolltouppereventhandle滚动到顶部/左边时触发
bindscrolltolowereventhandle滚动到底部/右边时触发
bindscrolleventhandle滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}
bindrefresherpullingeventhandle自定义下拉刷新控件被下拉
bindrefresherrefresheventhandle自定义下拉刷新被触发
bindrefresherrestoreeventhandle自定义下拉刷新被复位
bindrefresheraborteventhandle自定义下拉刷新被中止

测试代码:
新建一个页面score-view,在score-view.js中添加

const order = ['demo1', 'demo2', 'demo3']

Page({
  onShareAppMessage() {
    return {
      title: 'scroll-view',
      path: '/pages/scroll-view/scroll-view'
    }
  },

  data: {
    toView: 'green'
  },

  upper(e) {
    console.log(e)
  },

  lower(e) {
    console.log(e)
  },

  scroll(e) {
    console.log(e)
  },

  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },

  tap() {
    for (let i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200
        })
        break
      }
    }
  },

  tapMove() {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  }
})

score-view.wxml中添加

<view class="container">
  <view class="page-body">
    <view class="page-section">
      <view class="page-section-title">
        <text>Vertical Scroll\n纵向滚动</text>
      </view>
      <view class="page-section-spacing">
        <scroll-view scroll-y="true" style="height: 300rpx;" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
          <view id="demo1" class="scroll-view-item demo-text-1"></view>
          <view id="demo2"  class="scroll-view-item demo-text-2"></view>
          <view id="demo3" class="scroll-view-item demo-text-3"></view>
        </scroll-view>
      </view>
    </view>
    <view class="page-section">
      <view class="page-section-title">
        <text>Horizontal Scroll\n横向滚动</text>
      </view>
      <view class="page-section-spacing">
        <scroll-view class="scroll-view_H" scroll-x="true" bindscroll="scroll" style="width: 100%">
          <view id="demo1" class="scroll-view-item_H demo-text-1"></view>
          <view id="demo2"  class="scroll-view-item_H demo-text-2"></view>
          <view id="demo3" class="scroll-view-item_H demo-text-3"></view>
        </scroll-view>
      </view>
    </view>
  </view>
</view>

score-view.wxss中添加

.page-section-spacing{
  margin-top: 60rpx;
}
.scroll-view_H{
  white-space: nowrap;
}
.scroll-view-item{
  height: 300rpx;
}
.scroll-view-item_H{
  display: inline-block;
  width: 100%;
  height: 300rpx;
}

展示效果:
在这里插入图片描述

1.4、图标(icon)

图标组件,其实就是字体图标效果,但是这里所提供的只有最常用的几个。
图标的使用方式为:

<icon type="xxx"></icon>

字体图标属性为:

属性类型默认值必填说明
typestringicon的类型,有效值:success, success_no_circle, info, warn, waiting, cancel, download, search, clear
sizenumber/string23icon的大小,单位默认为px,2.4.0起支持传入单位(rpx/px),2.21.3起支持传入其余单位(rem 等)。
colorstringicon的颜色,同 css 的color

新建一个页面icon,其中icon.wxml中添加:

<icon type="success" size="50" color="red"></icon>
<icon type="success_no_circle" size="50"></icon>
<icon type="info" size="50"></icon>
<icon type="warn" size="50"></icon>
<icon type="waiting" size="50"></icon>
<icon type="cancel" size="50"></icon>
<icon type="download" size="50"></icon>
<icon type="search" size="50"></icon>
<icon type="clear" size="50"></icon>

展示效果为:
在这里插入图片描述

1.5、表单(form )

  • 表单,将用户输入的信息提交到服务器
  • 小程序的表单与html的表单基本一致
  • 表单页面随处可见,比如:登录,注册,信息修改,添加等

我们这里拿一个常用的登录页面来说:

  • 我们实现一个简单的登录注册,一般需要三个组件:
    • form 表单
    • button 按钮
    • input 输入框

新建一个页面login,在login.wxml中添加:

<view class="login">
  <form>
    <input placeholder="请输入用户名" />
    <input placeholder="请输入密码" />
    <button type="primary">登录</button>
  </form>
</view>

在login.wxss中添加

.login{
  margin-top: 100rpx;
}
input{
  border: 1px solid #999;
  border-radius: 5px;
  margin: 10px;
  padding-left: 10px;
  height: 70rpx;
}

展示效果为:
在这里插入图片描述

1.6、按钮(button)

  • 小程序的 button 按钮与 html 的非常类似,但是小程序的功能要更强大一些

使用形式:

<button>按钮</button>

按钮属性:

属性类型默认值必填说明
typestringdefault按钮的样式类型
sizestringdefault按钮的大小
plainbooleanfalse按钮是否镂空,背景色透明
disabledbooleanfalse是否禁用
loadingbooleanfalse名称前是否带 loading 图标
form-typestring用于 form组件,点击分别会触发form组件的 submit/reset 事件

其中type属性又分为:

合法值说明
primary绿色
default白色
warn红色

size属性分为:

合法值说明
default默认大小
mini小尺寸

测试代码:

<button>按钮</button>
<button type="default">按钮</button>
<button type="primary">按钮</button>
<button type="warn">按钮</button>
<button type="primary" size="default">按钮</button>
<button type="primary" size="mini">按钮</button>
<button type="primary" plain>按钮</button>
<button type="primary" disabled>按钮</button>
<button type="primary" loading>按钮</button>
<button type="primary" form-type="submit">按钮</button>

展示效果:
在这里插入图片描述

1.7、输入框(input)

  • 输入框是input, 与html的输入框类似,但是增加了很多新的功能

使用形式:

<input/>

属性说明:

属性类型默认值必填说明
valuestring输入框的初始内容
placeholderstring输入框为空时占位符
passwordbooleanfalse是否是密码类型
disabledbooleanfalse是否禁用
maxlengthnumber140最大输入长度,设置为 -1 的时候不限制最大长度
focusbooleanfalse获取焦点
typestringtextinput 的类型
confirm-typestringdone设置键盘右下角按钮的文字,仅在type='text’时生效

type 属性详解

合法值说明
text文本输入键盘
number数字输入键盘
idcard身份证输入键盘
digit带小数点的数字键盘
nickname昵称输入键盘

confirm-type属性详解

合法值说明
send右下角按钮为“发送”
search右下角按钮为“搜索”
next右下角按钮为“下一个”
go右下角按钮为“前往”
done右下角按钮为“完成”

测试代码:

<input />
<input value="测试信息"/>
<input placeholder="请输入用户名"/>
<input placeholder="请输入密码" password/>
<input placeholder="请输入密码" disabled/>
<input placeholder="文本" maxlength="10"/>
<input placeholder="文本" focus/>
<input placeholder="文本" type="text"/>
<input placeholder="文本" type="number"/>
<input placeholder="文本" type="idcard"/>
<input placeholder="文本" type="digit"/>
<input placeholder="文本" type="nickname"/>
<input placeholder="文本" type="text" confirm-type="send"/>
<input placeholder="文本" type="text" confirm-type="search"/>
<input placeholder="文本" type="text" confirm-type="next"/>
<input placeholder="文本" type="text" confirm-type="go"/>
<input placeholder="文本" type="text" confirm-type="done"/>
input{
  border: 1px solid #999;
  height: 80rpx;
  margin: 10px;
  padding-left: 10px;
}

展示效果:
在这里插入图片描述

1.8、picker(滚动选择器)

  • 选择器的种类有很多,分别为:
    1. 普通选择器
    2. 多列选择器
    3. 时间选择器
    4. 日期选择器
    5. 省市区选择器

1.8.1、普通选择器

指定mode属性为selector,或者默认不指定mode

<view>普通选择器</view>
<picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">
  <view class="picker">
     当前选择:{{array[index]}}
  </view>
</picker>

选择器展示效果需要配合逻辑

Page({
  data: {
    array: ['美国', '中国', '巴西', '日本'],
    index: 0
   },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
     })
   }
})
展示效果:

在这里插入图片描述

1.8.2、多列选择器

指定mode属性为multiSelector

<view>多列选择器</view>
<picker mode="multiSelector" bindchange="bindMultiPickerChange" value="{{multiIndex}}" range="{{multiArray}}">
  <view class="picker">
     当前选择:{{multiArray[0][multiIndex[0]]}},{{multiArray[1][multiIndex[1]]}},{{multiArray[2][multiIndex[2]]}}
  </view>
</picker>
Page({
  data: {
    multiArray: [
       ['无脊柱动物', '脊柱动物'],
       ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'],
       ['猪肉绦虫', '吸血虫']
     ],
    multiIndex: [0, 0, 0],
   },
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
     })
   }
})
展示效果:

在这里插入图片描述

1.8.3、时间选择器

指定mode属性为time

<view>时间选择器</view>
<picker mode="time" value="{{time}}" start="09:01" end="21:01" bindchange="bindTimeChange">
  <view class="picker">
     当前选择: {{time}}
  </view>
</picker>
Page({
  data: {
    time: '12:01'
   },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
     })
   }
})
展示效果:

在这里插入图片描述

1.8.4、日期选择器

指定mode属性为date

<view>日期选择器</view>
<picker mode="date" value="{{date}}" start="2000-09-01" end="2030-09-01" bindchange="bindDateChange">
  <view class="picker">
     当前选择: {{date}}
  </view>
</picker>
Page({
  data: {
    date: '2030-09-01'
   },
  bindDateChange: function (e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
     })
   }
})
展示效果:

在这里插入图片描述

1.8.5、省市区选择器

指定mode属性为region

<view>省市区选择器</view>
<picker mode="region" bindchange="bindRegionChange" value="{{region}}">
  <view class="picker">
     当前选择:{{region[0]}},{{region[1]}},{{region[2]}}
  </view>
</picker>
Page({
  data: {
    region: ['广东省', '广州市', '海珠区']
   },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
     })
   }
})
展示效果:

在这里插入图片描述

1.9、slider(滑动选择器)

使用形式

<slider/>

常用属性

属性类型默认值必填说明
stepnumber1步长,取值必须大于 0,并且可被(max - min)整除
show-valuebooleanfalse是否显示当前 value
minnumber0最小值
maxnumber100最大值
disabledbooleanfalse是否禁用
valuenumber0当前取值
backgroundColorcolor#e9e9e9背景条的颜色
activeColorcolor#1aad19已选择的颜色
block-colorcolor#ffffff滑块的颜

测试代码:

<slider />
<slider step="20"/>
<slider show-value/>
<slider min="50" max="200" show-value/>
<slider min="50" max="200" show-value disabled/>
<slider show-value value="30"/>
<slider show-value value="30" backgroundColor="red"/>
<slider show-value value="30" backgroundColor="red" activeColor="blue"/>
<slider block-color="red"/>

展示效果:

在这里插入图片描述

1.10、表单其他组件

表单是常用的组件,同样,在表单中,也有很多配套的组件

  1. checkbox
  2. radio
  3. label
  4. switch
  5. textarea

checkbox(复选框 )

多选项目,与html复选框基本一致

<checkboxchecked=“true”/>选中

checked 表示初始状态为选中(true) 或 未选中(false)
配合 checkbox-group 形成一组

<checkbox-group>
  <checkbox checked="true" />读书
  <checkbox checked="true" />打游戏
  <checkbox />听音乐
</checkbox-group>

radio(单选项目)

单选项目,与html单选框基本一致

<radiochecked=“true”/>选中

checked 表示初始状态为选中(true) 或 未选中(false)
配合 radio-group 形成一组

<radio-group>
  <radio checked="true"/>选项1
  <radio checked="false"/>选项2
  <radio checked="false"/>选项3
  <radio checked="false"/>选项4
</radio-group>

label(标签)

用来改进表单组件的可用性,与html的label基本一致

<label for="menu">
  <checkbox id="menu" checked="true"/>选中
</label>

switch(开关选择器)

开关选择器,有着比较美观的展示效果

<switch/>

属性列表
属性类型默认值必填说明
checkedbooleanfalse是否选中
disabledbooleanfalse是否禁用
typestringswitch样式,有效值:switch, checkbox
colorstring#04BE02switch 的颜色,同 css 的 color
<switch />
<switch checked="true"/>
<switch checked="true" disabled/>
<switch checked="true" type="checkbox"/>
<switch checked="true" color="red"/>

textarea(多行输入框)

多行输入框,与html多行输入框基本一致

<textarea/>

为了可见性,我们需要增加样式

textarea{
  border: 1px solid red;
}
属性类型默认值必填说明
valuestring输入框的内容
placeholderstring输入框为空时占位符
disabledbooleanfalse是否禁用
maxlengthnumber140最大输入长度,设置为 -1 的时候不限制最大长度
focusbooleanfalse获取焦点
auto-heightbooleanfalse是否自动增高
<textarea value="文本内容" />
<textarea placeholder="占位符" />
<textarea maxlength="10" />
<textarea disabled />
<textarea focus />
<textarea auto-height/>

1.11、navigator(页面之间跳转)

navigator 实现页面之间的跳转

<navigator url="/pages/other/other">跳转其他页面</navigator>

常用属性说明

属性类型默认值必填说明
urlstring当前小程序内的跳转链接
open-typestringnavigate跳转方式,默认打开新页面,redirect 在当前页面打开

测试代码:

<navigator url="/pages/other/other" >跳转其他页面</navigator>
<navigator url="/pages/slider/slider" open-type="redirect">在当前页打开</navigator>

扩展:生命周期函数

onUnload 在之前的讲解中无法测试,现在有了navigator,我们可以进行测试了
在 navigator 的属性open-type设置为redirect时,我们可以观察输入结果

Page({
  onUnload() {
    console.log("卸载");
   }
})

1.12、audio(音乐)

音频播放

<audio src="https://music.163.com/song/media/outer/url?id=1961763339" controls></audio>

切换音乐

通过修改audio的属性,切换音乐

<audio 
  id="{{ audioOptions.id }}"
  poster="{{ audioOptions.poster }}" 
  name="{{ audioOptions.name }}" 
  author="{{ audioOptions.author }}" 
  src="{{ audioOptions.src }}"
  controls="{{ audioOptions.controls }}"
  loop="{{ audioOptions.loop }}">
</audio>
<button type="primary" bindtap="changeMusicHandle">切换</button>
Page({
  data: {
    audioOptions:{
      id:"myAudio",
      name:"妈妈的话",
      author:"zby忠宇",
      poster:"https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
      src:"https://music.163.com/song/media/outer/url?id=1961763339",
      controls:true,
      loop:true
     }
   },
  changeMusicHandle(){
    this.setData({
      audioOptions:{
        id:"myAudio",
        name:"时光洪流",
        author:"程响",
        poster:"https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
        src:"https://music.163.com/song/media/outer/url?id=1868943615",
        controls:true,
        loop:true
       }
     })
   }
})

1.13、video(视频)

视频播放

<video src="http://iwenwiki.com/api/livable/livable.mp4"></video>

为了美观,我们将视频宽度充满全屏

video{
  width: 100%;
}

属性说明

属性类型默认值必填说明
srcstring要播放视频的资源地址,支持网络路径、本地临时路径
durationnumber指定视频时长
controlsbooleantrue是否显示默认播放控件(播放/暂停按钮、播放进度、时间)
autoplaybooleanfalse是否自动播放
loopbooleanfalse是否循环播放
mutedbooleanfalse是否静音播放
initial-timenumber0指定视频初始播放位置
show-mute-btnbooleanfalse是否显示静音按钮
danmu-listArray弹幕列表
danmu-btnbooleanfalse是否显示弹幕按钮,只在初始化时有效,不能动态变更
enable-danmubooleanfalse是否展示弹幕,只在初始化时有效,不能动态变更

测试代码:

<video 
  id="myVideo" src="http://iwenwiki.com/api/livable/livable.mp4"
  duration="100"
  controls
  autoplay
  loop
  muted
  initial-time="10"
  show-mute-btn
  title="制作歌曲"
  danmu-list="{{ danmuList }}"
  danmu-btn
  enable-danmu
></video>
<button type="primary" bindtap="sendDanmuHandle">发送弹幕</button>
Page({
  data: {
    danmuList: [{
      text: '第 1s 出现的弹幕',
      color: '#ff0000',
      time: 11
     }]
   },
  onReady() {
    this.videoContext = wx.createVideoContext('myVideo')
   },
  sendDanmuHandle() {
    this.videoContext.sendDanmu({
      text: "真好看",
      color: "#00ff00"
     })
   }
})

展示效果:

在这里插入图片描述

1.14、camera(拍照)

系统相机。扫码二维码功能

<camera style="width: 100%; height: 300px;"></camera>

属性说明

属性类型默认值必填说明
modestringnormal应用模式,只在初始化时有效,不能动态变更 normal:相机模式 scanCode:扫码模式
device-positionstringback摄像头朝向 front:前置 back:后置
flashstringauto闪光灯,值为 auto , on, off

测试代码:

<camera mode="normal" device-position="back" flash="on" style="width: 100%; height: 300px;"></camera>
<button type="primary" bindtap="takePhotoHandle">拍照</button>
<view>预览</view>
<image mode="widthFix" src="{{src}}"></image>属性说明
Page({
  data:{
    src:""
   },
  takePhotoHandle() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
         })
       }
     })
   }
})

1.15、map(地图)

地图,小程序地图实现功能相对比基础一些,如果要实现完整的地图能力,请参考腾讯地图:https://lbs.qq.com/product/miniapp/home/

<map latitude="23.099994" longitude="113.324520"></map>

属性说明

属性类型默认值必填说明
longitudenumber中心经度
latitudenumber中心纬度
scalenumber16缩放级别,取值范围为3-20
min-scalenumber3最小缩放级别
max-scalenumber20最大缩放级别

测试代码:

<map 
 latitude="{{latitude}}"
 longitude="{{longitude}}"
 scale="12"
 min-scale="10"
 max-scale="18"
></map>
Page({
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
   }
})

展示效果:

在这里插入图片描述

1.16、进度条(progress)

  • 进度条相信大家应该都知道是什么吧,我们日常生活中随地可见,我这里就不说明了

使用方式:20%的进度条

<progress percent="20"/>

属性设置:

属性说明

属性类型默认值必填说明
percentnumber百分比0~100
show-infobooleanfalse在进度条右侧显示百分比
border-radiusnumber/string0圆角大小
font-sizenumber/string16右侧百分比字体大小
stroke-widthnumber/string6进度条线的宽度
activeColorstring#09BB07进度条颜色
backgroundColorstring#EBEBEB未选择的进度条的颜色
activebooleanfalse进度条从左往右的动画
durationnumber30进度增加1%

测试用例:

<progress percent="20"/>
<progress percent="20" show-info/>
<progress percent="20" show-info font-size="30"/>
<progress percent="20" show-info font-size="30" stroke-width="20"/>
<progress percent="20" border-radius="5"/>
<progress percent="20" border-radius="5" activeColor="#f00"/>
<progress percent="20" border-radius="5" activeColor="#f00" backgroundColor="#00f"/>
<progress percent="20" border-radius="5" activeColor="#f00" backgroundColor="#00f" active/>
<progress percent="20" border-radius="5" activeColor="#f00" backgroundColor="#00f" active duration="90"/>

效果为:
在这里插入图片描述

有兴趣可以加知识星球:(每日打卡,每日一题,Java,前端)

知识星球学习,欢迎进来,保证受益无穷

我是zhz小白,一个在互联网行业的小白,立志成为一名架构师
https://blog.csdn.net/zhouhengzhe?t=1

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
微信小程序的 `tabBar` 组件是一个系统级组件,它的样式是由系统控制的,因此无法直接通过设置 `rgba` 背景色来改变其背景色。不过,你可以通过以下几种方法来实现自定义背景色的效果: 1. 使用 `backgroundColor` 属性 在 `app.json` 中设置 `tabBar` 的 `backgroundColor` 属性即可改变其背景色。该属性支持十六进制颜色值、RGB 颜色值和关键词等多种设置方式,例如: ```json { "tabBar": { "backgroundColor": "#ffffff" } } ``` 2. 使用 `background-color` 样式 在 `app.wxss` 中设置 `.custom-tab-bar` 样式,并在 `tabBar` 中添加 `custom-tab-bar` 类名即可改变其背景色。例如: ```css .custom-tab-bar { background-color: rgba(255, 255, 255, 0.5); } ``` ```json { "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/tabbar/home.png", "selectedIconPath": "images/tabbar/home-active.png", "clas": "custom-tab-bar" }, // ... ] } } ``` 3. 使用 `background-image` 样式 在 `app.wxss` 中设置 `.custom-tab-bar` 样式,并使用 `linear-gradient` 函数来创建一个渐变背景。例如: ```css .custom-tab-bar { background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5)); } ``` ```json { "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/tabbar/home.png", "selectedIconPath": "images/tabbar/home-active.png", "clas": "custom-tab-bar" }, // ... ] } } ``` 希望这些方法能够帮助你解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhz小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值