Home > Web Front-end > JS Tutorial > How to implement custom toast in WeChat mini program

How to implement custom toast in WeChat mini program

亚连
Release: 2018-06-23 17:38:03
Original
1635 people have browsed it

This article mainly introduces the implementation method of custom toast in WeChat applet, briefly describes the use of toast that comes with WeChat applet, and analyzes the definition and use of custom toast in the form of examples. Friends in need can refer to it. Next

The example in this article describes the implementation method of custom toast in WeChat applet. Share it with everyone for your reference, the details are as follows:

1. WeChat official default toast

toast is the most common, almost every App has it There is such a special effect. Let’s take a look at the toast effect that comes with the mini program. I immediately want to die~~

The toast effect that comes with WeChat:

js file:

wx.showToast({
 title: '成功',
 icon: 'success',
 duration: 2000
})
Copy after login

The usage is super simple, but the official applet has several problems:

can only display two icons: success and loading

and icon Cannot be removed

The maximum duration is 10 seconds

2. Custom toast

Our most common toast is towards the bottom , and the height is relatively small~~

Look at the effect first:

It seems simple, but it is not simple to implement. How to achieve it:

1) Create a public toast template file, because every page needs to use toast

<!-- wetoast.wxml -->
<template name="wetoast">
 <view class="wetoast {{reveal ? &#39;wetoast_show&#39; : &#39;&#39;}}">
  <view class="wetoast__mask"></view>
  <view class="wetoast__bd {{position}}" animation="{{animationData}}">
   <block wx:if="{{title}}">
    <view class="wetoast__bd__title {{titleClassName || &#39;&#39;}}">{{title}}</view>
   </block>
  </view>
 </view>
</template>
Copy after login

2) JS mainly has the following usage

Core code:

let pages = getCurrentPages();
let curPage = pages[pages.length - 1];
Copy after login

This code is the core. getCurrentPages().length - 1 means that the page of the current page can be obtained. Only when the page is obtained can it be set through page.setData The data of the current page is bound to the toast.

Core code:

let animation = wx.createAnimation();
animation.opacity(1).step();
Copy after login

This code has a slow animation effect when the toast disappears.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement the side sliding menu effect in vue swiper

How to implement the triangular arrow labeling function using Angular

How to implement the timer function using Angular

How to implement the delay hiding function through JS

The above is the detailed content of How to implement custom toast in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template