微信小程序自定义toast组件的方法详解【含动画】

yizhihongxing

我来为你详细讲解“微信小程序自定义toast组件的方法详解【含动画】”的攻略。

什么是Toast组件

Toast组件是一种提示框,通常用于向用户展示一些简短的信息或提示。在微信小程序中,Toast组件比较常见,通过它可以向用户提示请求数据的进度,或者一些操作的结果信息。

开始制作自定义Toast组件

1. 创建承载Toast的组件

在 WeUI 中,Toast组件是在基础组件之上的,因此你需要先在基础组件里新建一个名为toast的组件。

<template name="toast">
  <view class="weui-toast">
    <block wx:if="{{type!='text'}}">
        <view class="weui-mask"></view>
    </block>
    <view class="weui-toast__content">
        <block wx:if="{{type!='loading'}}">
        {{content}}
        </block>
        <block wx:if="{{type=='loading'}}">
          <image src="https://weui.io/images/loading.svg" class="weui-toast__loading" />
          <view class="weui-toast__loading-box"></view>
          <view class="weui-toast__loading-text">{{content}}</view>
        </block>
    </view>
  </view>
</template>

2. 定义组件样式

我们需要为toast组件添加样式。这里我们定义了两种样式,一种为text,一种为loading

// toast样式
.weui-toast {
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  z-index: 5000;

  &__content {
    display: flex;
    height: 100%;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: #fff;
    font-size: 16px;
    font-weight: 400;

    &--text {
      padding: 15px 20px;
      background-color: rgba(0,0,0,.5);
      border-radius: 5px;
      box-shadow: 0 10px 30px rgba(0,0,0,.2);
    }

    &--loading {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .weui-toast__loading {
        width: 50px;
        height: 50px;
        animation: rotate 1s linear infinite;

        @keyframes rotate {
          from { transform: rotate(0deg); }
          to   { transform: rotate(360deg); }
        }
      }
      .weui-toast__loading-box {
        margin-top: 10px;
        width: 100%;
        height: 1px;
        background-color: rgba(255, 255, 255, .2);
        position: relative;
        &:before,
        &:after {
          position: absolute;
          content: '';
          top: 0;
          left: 50%;
          width: 10px;
          height: 10px;
          margin-left: -5px;
          border-radius: 100%;
          background-color: #fff;
          animation: pulse 1s linear infinite;
        }
        &:before {
          margin-left:-15px;
          animation-delay:.5s;
        }
        &:after {
          margin-left:5px;
          animation-delay:.8s;
        }
      }
      .weui-toast__loading-text {
        font-size: 16px;
        line-height: 32px;
        margin-top: 10px;
      }
    }
  }

  &__mask {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,.3);
    z-index: 5001;
  }
}

3. 定义组件属性与方法

我们需要在toast组件中定义属性和方法。其中,type是toast类型,content是toast显示的内容,duration是toast显示的时间。

Component({
  properties: {
    type: {
      type: String,
      value: 'text'
    },
    content: {
      type: String,
      value: ''
    },
    duration: {
      type: Number,
      value: 2000
    }
  },
  methods: {
    show: function () {
      this.setData({
        show: true
      });
      setTimeout(() => {
        this.hide();
      }, this.data.duration);
    },
    hide: function () {
      this.setData({
        show: false
      });
    }
  }
})

至此,一个简单的自定义toast组件就已完成。接下来我们可以在项目中使用这个组件了。

4. 在页面中使用自定义Toast组件

下面是在页面中使用toast的示例。在这个例子中,我们创建了两个toast组件,一个是text类型,另一个是loading类型。

<toast id="textToast" duration="2000" content="这是一个text toats" type="text"></toast>
<toast id="loadingToast" duration="3000" content="这是一个loading toats" type="loading" ref="loadingToast"></toast>

在JavaScript代码中,我们可以通过调用toastshow()hide()方法来显示或隐藏toast组件。

Page({
  showToast: function(e) {
    var id = e.currentTarget.id;
    if (id === 'textToast') {
      this.selectComponent('#textToast').show();
    } else if ( id === 'loadingToast') {
      this.selectComponent('#loadingToast').show();
    }
  },
  hideToast: function () {
    this.selectComponent('#loadingToast').hide();
  }
})

showToast方法中,我们通过selectComponent方法获取到了toast组件,并且调用show()方法来显示toast组件。在hideToast方法中,我们调用了toast组件的hide()方法来隐藏组件。

