小程序自定义showToast组件

预先说明:我用的colorUI的icon,如果没有用color UI,你可能需要引入一下color UI或者修改一下

后续:还是官方的方便,因为样式好说,但是页面一层一层、怎么控制弹框在当前层页面上显示很麻烦,也看了其他组件的一些绑定逻辑,额,看不懂。

情况

众所周知,小程序的消息提示框带上图标只能显示7个汉字,很不方便。
在这里插入图片描述
在做小程序的时候发现这个问题,连返回的错误消息都输出不完,要你何用。。因此自己整一个消息组件。

效果展示:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码:

toast.wxml

<view class="toast-mask">
  <view class="toast">
    <view class="toast-icon cuIcon-{
    {icon}}fill line-{
    {iconColor}} text-center"></view>
    <view class="toast-title">{
  {title}}</view>
    <view class="toast-content">{
  {content}}</view>
  </view>
</view>

toast.wxss

@import '/colorui/main.wxss';
@import '/colorui/icon.wxss';

.toast-mask 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中,自定义`Toast`是为了满足开发者更灵活地控制提示信息的显示效果。`Toast`是一个轻量级的组件,用于显示短时间的提示信息,通常会在页面底部弹出。要实现自定义`Toast`,你可以创建一个新的`component`,并覆盖默认的样式和行为。以下是一个简单的步骤: 1. 在`pages`文件夹下创建一个新文件(如`custom-toast/index.wxml`),添加自定义的`Toast`组件: ```html <!-- custom-toast/index.wxml --> <view class="custom-toast"> <view class="toast-message">{{ message }}</view> <button class="toast-close" bindtap="dismissToast">关闭</button> </view> ``` 2. 在同目录下的`custom-toast/index.wxss`中,定义样式: ```css .custom-toast { position: fixed; bottom: 0; left: 0; right: 0; background-color: rgba(0, 0, 0, 0.8); padding: 16rpx; display: flex; justify-content: center; align-items: center; z-index: 1000; transition: all 0.3s ease-in-out; } .toast-message { color: white; font-size: 18rpx; } .toast-close { position: absolute; right: 10rpx; top: 50%; transform: translateY(-50%); color: white; cursor: pointer; } ``` 3. 在`custom-toast/index.js`中,编写逻辑和方法: ```javascript Page({ data: { message: '', dismissTimeout: null }, dismissToast() { clearTimeout(this.dismissTimeout); this.setData({ message: '' }); }, showToast(message) { this.setData({ message: message }); this.dismissTimeout = setTimeout(() => this.dismissToast(), 2000); // 自定义关闭时间 } }) ``` 4. 在需要使用自定义`Toast`的地方调用`this.showToast()`方法,传递你想要显示的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值