微信小程序自定义弹窗组件

业务背景:弹窗有时字体较多,超过7个字,不适用wx.showToast.

组件代码

csharp 复制代码
<view class="toast-box {{isShow? 'show':''}}" animation="{{animationData}}">
  <view class="toast-content" >
    <view class="toast-img">
      <block wx:if="{{type==='success'}}">
        <image class="toast-icon" src="/images/correct.png" />
      </block>
      <block wx:if="{{type==='fail'}}">
        <image class="toast-icon" src="/images/warn.png" />
      </block>
    </view>
    <view class="toast-title">{{title}}</view>
    <view style="width:100%;border-top: 1rpx solid #ddd;"></view>
    <view bindtap="handleClose" style="width: 100%;text-align: center;color: #666;line-height: 80rpx;">
    确 定
    </view>
  </view>
</view>

/* components/toast/toast.wxss */

csharp 复制代码
.toast-box {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  z-index: 11;
  display: none;
  background: rgba(0, 0, 0, .6);
  opacity: 0;
 }
 .show{
  display: block;
 }
 .toast-content {
  position: absolute;
  left: 50%;
  top: 45%;
  width:600rpx;
  /*height: 250rpx;*/
  border-radius: 10rpx;
  box-sizing: bordre-box;
  transform: translate(-50%, -50%);
  background: white;
 }
 .toast-img{
   width: 100%;
   height: 100rpx;
   padding-top: 15rpx;
   box-sizing: bordre-box;
   text-align: center;
 }
 .toast-icon{
   width: 100rpx;
   height: 100rpx;
 }
 .toast-title {
   width: 100%;
   padding: 30rpx;
   line-height: 45rpx;
   color: #666;
   text-align: center;
   font-size: 28rpx;
   box-sizing: border-box;
 }

// components/toast/toast.js

csharp 复制代码
Component({
  properties: {
  },
  data: {
   type: 'fail',
   title: '',
   isShow: false,
   animationData: ''
  },
  methods: {
   showToast: function (data) {
    const that = this;
    if (this._showTimer) {
     clearTimeout(this._showTimer)
    }
    if (this._animationTimer) {
     clearTimeout(this._animationTimer)
    }
    // display需要先设置为block之后,才能执行动画
    this.setData({
     title: data.title,
     type: data.type,
     isShow: true,
    });
    this._animationTimer = setTimeout(() => {
     const animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      delay: 0
     })
     animation.opacity(1).step();
     that.setData({
      animationData: animation.export(),
     })
    }, 50)
    this._showTimer = setTimeout(function () {
     that.hideToast();
     if (data.compelete && (typeof data.compelete === 'function')) {
      data.compelete()
     }
    }, data.duration)
   },
   handleClose(){
    this.hideToast();
   },
   hideToast: function () {
    if (this._hideTimer) {
     clearTimeout(this._hideTimer)
    }
    let animation = wx.createAnimation({
     duration: 200,
     timingFunction: 'ease',
     delay: 0
    })
    animation.opacity(0).step();
    this.setData({
     animationData: animation.export(),
    })
    this._hideTimer = setTimeout(() => {
     this.setData({
      isShow: false,
     })
    }, 0)
   }
  }
 })

引用部分

csharp 复制代码
// json
 {
  "usingComponents": {
    "vas-toast": "../../components/toast/toast"
  }
}
// html
<vas-toast id='toast'></vas-toast>
js
  onShow: function () {
    this.prompt = this.selectComponent("#toast");
  },
that.prompt.showToast({
            type: 'fail',
            title: res.data.errmsg,
            duration: 4000,
            compelete: function () {}
          })
相关推荐
说私域8 小时前
移动互联网生态下定制开发开源AI智能名片S2B2C商城小程序源码在营销技术中的应用与发展
人工智能·小程序·开源
微爱帮监所写信寄信8 小时前
微爱帮监狱写信寄信工具服务器【Linux篇章】再续:TCP协议——用技术隐喻重构网络世界的底层逻辑
linux·服务器·开发语言·网络·网络协议·小程序·监狱寄信
微爱帮监所写信寄信11 小时前
微爱帮监狱写信寄信小程序:MySQL核心日志与备份恢复安全架构
数据库·mysql·小程序·邮局·监狱寄信·挂号信·邮政
wan10412 小时前
用户隐私协议URL
微信小程序
Z单单13 小时前
微信小程序订单信息录入路径设置
微信小程序·小程序
码界奇点14 小时前
基于Spring Boot和微信小程序的小程序商城系统设计与实现
spring boot·微信小程序·小程序·毕业设计·源代码管理
微爱帮监所写信寄信14 小时前
微爱帮监狱寄信写信小程序:深入理解JavaScript中的Symbol特性
开发语言·javascript·网络协议·小程序·监狱寄信·微爱帮
计算机毕设指导615 小时前
基于微信小程序的智慧社区娱乐服务管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·娱乐
微爱帮监所写信寄信16 小时前
6G+AI:重构微爱帮监狱写信寄信小程序的特殊通信未来
人工智能·小程序·重构·6g·监狱信件·监狱系统·服刑人员子女
赵庆明老师16 小时前
uniapp 微信小程序页面JS模板
javascript·微信小程序·uni-app