一:组件的定义及其属性:
组件是页面视图的基本组成的单元,其格式如下:
<标签名 属性名=“属性值”>内容</标签名>
组件的属性有:
二:容器视图组件解析:
容器视图组件总共有三个组件,分别为view scroll-view和swiper三个组件
view组件:
view组件是块级组件,没有特殊功能,主要用于布局展示,相当于HTML中的div,是布局中最基本的用户界面(User Interace,UI)组件,通过设置view的CSS属性可以实现各种复杂的布局。view组件的特有属性
scroll-view组件
通过设置该组件可以实现滚动视图的功能,其属性有
swiper组件:
滑动页面等效果。一个完整的swiper组件由<swipev>和<siper-iem/>两个标签组成,它们不能单独使用。<swiper/>中只能放置一个或多个<swiper-iem/>,若放置其他组件则会被删除;<swiper -item/>内部可以放置任何组件,默认宽高自动设置为100% 其属性值如图所示:
二:基础内容组件
基础内容组件包括icon,text和progress组件
1:icon组件为图标组件,通常用于表示一种状态。其属性有,success info warn waiting cancel等
2,text组件:该组件用于展示内容,类似于html中的<spana.>,text组件中的的内容支持长选,属于行内元素,其属性如图所示
3:progress组件:
代码如下
<view>显示百分比</view>
<progress percent='80' show-info='80'></progress>
<view>改变宽度</view>
<progress percent='80' show-width='2'></progress>
<view>自动显示进度条</view>
<progress percent='80' active></progress>
三:表单组件:
buttonbutton组件用来实现用户和应用之间的交互,同时按钮的颜色起引导作用。一般来说在一个程序中一个按钮至少有3种状态:默认点击(default)、建议点击(primary)、谨慎点击(war)。在构建项目时,应在合适的场景使用合适的按钮,当<button>被<formV>包裹时,可以通过设置form-type属性来触发表单对应的事件。button组件的属性如表4-1所示
代码:
<view>
icon类型:
<black wx:for="{{iconType}}">
<icon type="{{item}}"/>{{item}}
</black>
</view>
<view>icon大小
<black wx:for="{{iconSize}}">
<icon type="success"size="30"color="{{item}}"/>{{item}}</black></view>
<button type="default">type:default</button>
<button type="primary">type:primary</button>
<button type="warn">type:warn</button>
<button type="default" bindtap='buttonsize' size="{{size}}">改变size</button>
<button type="default" bindtap='buttonPlain' plain="{{plain}}">改变plain</button>
<button type="default" bindtap='buttonloading' loading="{{loading}}">改变loading显示</button>
效果如图:
2:radio组件:
单选框用来从一组选项中选取一个选项在小程序中,单选框由<radio-group/>(单项选择器)和<radio/>(单选项目)两个组件组合而成,一个包含多个<radio/>的<radio-group/>表示一组单选项,在同一组单选项中<radio/>是互斥的,当一个按钮被选中后,之前选中的按钮就变为非选。它们的属性如表4-8所示
训练代码如图:
<!-- <view>
icon类型:
<black wx:for="{{iconType}}">
<icon type="{{item}}"/>{{item}}
</black>
</view>
<view>icon大小
<black wx:for="{{iconSize}}">
<icon type="success"size="30"color="{{item}}"/>{{item}}</black></view>
<button type="default">type:default</button>
<button type="primary">type:primary</button>
<button type="warn">type:warn</button>
<button type="default" bindtap='buttonsize' size="{{size}}">改变size</button>
<button type="default" bindtap='buttonPlain' plain="{{plain}}">改变plain</button>
<button type="default" bindtap='buttonloading' loading="{{loading}}">改变loading显示</button> -->
<view>选择你喜欢的城市:</view>
<radio-group bindchange="citychange">
<radio value="西安">西安</radio>
<radio value="北京">北京</radio>
<radio value="上海">上海</radio>
<radio value="广州">广州</radio>
<radio value="深圳">深圳</radio>
</radio-group>
<view>你的选择:{{city}}</view>
<view>选择你喜爱的计算机语言</view>
<radio-group class="radio-group" bindchange="radiochange">
<lable class = "radio" wx:for = "{{radios}}">
<radio value = "{{item.value}}"checked = "{{item.chenked}}"/>{{item.name}}
</lable>
</radio-group>
结果如图所示: 3:checkbox组件:复选框用于从一组选项中选取多个选项,小javapython phpsnt程序中复选框由<checkbox-group/>(多项选择器)和<checkbox/>(多选项目)两个组件组合而成。一个<checkbox-group/>表示一组选项,可以在一组选项中选中多们的属性如表
代码如图:
<view>选择您要去的城市:</view>
<checkbox-group bindchange= "citychange">
<label wx:for = "{{ citys }}">
<checkbox value = "{{item.value}}" checked = '{{item.checked}}'>{{item.value}}
</checkbox>
</label>
</checkbox-group>
<view>您的选择是:{{ city }}</view>
效果如图4:switch组件:
switch组件的作用类似开关选择器的作用,其属性如图所示
练习代码如图:
5:slider组件:
该组件可以通过滑动来设置相应的值,其属性如图所示:
训练代码如图所示:
Page({
data:{
size:'20'
},
sliderchange:function(e){
this.setData({ size:e.detail.value})
}
})
<view>默认 min=0 max=100 step=1</view>
<slider></slider>
<view>显示当前值</view>
<slider show-value></slider>
<view>设置min=20 max=200 step=10</view>
<slider min='0' max='200' step="10" show-value></slider>
<view>背景条红色,已选定颜色绿色</view>
<slider color="#f00" selected-color="#0f0"></slider>
<view>滑动改变icon的大小</view>
<slider show-value bindchange ='sliderchange'></slider>
<icon type="success" size="{{size}}"></icon>
运行结果如图:7:piker组件:
picker-view组件为嵌人页面的滚动选择器。相对于picker组件,picker-view组件的列的个数和列的内容由用户通过<picker-viewcolumn/>自定义设置。picker-view组件的属性如表训练代码如图所示:
Page({
data:{
years:years,
months:months,
days:days,
year:date.getFullYear(),
month:date.getMonth() +1,
day:date.getDate(),
value:[118,0,0],//定位到2018年1月1日
},
bindChange:function(e){
const val = e.detail.value
console.log(val);
this.setData({
year:this.data.years[val[0] ],
month:this.data.months[val[1] ],
day:this.data.days[val[2] ]
})
}
})
<view>当年日期:{{year}}年 {{month}}月 {{day}}天</view>
<picker-view indicator-style ="height: 50px:" style="width: 100%;height: 300px;"value="{{value}}" bindcahnge="bindChange">
<picker-view-column>
<view wx:for="{{years}}" style ="line-height: 50px">{{item}}年</view>
</picker-view-column>
<picker-view-column>
<view wx:for="{{months}}" style ="line-height: 50px">{{item}}月</view>
</picker-view-column>
<picker-view-column>
<view wx:for="{{days}}" style ="line-height: 50px">{{item}}日</view>
</picker-view-column>
</picker-view>
代码运行结果如图:8:input组件:
该组件作为输入框,用户可以输入相应的信息,其属性如图所示:
联系代码如图
Page({
data:{
focus:false,
inputValue:""
},
bindButtonTap:function(){
this.setData({
focus:true
})
},
bindKeyInput:function(e){
this.setData({
inputValue:e.detail.value
})
},
bindReplaceInput:function(e){
var value =e.detail.value
var pos =e.detail.cursor
if(pos!=-1){
var left =e.detail.value.slice(0,pos)
pos =left.replace(/11/g,'2').length
}
return{
value:value.replace(/11/g,'2'),
Cursor:pos
}
}
})
<input placeholder="这是一个可以自动聚焦的 input" auto-focus/>
<input placeholder="这个只有在按钮点击的时候才聚焦" focus ="{{focus}}"/>
<button bindtap="bindButtonTap">使得输入框获取焦点</button>
<input maxlength="10" placeholder="最大输入长度为10"/>
<view class="section_title">你输入的是:{{inputValue}}</view>
<input bindinput="bindKeyInput" placeholder="输入同步到view中"/>
<input bindinput="bindReplaceInput" placeholder="连续的两个1会变成2"/>
<input password type="number"/>
<input password type="text"/>
<input type="digit" placeholder="带小数点的数字键盘"/>
<input type="idcard" placeholder="身份证输入键盘"/>
<input placeholder-style="color:red" placeholder="占位符字体是红色的"/>
运行结果如图所示9:textarea组件:
该组件为多行输入组件,可以实现多行内容的输入,该属性如图所示
训练代码如图所示
<textarea bindblur="bindTextAreaBlur"auto-height placeholder="自动变高"></textarea>
<textarea placeholder="placeholder 颜色是红色的" placeholder-style="color:red;"></textarea>
<input placeholder="这是一个可以自动聚焦的 input" auto-focus/>
<input placeholder="这个只有在按钮点击的时候才聚焦" focus ="{{focus}}"/>
<button bindtap="bindButtonTap">使得输入框获取焦点</button>
<textarea placeholder="form中的textarea" name = "textarea"/>
<button form-type="submit">提交</button>
Page({
data:{
height:10,
focus:false
},
bindButtonTap:function(){
this.setData({
focus:true
})
},
bindTextAreaBlur:function(e){
console.log(e.detail.value)
},
bindFormSubmit:function(e){
console.log(e.detail.value.textarea)
}
})
运行结果如图所示:10:lable组件:
label组件为标签组件,用于提升表单组件的可用性。label组件支持使用for属性找到对应的id,或者将控件放在该标签下,当点击label组件时,就会触发对应的控件。for属性的优先级高于内部控件,内部有多个控件的时候默认触发第一个控件。
目前,label组件可以绑定的控件有<button/ >、<checkbox/ >、<radio/>、<switch/ >
训练代码如图所示:
<view><checkbox></checkbox>中国</view>
<view><label><checkbox> </checkbox >中国</label></view>
<checkbox-group bindchange="cityChange">
<label wx:for="{{citys}}">
<checkbox value ="{{item.value}}" checked = '{{item.checked}}'>
{{item.value}}</checkbox>
</label>
</checkbox-group>
<view>您的选择是:{{city}}</view>、
Page({
city:"",
data:{
citys:[
{name:'km',value:'昆明'},
{name:'sy',value:'三亚'},
{name:'zh',value:'珠海',checked:'true'},
{name:'dl',value:'大连'}]
},
cityChange:function(e){
console.log(e.detail.value);
var city =e.detail.value;
this.setData({city: city})
}
})
训练结果如图所示:11:form组件:
该组件为表单组件,用于将组件内的用户信息进行提交,当</ form>表单中的formType为submit的<button />组件时 会将表单中的value值进行提交,其属性如图所示训练代码如图所示:
<form bindsubmit ="formSubmit " bindreset ="formReset ">
<view>姓名:
<input type="text"name="xm"/>
</view>
<view>性别:
<radio-group name="xb">
<label><radio value="男" checked/>男</label>
<label><radio value="女"/>女</label>
</radio-group>
</view>
<view>爱好:
<checkbox-group name="hobby">
<label wx:for="{{hobbies}}">
<checkbox value="{{item.value}}" checked='{{item.checked}}'>{{item.value}}</checkbox>
</label>
</checkbox-group>
</view>
<button formType='submit'>提交</button>
<button formrype='reset'>重置</button>
</form>
Page({
hobby :"",
data:{
hobbies :[
{name:'jsj',value:'计算机',checked:'true'},
{name:'music',value:'听音乐'},
{name:'game',value:'玩电竟'},
{name:'swim',value:'游泳',checked:'true'}]
},
formSubmit:function(e){
console.log('form发生了submit事件,携带数据为:'.e.detail.value)
},
formReset:function(){
console.log('form发生了reset事件')
}
})
四:多媒体组件:
多媒体组件包括image (图像)、audio video (视频)、camera (相机) 组件,使用这些组件, 可以让页面更具有吸引力
1:image组件:、imagae组件为图像组件, 与html 中的< img/ > 类似, 系统默认image 组件的宽度为300px、高度为2250px,image 组件的属性如表所示训练代码如图所示
<block wx:for ="{{modes}}">
<view>当前图片的模式是:{{item}}</view>
<image mode ="{{item}}" src ="/pages/index/image/ahh.jpg" style ="width:100%,height:100%"/>
</block>
Page({
data:{
modes:['scaleToFill','aspectFit','aspectFill','widthFrix']
}
})
结果如图:
2:裁剪模式:
top不缩放图片,只显示图片的顶部区域
bottom不缩放图片,只显示图片的底部区域
center不缩放图片,只显示图片的中间区域
left不缩放图片,只显示图片的左边区域
night 不缩放图片,只显示图片的右边区域
top_left 不缩放图片,只显示图片的左上边区域
:top_right 不缩放图片,只显示图片的右上边区域
bottom_left 不缩放图片,只显示图片的左下边区域
bottom _right不缩放图片,只显示图片的右下边区域t示例代码如下
<black wx:for="{{m odes}}">
<view>当前图片的模式是:“{{item}}"</view>
<image mode="{{item}}" src="/images/ooo.jpg"></image>
</black>
Page({
data:{
modes:['top','center','left','right','top_left','top_right','bottom_left','bottom_right']
}
})
结果如图所示
5:audio组件:
该组件用于音频设置:属性如图所示4
代码如图所及:
<audio src ="{{src}}" action="{{action}}" poster = "{{poster}}" name = "{{name}}" author ="{{author}} " loop controls></audio >
<button type="primary" bindtap='play'>播放</button >
<button type="primary" bindtap="pause">暂停</button >
<button type ="primary" bindtap="playRate">设置速率</button>
<button type="primary" bindtap="currentTime">设置当前时间(秒)
</button>
Page( {
data:{
poster:'http://.gtimg.cn/usic/hoto new/002R300x300M000003rsKF44Gyask.jpg?max age=2592000',
name:'此时此刻',
author:'许巍',
src:'http://ws.stream.qqmusic.gg.comM500001VfvsJ21xFab.mp3? guidffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
},
play:function(){
this.setData({
action:{
method: 'play'
}
})
},
pause:function(){
this.setData({
action:{
method: 'pause'
}
})
},
playRate:function(){
this.setData({
action:{
method:'setPlaybackRate',
data:10 //速率
}
})
console.log('当前速率:'+this.data.action.data)
},
currentTime:function(e){
this.setData({
action:{
method:'setCurrentTime',
data:120
}
})
}
})
结果如图所示:6:video
用于实现视频的播放与暂停等,视频默认宽度为300px,高为225px。属性如下
训练代码如图所示:
<video src ="{{src}}" controls ></video >
<view class="btn-area">
<button bindtap="bindButtonTap">获取视频</button>
</view>
Page({
data:{
srC:"",
},
bindButtonTap:function(){
var that =this
wx.chooseVideo()({
sourceType: [ 'album' ,'camera'],
maxDuration:60,
camera:['front','back'],
success:function(res){
that.setData({
src:res.tempFilePath
})
}
})
}
})
结果如图所示
camera组件
camera 组件为系统相机组件, 可以实现拍照或录像功能。 在一个页面中, 只能有一个camera 组件。 在开发工具中运行时, 使用电脑摄像头实现拍照或录像; 在手机中运行时, 使用手机前后摄像头实现拍照或录像。属性如图所示
训练代码如图所示:
<camera device-position ="back" flash ="off" binderror ="error"
style="width:100%;height:350px;"></camera>
<button type ="primary" bindtap ="takePhoto" >拍照</button ><view>预览</view >
<image mode ="widthFix" src ="{{src}}"></image>、
Page({
takePhoto(){
const ctx=wx.createCameraContext()//创建并返回 camera上下文对象/拍照,成功则返回图片
ctx.takePhoto({
quality:' high',
success:(res)=>{
this.setData({
src:res.tempImagePath
})
}
})
},
error(e){
console.log(e.detail)
}
})
结果如图: