微信小程序開發(fā)常用功能點與使用方法總結(jié)
在這里,我主要整理了一些小程序開發(fā)過程中常用的功能點,非常實用,下面來一看看看吧
1、獲取高度寬度
var windowHeight = wx.getSystemInfoSync().windowHeight var windowWidth = wx.getSystemInfoSync().windowWidth
2、動態(tài)綁定 style 樣式 和class
class="operBtn {{select==1?'activeClass':''}}"
style="display:{{displayPhoto}}"
3、wx:if
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>
4、點擊事件
<van-tag plain style="margin-left:10px;" bindtap="toggle">全選</van-tag>
//冒泡事件catchtap做點擊事件
<van-button class="shop-but" size="mini" icon="like-o" catchtap="getWish" data-pro_id='{{item.product_id}}'></van-button>
//獲取點擊數(shù)據(jù)
let id = e.currentTarget.dataset.id
5、wx:for
//在組件上使用 wx:for 控制屬性綁定一個數(shù)組,即可使用數(shù)組中各項的數(shù)據(jù)重復(fù)渲染該組件。
//默認(rèn)數(shù)組的當(dāng)前項的下標(biāo)變量名默認(rèn)為 index,數(shù)組當(dāng)前項的變量名默認(rèn)為 item
<view wx:for="{{array}}">
{{index}}: {{item.message}}
</view>
//或者
//使用 wx:for-item 可以指定數(shù)組當(dāng)前元素的變量名,
//使用 wx:for-index 可以指定數(shù)組當(dāng)前下標(biāo)的變量名:
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
{{idx}}: {{itemName.message}}
</view>
6、data里面的數(shù)據(jù)
//存儲
this.setData({
result: event.detail
});
//使用
this.data.result
7、修改頁面配置??梢韵吕⑿?;修改頁面標(biāo)題
"enablePullDownRefresh": true, "navigationBarBackgroundColor": "#FFA500", "navigationBarTitleText": "我的發(fā)票", "navigationBarTextStyle": "white"
8、動態(tài)修改頁面標(biāo)題
wx.setNavigationBarTitle({
title: this.data.info.name
})
9.橫線
<van-divider custom-style="margin:10px 0;" />
10、WXS(WeiXin Script)是小程序的一套腳本語言,結(jié)合 WXML,可以構(gòu)建出頁面的結(jié)構(gòu)。
//時間格式化
<wxs module="format">
var format = function(date) {
var date = getDate(date)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var weekDay = date.getDay()
var week = ''
switch(weekDay){
case 0:
week = '周日'
break;
case 1:
week = '周一'
break;
case 2:
week = '周二'
break;
case 3:
week = '周三'
break;
case 4:
week = '周四'
break;
case 5:
week = '周五'
break;
case 6:
week = '周六'
break;
}
return month + '月' + day + '日' +' ' + week;
}
module.exports.format = format;
</wxs>
//手機郵箱打星號
<wxs module="phone">
var toHide = function(array) {
var mphone = array.substring(0, 3) + '****' + array.substring(7);
return mphone;
}
module.exports.toHide = toHide;
</wxs>
11、video,屬性用法
<video
id="myVideo"
src="{{url}}"
binderror="videoErrorCallback"
show-center-play-btn='{{false}}'
show-play-btn="{{true}}"
controls
title="課程"
object-fit="fill"
enable-auto-rotation="true"
bindtimeupdate="bindtimeupdate"
></video>
//
let videoCtx = wx.createVideoContext('myVideo', this)
videoCtx.pause()
//bindtimeupdate 獲取進度時間,根據(jù)時間來進行限制播放操作
bindtimeupdate:function(res){//播放中函數(shù),查看當(dāng)前播放時間等
let video_status = this.data.video_status
let that = this
if (res.detail.currentTime > 10) {
if (video_status === '0') {
wx.showModal({
title: '登錄',
content: '試聽課程結(jié)束,如需繼續(xù)查看,請先登錄',confirmText:'確定',
success (res) {
if (res.confirm) {
wx.switchTab({
url: '/pages/user/user'
})
} else if (res.cancel) {
wx.navigateBack({
delta: 1,
})
}
}
})
} else if (video_status === '2'){
let videoCtx = wx.createVideoContext('myVideo', this)
videoCtx.pause()
wx.showModal({
title: '購買課程',
content: '試聽課程結(jié)束,如需繼續(xù)查看,請先購買',confirmText:'立即支付',
success (res) {
if (res.confirm) {
that.onClickButton()
} else if (res.cancel) {
wx.navigateBack({
delta: 1,
})
}
}
})
}
} else {
}
},
12、數(shù)據(jù)的存儲
//存
try {
wx.setStorageSync('car', info)
} catch (e) { }
//獲取
try {
var value = wx.getStorageSync('car')
if (value) {
this.setData({
ApplyContent:value
})
}
} catch (e) {
}
13、地圖使用
//官網(wǎng)
const QQMapWX = require('../../assets/js/qqmap-wx-jssdk')
const qqmapsdk = new QQMapWX({
key: 'XSTBZ-G74CJ-7BVFQ-KC2JD-ONRK5-ZABGU'
})
getAddressLocation(){
let that = this
wx.getLocation({
type: 'gcj02',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
that.getAddress(latitude, longitude)
}
})
},
getAddress(latitude, longitude) {
let that = this
qqmapsdk.reverseGeocoder({
location: {latitude,longitude},
success(res) {
console.log(res);
var mks = []
mks.push({ // 獲取返回結(jié)果,放到mks數(shù)組中
title: res.result.address,
id: 1,
latitude: latitude,
longitude: longitude,
iconPath: "../../img/mk.png", //圖標(biāo)路徑
width: 20,
height: 20
})
that.setData({
address:res.result.address,
markers: mks,
latitude: latitude,
longitude: longitude,
})
}
})
},
14、點擊復(fù)制
copyBtn: function (e) {
var currentidx = e.currentTarget.dataset.num;
console.log(currentidx);
wx.setClipboardData({
data: currentidx,
success: function (res) {
wx.showToast({
title: '復(fù)制成功',
});
}
});
},
總結(jié)
到此這篇關(guān)于微信小程序開發(fā)常用功能點與使用方法的文章就介紹到這了,更多相關(guān)微信小程序開發(fā)常用功能點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過循環(huán)優(yōu)化 JavaScript 程序
這篇文章主要介紹了通過循環(huán)優(yōu)化 JavaScript 程序,對于提高 JavaScript 程序的性能這個問題,最簡單同時也是很容易被忽視的方法就是學(xué)習(xí)如何正確編寫高性能循環(huán)語句。下面我們來學(xué)習(xí)一下吧2019-06-06
bootstrap tooltips在 angularJS中的使用方法
這篇文章主要介紹了bootstrap tooltips在 angularJS中的使用 ,需要的朋友可以參考下2019-04-04
javascript實現(xiàn)客戶端兼容各瀏覽器創(chuàng)建csv并下載的方法
這篇文章主要介紹了javascript實現(xiàn)客戶端兼容各瀏覽器創(chuàng)建csv并下載的方法,實例分析了javascript操作csv文件的技巧,需要的朋友可以參考下2015-03-03
Javascript 判斷兩個IP是否在同一網(wǎng)段實例代碼
javascript功能的強大之處是我們大家有目共睹的,幾天小編向大家介紹Javascript 判斷兩個IP是否在同一網(wǎng)段,感興趣的朋友可以參考一下2016-11-11
JS異步編程之generator與async/await語法糖詳解
這篇文章主要為大家詳細(xì)介紹了JS異步編程中g(shù)enerator與async/await語法糖的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2022-11-11
javascript實現(xiàn)的淘寶旅行通用日歷組件用法實例
這篇文章主要介紹了javascript實現(xiàn)的淘寶旅行通用日歷組件,以實例形式分析了該日歷組件的相關(guān)設(shè)置及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