通过以上步骤,自定义toast组件已经完成了。在应用中,我们通过调用show()hide()方法来控制toast的显示和隐藏。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:微信小程序自定义toast组件的方法详解【含动画】 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • Vue项目导入导出文件功能以及导出文件后乱码问题及解决

    下面是Vue项目导入导出文件功能以及乱码问题及解决的完整攻略。 1. Vue项目导入导出文件功能的实现思路 在Vue项目中,我们可以使用FileSaver.js 实现文件的导出。而文件的导入则需要使用HTML5 File API。具体实现思路如下: 首先确保引入了FileSaver.js库,可以使用npm包管理器进行安装: bashnpm install f…

    Vue 2023年5月27日
    00
  • Vue.js每天必学之组件与组件间的通信

    Vue.js每天必学之组件与组件间的通信 在Vue.js中,组件是一个重要的概念。组件可以方便地构建复杂的应用程序,并且可以通过组件之间的通信来实现数据的共享与交互。本文将对Vue.js中组件与组件间的通信进行详细的讲解,并给出两个示例说明。 组件 在Vue.js中,组件是一个独立的模块化单元,有自己的模板、数据和方法。组件可以嵌套,可以被重复使用,并且可以…

    Vue 2023年5月27日
    00
  • vue离开当前页面触发的函数代码

    当用户离开Vue页面时,我们可以使用beforeRouteLeave路由导航守卫去触发相应的函数代码。下面,我将详细讲解该攻略的应用步骤和示例说明。 步骤一:添加路由导航守卫 在Vue的路由配置中添加beforeRouteLeave守卫,并指定它所要触发的函数代码。以下是守卫函数的结构: beforeRouteLeave(to, from, next) { …

    Vue 2023年5月28日
    00
  • Vue导出Excel功能的全过程记录

    下面是详细讲解“Vue导出Excel功能的全过程记录”的完整攻略。 1. 背景说明 Excel是一个办公软件中常用的工具,它可以方便地进行表格数据的输入和编辑。在Web应用中,我们可能需要将表格数据以Excel格式导出,这就需要实现一个导出Excel的功能。Vue是一款流行的JavaScript框架,本文将介绍如何在Vue项目中实现导出Excel的功能。 2…

    Vue 2023年5月27日
    00
  • Vue中使用 Echarts5.0 遇到的一些问题(vue-cli 下开发)

    当在Vue项目中使用Echarts5.0时,可能会遇到以下问题: 1. 需要手动引入echarts.min.js 如需在vue组件中使用echarts5.0,应先手动引入echarts.min.js。可以通过NPM或从cdn获取: npm install echarts –save 然后在Vue组件中引入echarts.min.js: import ech…

    Vue 2023年5月29日
    00
  • 详细对比Ember.js和Vue.js

    详细对比Ember.js和Vue.js 在讨论Ember.js和Vue.js之前,我们需要了解什么是前端框架。前端框架是一种帮助我们快速构建Web应用程序的工具,使用前端框架可以节省我们的开发时间和精力。Ember.js和Vue.js都是非常优秀的前端框架,下面我们将逐一对比这两种框架,让大家更好地了解它们的优缺点。 Ember.js Ember.js是一种…

    Vue 2023年5月28日
    00
  • vue-router懒加载速度缓慢问题及解决方法

    Vue.js是一个轻量级的前端JavaScript框架,在构建单页面应用时非常高效且易用。Vue提供的vue-router路由管理器也非常好用,可以让我们轻松地进行路由控制和组件管理。然而,在使用vue-router时,我们可能会遇到懒加载速度缓慢的问题,本文将详细介绍这个问题的成因以及解决方法。 什么是vue-router懒加载 Vue.js中的路由可以通…

    Vue 2023年5月28日
    00
  • Vue2.0生命周期的理解

    关于Vue2.0生命周期的理解,我们可以说是一个Vue开发中的关键知识点之一。下面我会给出完整的攻略,涉及Vue实例的整个生命周期过程,包括各个周期阶段的触发时机、各个周期函数的执行顺序、常见实用场景等等,帮助你更好地理解Vue2.0的生命周期。 何为Vue2.0生命周期 Vue实例参照了生命周期的概念,每个组件(包括根组件)在被创建、更新、销毁时都有自己的…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部