微信小程式-UI控制元件的使用(1)
阿新 • • 發佈:2019-01-02
button的初體驗:
初始化一個專案,新建一個目錄firstPage,結構如下:
在firtst.wxml中寫UI
<view class="content"><text class="title">button 示例</text></view>
<button type="default" size="{{defaultSize}}" loading="{{loading}}" plain="{{plain}}"
disabled="{{disabled}}" bindtap="default" hover-class ="other-button-hover"> default </button>
<button type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}"
disabled="{{disabled}}" bindtap="primary"> primary </button>
<button type="warn" size="{{warnSize}}" loading="{{loading}}" plain="{{plain}}"
disabled ="{{disabled}}" bindtap="warn"> warn </button>
<button bindtap="setDisabled">點選設定以上按鈕disabled屬性</button>
<button bindtap="setPlain">點選設定以上按鈕plain屬性</button>
<button bindtap="setLoading">點選設定以上按鈕loading屬性</button>
在first.wxss中寫對應的佈局:
/* pages/firstPage/first.wxss */
/** 修改button預設的點選態樣式類**/
.button-hover {
background-color: red;
}
/** 新增自定義button點選態樣式類**/
.other-button-hover {
background-color: blue;
}
.content{
margin-top: 10px;
margin-bottom: 60px;
text-align: center;
}
.title{
padding-bottom: 10px;
在first.js中進行邏輯和資料的設定:
var pageConfig = {
data:{
defaultSize: 'default',
primarySize: 'default',
warnSize: 'default',
disabled: false,
plain: false,
loading: false
},
setDisabled: function(e) {
this.setData({
disabled: !this.data.disabled
})
},
setPlain: function(e) {
this.setData({
plain: !this.data.plain
})
},
setLoading: function(e) {
this.setData({
loading: !this.data.loading
})
}
}
Page(pageConfig)
最後在app.json中配置路徑,顯示如下:
label的初體驗:
label.wxml
<view class="content">
<text class="title">label 示例</text>
</view>
<label>這是個label</label>
<view class="section section_gap">
<view class="section__title">表單元件在label內</view>
<checkbox-group class="group" bindchange="checkboxChange">
<view class="label-1" wx:for="{{checkboxItems}}">
<label>
<checkbox hidden value="{{item.name}}" checked="{{item.checked}}"></checkbox>
<view class="label-1__icon">
<view class="label-1__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
</view>
<text class="label-1__text">{{item.value}}</text>
</label>
</view>
</checkbox-group>
</view>
<view class="section section_gap">
<view class="section__title">label用for標識表單元件</view>
<radio-group class="group" bindchange="radioChange">
<view class="label-2" wx:for="{{radioItems}}">
<radio id="{{item.name}}" hidden value="{{item.name}}" checked="{{item.checked}}"></radio>
<view class="label-2__icon">
<view class="label-2__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
</view>
<label class="label-2__text" for="{{item.name}}"><text>{{item.name}}</text></label>
</view>
</radio-group>
</view>
label.wxss
/* pages/label/label.wxss */
.content{
margin-top: 10px;
margin-bottom: 60px;
text-align: center;
}
.title{
padding-bottom: 10px;
border-bottom: 1px solid lightgray;
}
.label-1, .label-2{
margin-bottom: 15px;
}
.label-1__text, .label-2__text {
display: inline-block;
vertical-align: middle;
}
.label-1__icon {
position: relative;
margin-right: 10px;
display: inline-block;
vertical-align: middle;
width: 18px;
height: 18px;
background: #fcfff4;
}
.label-1__icon-checked {
position: absolute;
top: 3px;
left: 3px;
width: 12px;
height: 12px;
background: #1aad19;
}
.label-2__icon {
position: relative;
display: inline-block;
vertical-align: middle;
margin-right: 10px;
width: 18px;
height: 18px;
background: #fcfff4;
border-radius: 50px;
}
.label-2__icon-checked {
position: absolute;
left: 3px;
top: 3px;
width: 12px;
height: 12px;
background: #1aad19;
border-radius: 50%;
}
.label-4_text{
text-align: center;
margin-top: 15px;
}
.group{
background-color: red;
}
label.js
Page({
data: {
checkboxItems: [
{name: 'USA', value: '美國'},
{name: 'CHN', value: '中國', checked: 'true'},
{name: 'BRA', value: '巴西'},
{name: 'JPN', value: '日本', checked: 'true'},
{name: 'ENG', value: '英國'},
{name: 'TUR', value: '法國'},
],
radioItems: [
{name: 'USA', value: '美國'},
{name: 'CHN', value: '中國', checked: 'true'},
{name: 'BRA', value: '巴西'},
{name: 'JPN', value: '日本'},
{name: 'ENG', value: '英國'},
{name: 'TUR', value: '法國'},
],
hidden: false
},
checkboxChange: function(e) {
var checked = e.detail.value
var changed = {}
for (var i = 0; i < this.data.checkboxItems.length; i ++) {
if (checked.indexOf(this.data.checkboxItems[i].name) !== -1) {
changed['checkboxItems['+i+'].checked'] = true
} else {
changed['checkboxItems['+i+'].checked'] = false
}
}
this.setData(changed)
},
radioChange: function(e) {
var checked = e.detail.value
var changed = {}
for (var i = 0; i < this.data.radioItems.length; i ++) {
if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
changed['radioItems['+i+'].checked'] = true
} else {
changed['radioItems['+i+'].checked'] = false
}
}
this.setData(changed)
}
})
input初體驗:
input.wxml
<view class="content">
<text class="title">input 示例</text>
</view>
<view>
<input placeholder="這是一個可以自動聚焦的input" auto-focus/>
</view>
<view class="section">
<input placeholder="這個只有在按鈕點選的時候才聚焦" focus="{{focus}}" />
<view class="btn-area">
<button bindtap="bindButtonTap">使得輸入框獲取焦點</button>
</view>
</view>
<input maxlength="10" placeholder="最大輸入長度10" />
<view class="section">
<view class="section__title">你輸入的是:{{inputValue}}</view>
<input bindinput="bindKeyInput" placeholder="輸入同步到view中" />
</view>
<input bindinput="bindReplaceInput" placeholder="連續的兩個1會變成2" />
<view class="section">
<input bindinput="bindHideKeyboard" placeholder="輸入123自動收起鍵盤" />
</view>
<view class="section">
<input password type="number" />
</view>
<view class="section">
<input password type="text" />
</view>
<view class="section">
<input type="digit" placeholder="帶小數點的數字鍵盤"/>
</view>
<view class="section">
<input type="idcard" placeholder="身份證輸入鍵盤" />
</view>
<view class="section">
<input placeholder-style="color:red" placeholder="佔位符字型是紅色的" />
</view>
input.js
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
}
},
bindHideKeyboard:function(e){
if (e.detail.value === '123') {
//收起鍵盤
wx.hideKeyboard()
}
}
})