微信小程序toast组件(解决wx.showToast文本最多显示两行问题)

这篇具有很好参考价值的文章主要介绍了微信小程序toast组件(解决wx.showToast文本最多显示两行问题)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

创建toast组件

微信小程序toast,微信小程序,小程序

 index.wxmi

<!--components/toast/index.wxml-->
<cover-view wx:if="{{show}}" class="toast {{type==='confirm' ? 'confirm' : ''}} {{show === null ? '' : show?'fadeIn':'fadeDown'}} {{mask?'toast-mask':''}}">
  <cover-view class="toast-container">
    <cover-view wx:if="{{type === 'success'}}">success</cover-view>
    <cover-view wx:if="{{type === 'fail'}}">fail</cover-view>
    <!-- cover-view 内文字在IOS 被遮挡截取显示不全的问题
    解决:在文字后面添加一个全角空格 就能解决该问题 -->
    <cover-view wx:if="{{message}}" class="toast-message">{{message}} </cover-view>
    <cover-view wx:if="{{tip}}" class="toast-message-tip">{{tip}}</cover-view>
    <cover-view wx:if="{{type === 'confirm'}}" class="confrim_button">
      <cover-view class="confirm-left" data-value="{{0}}" bindtap="confirmCallback">取消</cover-view>
      <cover-view class="confirm-right" data-value="{{1}}" bindtap="confirmCallback">确认</cover-view>
    </cover-view>
  </cover-view>
</cover-view>

index.less

/* components/toast/index.wxss */
.toast {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  z-index: 10000;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
}
.toast.confirm .toast-container {
  padding: 0;
  padding-top: 52rpx;
}
.toast-message {
  white-space: normal !important;
  line-height: 40rpx !important;
}
.toast.confirm .toast-message,
.toast.confirm .toast-message-tip {
  padding: 0 28rpx;
}
.toast.confirm .toast-message-tip {
  padding-bottom: 52rpx;
}
.toast-container {
  position: relative;
  background: #505050;
  border-radius: 20rpx;
  max-width: 80%;
  padding: 39rpx 60rpx;
  z-index: 1001;
  box-sizing: border-box;
  overflow: hidden;
  font-size: 28rpx;
  font-weight: 400;
  color: #ffffff;
  line-height: 40rpx;
  white-space: pre-wrap;
}
@keyframes aniIn {
  0% {
    opacity: 0;
    visibility: visible;
  }
  100% {
    opacity: 1;
    visibility: visible;
  }
}
@keyframes aniDown {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
  }
}
.fadeDown {
  animation: aniDown 0.2s linear;
  animation-fill-mode: forwards;
}
.fadeIn {
  animation: aniIn 0.2s linear;
  animation-fill-mode: forwards;
}
.toast-close {
  position: absolute;
  top: 28rpx;
  right: 28rpx;
}
.toast-close .close_image {
  width: 48rpx;
  height: 48rpx;
}
.toast-message-tip {
  font-size: 30rpx;
  font-weight: 400;
  color: #262626;
  line-height: 40rpx;
  margin-top: 28rpx;
  text-align: center;
  white-space: pre-wrap;
}
.confrim_button {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 0 88rpx 48rpx 88rpx;
}
.confrim_button .confirm-left,
.confrim_button .confirm-right {
  height: 70rpx;
  line-height: 70rpx;
  width: 200rpx;
  border-radius: 35rpx;
  border: 2rpx solid #c2c2c2;
  text-align: center;
  color: #565656;
  box-sizing: border-box;
}
.confrim_button .confirm-right {
  color: #ffffff;
  border: none;
  background: linear-gradient(270deg, #ff743b 0%, #ff312a 100%);
}

index.json

{
  "component": true,
  "usingComponents": {}
}

index.ts

// components/toast/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    mask: {
      type: Boolean,
      value: true,
    },
    show: {
      type: null,
      value: null,
    },
    duration: {
      type: Number,
      value: 2000,
    },
    message: {
      type: String,
      value: "",
    },
    tip: {
      type: String,
      value: "",
    },
    type: {
      type: String,
      value: "text",
    },
    closeButton: {
      type: Boolean,
      value: true,
    },
    className: {
      type: String,
      value: "",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {},
});

toast.js

let defaultOptions = {
  type: "text",
  mask: true,
  message: "",
  show: true,
  duration: 2000,
  selector: "#toast",
  closeButton: true,
  callback: null,
  tip: "",
  className: "",
};

function getContext() {
  const pages = getCurrentPages();
  return pages[pages.length - 1];
}

function isObj(message) {
  return message !== null && typeof message === "object"
    ? message
    : { message };
}

let queue = [];

function Toast(currentOption) {
  let options = Object.assign({}, defaultOptions, isObj(currentOption));
  let context = getContext();

  let toast = context.selectComponent(options.selector);
  if (!toast) {
    console.error(
      "未找到 toast 节点, 请确认 selector 及 context 是否正确, 检查页面是否引入toast组件!"
    );
    return;
  }

  toast.clear = (e) => {
    toast.setData({ show: false });
  };
  toast.confirmCallback = (e) => {
    clearTimeout(toast.timer);
    toast.clear();
    toast.setData({ show: false });
    options.callback && options.callback(e.target.dataset.value);
  };
  queue.push(toast);
  toast.setData(options);

  clearTimeout(toast.timer);
  if (options.duration > 0) {
    toast.timer = setTimeout(() => {
      toast.clear();
      options.callback && options.callback();
      queue = queue.filter((item) => item !== toast);
    }, options.duration);
  }
  return toast;
}

