小程序自定義彈出框效果
本文實(shí)例為大家分享了小程序自定義彈出框效果的具體代碼,供大家參考,具體內(nèi)容如下


my-pop----api:
title ------字符串---------自定義彈窗標(biāo)題
context ------字符串---------自定義彈窗內(nèi)容
cancelTxt ------字符串---------取消按鈕文本
cancelCor ------字符串---------取消按鈕顏色
inp ------布爾值---------true文本彈出框---------fasle默認(rèn)彈出框
okTxt ------字符串---------確定按鈕文字
okCor ------字符串---------確定按鈕顏色
@cancel ------事件---------點(diǎn)擊取消按鈕事件可攜帶value參數(shù)
@ok ------事件---------點(diǎn)擊確定按鈕事件可攜帶value參數(shù)
//使用方法
//在目標(biāo)文件json文件里引入該組件
"usingComponents": {
? ? "mypop":"/components/my-pop"
?}
<mypop id="mypop" context="測(cè)試一下主體" inp="{{true}}" bindok="ok"/>
//給組件一個(gè)id?
this.selectComponent("#mypop").openPop(); ? //打開彈窗
ok(e) ?//點(diǎn)擊確定觸發(fā) 如果是inp類型彈窗 e 就是input的value , 反之e為空串
cancel(e) //點(diǎn)擊取消觸發(fā) ?如果是inp類型彈窗 e 就是input的value ?, 反之e為空串彈窗js文件:
Component({
? /**
? ?* 組件的屬性列表
? ?*/
? properties: {
? ? title: {
? ? ? type: String,
? ? ? value: '默認(rèn)標(biāo)題',?
? ? },
? ? context: {
? ? ? type: String,
? ? ? value: '默認(rèn)內(nèi)容',?
? ? },
? ? inp:{
? ? ? type: Boolean,
? ? ? value: true
? ? },
? ? cancelTxt: {
? ? ? type: String,
? ? ? value: '取消',?
? ? },
? ? cancelCor:{
? ? ? type:String,
? ? ? value:'black'
? ? },
? ? okTxt: {
? ? ? type: String,
? ? ? value: '確認(rèn)',?
? ? },
? ? okCor:{
? ? ? type:String,
? ? ? value:'red'
? ? },
? },
? /**
? ?* 組件的初始數(shù)據(jù)
? ?*/
? data: {
? ? show:false,
? ? animation:'',
? ? animationPop:'',
? ? inpVal:''
? },
? methods: {
? ? bindinput(e){
? ? ? let {value} = e.detail
? ? ? this.setData({inpVal:value})
? ? },
? ? ok(){
? ? ? this.triggerEvent("ok",this.data.inpVal);
? ? ? this.hidePop()
? ? ? this.setData({inpVal:''})
? ? },
? ? cancel(){
? ? ? this.triggerEvent("cancel",this.data.inpVal);
? ? ? this.hidePop()
? ? ? this.setData({inpVal:''})
? ? },
? ? openPop(){
? ? ? var animation = wx.createAnimation({
? ? ? ? ? duration: 200,
? ? ? ? ? timingFunction: "linear",
? ? ? ? ? delay: 0
? ? ? })
? ? ? this.animation = animation
? ? ? animation.opacity(0).step()
? ? ? this.setData({
? ? ? ? animationData: animation.export(),
? ? ? ? show:true
? ? ? })
? ? ? setTimeout(function () {
? ? ? ? ? animation.opacity(1).step()
? ? ? ? ? this.setData({
? ? ? ? ? ? ? animationData: animation.export()
? ? ? ? ? })
? ? ? }.bind(this), 200)
? ? },
? ? hidePop(){
? ? ? var animation = wx.createAnimation({
? ? ? ? ? duration: 200,
? ? ? ? ? timingFunction: "linear",
? ? ? ? ? delay: 0
? ? ? })
? ? ? this.animation = animation
? ? ? animation.opacity(0).step()
? ? ? this.setData({
? ? ? ? animationData: animation.export()
? ? ? })
? ? ? setTimeout(function () {
? ? ? ? ? this.setData({
? ? ? ? ? ? ? show:false
? ? ? ? ? })
? ? ? }.bind(this), 200)
? ? }
? }
})彈窗wxml文件:
<!--components/my-pop.wxml-->
<view>
? <view class="mask" animation="{{animationData}}" bindtap="hidePop" wx:if="{{show}}">
? ? <view class="content" animation="{{animationPop}}" catchtap >
? ? ? <view class="title">{{title}}</view>
? ? ? <view class="txt" wx:if="{{!inp}}">{{context}}</view>
? ? ? <view class="inp" wx:if="{{inp}}">
? ? ? ? <input type="text" value='{{inpVal}}' bindinput="bindinput" name="" id="" />
? ? ? </view>
? ? ? <view class="btnView">
? ? ? ? <view class="cancel" hover-class="op5" bindtap="cancel" style="color:{{cancelCor}}">{{cancelTxt}}</view>
? ? ? ? <view class="partition"></view>
? ? ? ? <view class="ok" hover-class="op5" bindtap="ok" style="color:{{okCor}}">{{okTxt}}</view>
? ? ? </view>
? ? </view>
? </view>
</view>彈窗wxss文件:
.mask{
? width: 100%;
? height: 100vh;
? position: fixed;
? top: 0; left: 0;
? z-index: 100;
? background: rgba(0, 0, 0, 0.4);
? display: flex;
? align-items: center;
? justify-content: center;
}
.content{
? background: #FFFFFF;
? border-radius: 16rpx;
? width: 70%;?
}
.title{
? padding: 32rpx 0rpx;
? text-align: center;
? font-weight: 500;
?? ?font-size: 32rpx;
?? ?color: black;
}
.txt{
? color: #000000;
? font-size: 24rpx;
? text-align: center;
? margin-bottom: 32rpx;
}
.btnView{
? border-top: 1rpx solid #D4D6D8;
? display: flex;
? align-items: center;
? justify-content: space-around;
}
.cancel{
? width: 49%;
? display: flex;
? align-items: center;
? justify-content: center;
? height: 80rpx; line-height: 80rpx;
}
.ok{
? width: 49%;
? display: flex;
? align-items: center;
? justify-content: center;
? height: 80rpx; line-height: 80rpx;
}
.inp{
? ? text-align: center;
? ? padding: 5px 0px;
?? ??? ?font-size: 24rpx;
?? ??? ?margin: auto;
?? ??? ?color: #868686;
?? ??? ?width: 90%;
?? ??? ?border: 1.2px solid #DEDEDE;
? ? border-radius: 5px;
? ? margin-bottom: 32rpx;
}
.op5{
? opacity: .5;
? background: rgba(0,0,0,0.05);
}
.partition{
? width: 2rpx;
? height: 80rpx;
? background: #D4D6D8;
}彈窗json文件:
{
? "component": true,
? "usingComponents": {}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript DSL 流暢接口(使用鏈?zhǔn)秸{(diào)用)實(shí)例
這篇文章主要介紹了JavaScript DSL 流暢接口(使用鏈?zhǔn)秸{(diào)用)實(shí)例,本文講解了DSL 流暢接口、DSL 表達(dá)式生成器等內(nèi)容,需要的朋友可以參考下2015-03-03
線路分流自動(dòng)智能跳轉(zhuǎn)代碼,自動(dòng)選擇最快鏡像網(wǎng)站(js)
線路分流自動(dòng)智能跳轉(zhuǎn)代碼,自動(dòng)選擇最快鏡像網(wǎng)站,自動(dòng)選擇電信或網(wǎng)通線路跳轉(zhuǎn)代碼2011-10-10
JavaScript中數(shù)據(jù)結(jié)構(gòu)與算法(四):串(BF)
這篇文章主要介紹了JavaScript中數(shù)據(jù)結(jié)構(gòu)與算法(四):串(BF),串是由零個(gè)或多個(gè)字符組成的有限序列,又叫做字符串,本文著重講解了BF(Brute Force)算法,需要的朋友可以參考下2015-06-06
JS實(shí)現(xiàn)根據(jù)用戶輸入分鐘進(jìn)行倒計(jì)時(shí)功能
倒計(jì)時(shí)功能大家無(wú)論在各大網(wǎng)站都可以看到,今天小編給大家分享一段基于js實(shí)現(xiàn)的根據(jù)用戶輸入分鐘進(jìn)行倒計(jì)時(shí)功能,非常不錯(cuò),需要的朋友參考下吧2016-11-11
innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個(gè)特例情況。
innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個(gè)特例情況。...2007-01-01
js獲取TreeView控件選中節(jié)點(diǎn)的Text和Value值的方法
在實(shí)際項(xiàng)目中,遇到一個(gè)問題,首先彈出一個(gè)新窗口,新窗口中放了一個(gè)TreeView控件,現(xiàn)在要解決的是,如何單擊TreeView中一個(gè)節(jié)點(diǎn),返回Text和Value到父頁(yè)面并關(guān)閉該新窗口,本文將詳細(xì)介紹此方法的實(shí)現(xiàn)2012-11-11
微信小程序使用map組件實(shí)現(xiàn)檢索(定位位置)周邊的POI功能示例
這篇文章主要介紹了微信小程序使用map組件實(shí)現(xiàn)檢索(定位位置)周邊的POI功能,涉及微信小程序基于map組件與高德地圖PAI接口的定位操作相關(guān)使用技巧,需要的朋友可以參考下2019-01-01
用javascript做一個(gè)小游戲平臺(tái) (二) 游戲選擇器
昨天晚上“設(shè)計(jì)”了n久,那些代碼都還沒有運(yùn)行起來(lái),有點(diǎn)心急、有點(diǎn)郁悶。2010-01-01

