国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序使用component自定義toast彈窗效果

2019-11-19 12:27:49
字體:
來源:轉載
供稿:網友

前言

微信小程序自帶的消息提示框有字數限制,而且圖標僅僅只有"success","loading","none"。當我們在實際開發過程中,面對UI給的設計圖稿和微信小程序默認提供的消息提示框具有很大差別的時候,自然就不能再使用微信小程序的消息提示框,而應當使用component自定義消息提示框組件。

效果圖

Step1:初始化組件

新建一個components文件夾,這個文件夾用來存放我們以后要開發的所有自定義組件。

然后在components文件夾中創建Toast文件夾,在Toast上右擊新建Component 之后就會自動創建相對應的wxml、wxss、js、json文件。

Step2:組件的相關配置

將toast.json 中component 設置為true

toast.json:

{ "component": true,  // 自定義組件聲明 "usingComponents": {}  // 可選項,用于引用別的組件}

然后在toast.wxml文件里寫彈窗組件的模板,在toast.wxss文件里寫組件的樣式

toast.wxml:

<!--components/Toast/toast.wxml--><view class='mask' hidden="{{isShow}}"> <image class="image" src='../../images/{{icon}}.png' mode='aspectFit'></image> <view class='info'>{{information}}</view></view>

toast.wxss:

/* components/Toast/toast.wxss */.mask{ width: 400rpx; height: 300rpx; border-radius:10rpx;  position: fixed; z-index: 1000; top: 300rpx; left: 175rpx; background: rgba(0, 0, 0, 0.6);}.image{ z-index: 1000; width: 120rpx; height: 120rpx; margin-left: 140rpx;}.info{ margin-top:50rpx;  z-index: 1000; text-align: center; color: #ffffff;} width: 400rpx; height: 300rpx; border-radius:10rpx;  position: fixed; z-index: 1000; top: 300rpx; left: 175rpx; background: rgba(0, 0, 0, 0.6);}.image{ z-index: 1000; width: 120rpx; height: 120rpx; margin-left:80rpx;}.info{ margin-top:50rpx;  z-index: 1000; text-align: center; color: #ffffff;}

Step3:定義屬性、數據和事件

可以看到在toast.wxml文件中出現了{{isShow}}、{{icon}}、{{information}} 變量,這是為了組件模板能夠根據傳入的屬性動態變化。

toast.js :

// components/Toast/toast.jsComponent({ /** * 組件的屬性列表 */ properties: {    //定義組件屬性 information:{   //用來顯示提示信息  type: String,   // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)  value: '提示信息'  // 屬性初始值(可選),如果未指定則會根據類型選擇一個 }, icon:{     //圖標類型,我在images文件夾中存放了success和fail的兩個圖標  type:String,  value:'success' }, showTime:{    //彈窗開始顯示的時間單位ms  type: Number,  value:1000 }, hideTime: {    //彈窗開始隱藏的時間單位ms  type: Number,  value: 1000 } }, /** * 組件的初始數據 */ data: { isShow:true }, /** * 組件的方法列表 */ methods:{  showToast:function () {  let that = this;  setTimeout(function () {   that.setData({   isShow: !that.data.isShow  });  }, that.data.showTime); }, hideToast: function (e) {  let that = this;  setTimeout(function(){    that.setData({   isShow: !that.data.isShow  });  },that.data.hideTime); } }})

Step4:使用彈窗/strong>

目前已經完成了toast組件模板,接下來就是在需要顯示這個彈窗的頁面中使用它。

index.json:引入組件

{ "usingComponents": { "toast": "/components/Toast/toast" }}

index.wxml:

<!--page/index/index.wxml--><view class="container"> <toast id='toast'information="提交成功,我們會盡快和您聯系" icon="success" showTime="1000" hideTime='2000'></toast> <button type="primary" bindtap="show"> 顯示彈窗 </button></view>

index.js:

// page/index/index.jsPage({ /** * 頁面的初始數據 */ data: { }, show:function(){ this.toast.showToast(); this.toast.hideToast(); }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { }, /** * 生命周期函數--監聽頁面初次渲染完成 */ onReady: function () { this.toast = this.selectComponent("#toast"); }, /** * 生命周期函數--監聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數--監聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關事件處理函數--監聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數 */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { }})

至此我們就完成了自定義toast組件的步驟。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大连市| 衡东县| 汕尾市| 富蕴县| 钟祥市| 久治县| 淮南市| 玉龙| 平顺县| 临潭县| 宁都县| 安仁县| 北海市| 西安市| 同仁县| 普定县| 双柏县| 体育| 剑川县| 临漳县| 丹巴县| 肥东县| 绥德县| 桦川县| 皮山县| 东乌珠穆沁旗| 原平市| 湘西| 承德市| 临江市| 宁武县| 吉首市| 资溪县| 牡丹江市| 贵德县| 沿河| 介休市| 霞浦县| 渑池县| 英山县| 柘城县|