const createMethod = (type) => (options) =>
  Toast(Object.assign({}, isObj(type), isObj(options)));

Toast.success = createMethod("success");
Toast.fail = createMethod("fail");
Toast.confirm = createMethod({
  type: "confirm",
  closeButton: false,
  duration: 0,
});
Toast.loading = (message) => {
  wx.showLoading({
    title: message || "加载中...",
    mask: true,
  });
};
Toast.loading.clear = () => {
  wx.hideLoading();
};

Toast.clear = () => {
  queue.forEach((toast) => {
    toast.clear();
  });
  queue = [];
};

export default Toast;

使用

index.json

微信小程序toast,微信小程序,小程序

index.wxml

<toast id="toast"></toast>

 index.ts

import Toast from "../../../components/toast/toast.js";
Toast({
  message: "券面值过大,建议券面值与使用门槛的比例小于等于0.4",
});

微信小程序toast,微信小程序,小程序

 效果

微信小程序toast,微信小程序,小程序文章来源地址https://www.toymoban.com/news/detail-527072.html

到了这里,关于微信小程序toast组件(解决wx.showToast文本最多显示两行问题)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序文本组件text使用详解-微信小程序系统学习攻略​

    也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好。 在微信小程序中,组件 text 用来显示文本,基本使用代码如下: 1 基本样式设置 基本使用还是比较简单的,下面咱们来论述一下文本样式的设置,首先是给他设置一个 class 然后在对应的 wxss 文件中编写

    2023年04月13日
    浏览(34)
  • 微信小程序不能使用wx.getlocation的解决方法

    1、没在小程序开发平台申请开通wx.getlocation API; 2、没有在app.json文件中声明配置; 3、开发版本库较高,调到2.25.3试试; 4、打开微信小程序页面右上角三个点,打开小程序设置,查看是否允许使用位置信息。

    2024年02月11日
    浏览(41)
  • 微信小程序富文本组件mp-html

    支持在多个主流的小程序平台和  uni-app  中使用 支持丰富的标签(包括  table 、 video 、 svg  等) 支持丰富的事件效果(自动预览图片、链接处理等) 支持设置占位图(加载中、出错时、预览时) 支持锚点跳转、长按复制等丰富功能 支持大部分  html  实体 丰富的插件(关

    2024年02月16日
    浏览(31)
  • 微信小程序 --- 通用模块封装(showToast,showModal ,本地存储)

    目录 01. 为什么进行模块封装 02. 消息提示模块封装 03. 模态对话框封装 04. 封装本地存储 API 05. 拓展:封装异步存储API+优化代码 01. 为什么进行模块封装 在进行项目开发的时候,我们经常的会频繁的使用到一些 API, 例如: wx.showToast()  、 wx.showModal() 等消息提示  API  ,这些

    2024年02月22日
    浏览(32)
  • 微信小程序授权登录wx.getUserProfile获取不到昵称及头像解决方案

    半年前做的个小程序,更新了二个文字,重新上传审核通过,悲剧了,新用户的昵称全部变为微信用户,头像全部变为默认头像,查了半天代码没找到原因,相当头大,搜了一下文档,尴尬了,11月9号新更新的规则,不再返回昵称和头像值....需要用头像昵称获取能力去触发获

    2024年02月11日
    浏览(30)
  • 微信小程序使用第三方组件wxParse加载富文本html

    微信小程序 微信小程序加载富文本html 微信小程序富文本第三方组件wxParse wxParse 富文本html wxParse 是一个微信小程序富文本解析组件,支持支持Html及markdown转wxml。 wxParse gitHub地址:https://github.com/icindy/wxParse 目前项目已停止维护了,原因未知。 按照gitHub上的指,下载demo之后,

    2024年02月12日
    浏览(30)
  • 解决微信小程序bindgetphonenumber和wx.login获取的code不同步问题

    微信小程序使用 手机号快速验证组件 在获取用户手机号的时候,经常会因为提交参数的code和iv、encryptedData参数匹配不一致而报错。其根本原因在官方有相应的解释: 注意使用旧版本组件时 ,需先调用wx.login接口。所以在用户点了拒绝之后授权之后,需要重新获取调用wx.lo

    2024年02月11日
    浏览(34)
  • 微信小程序showToast在真机中显示时间不可控制,显示时间短

    现象: 使用uniapp开发微信小程序,使用showToast,设置duration来控制提示展现时长,发现在微信开发者工具正常,在真机中显示时间比较短,并且设置duration不生效。 原因: 排查发现,是因为在调用showToast之后,又调用了hideLoading() ,执行hideLoading的时候也会把showToast也关闭。

    2024年02月13日
    浏览(28)
  • 【微信小程序创作之路】- 小程序事件绑定、动态提示Toast、对话框 Modal

    第六章 小程序事件绑定、动态提示Toast、对话框 Modal 本章主要讲解小程序事件绑定、动态提示Toast、对话框 Modal,结合代码示例,我们来研究一下! 事件是 视图层到逻辑层的通讯方式 。事件是小程序和用户互动的主要方式,通过事件将用户在 视图层 的行为,反馈到 逻辑层

    2024年02月14日
    浏览(40)
  • 微信小程序报错“qqmap-wx-jssdk.js‘ is not defined”解决方法

    一、问题描述 在使用微信小程序开发过程中,有时会遇到“qqmap-wx-jssdk.js‘ is not defined”的报错信息。这个错误通常与腾讯地图SDK相关,表明小程序试图引用一个未定义的脚本。 二、解决方案 解决此问题的方法有多种,以下是一些常见的解决方案: 检查引用顺序:确保在使

    2024年04月15日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包