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

业务背景:弹窗有时字体较多,超过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 () {}
          })
相关推荐
千寻技术帮6 小时前
50024_基于微信小程序的食品购商城
小程序·源码·项目·文档·ppt
说私域12 小时前
定制开发AI智能名片S2B2C预约服务小程序的定制开发与优势分析
大数据·人工智能·小程序
云起SAAS21 小时前
名字姓名起名打分评分抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·名字姓名起名打分评分
说私域1 天前
从裂变能力竞争到技术水平竞争:开源AI智能名片链动2+1模式S2B2C商城小程序对微商企业竞争格局的重塑
人工智能·小程序·开源
开发加微信:hedian1161 天前
短剧小程序开发全攻略:技术选型与实现思路
微信·小程序·架构·aigc·交互
2501_915918411 天前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
星光一影1 天前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码
mysql·小程序·uni-app·php
阿奇__1 天前
uniapp h5 app 小程序获取当前定位
小程序·uni-app
2501_915106321 天前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
李纲明2 天前
Wordpress如何选择适合外贸的模板主题?
微信小程序·php