轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

文字超出限制在末尾加省略号,showToast消息提示接口,在图片未能正确加载时 显示默认 ...

发布者: wxxxw | 发布时间: 2018-7-12 06:15| 查看数: 5459| 评论数: 1|帖子模式

一:文字超出限制如何在末尾加省略号

作者:YanzYan,原文地址 
当文字超出一行时会自动换行 那如何让文字不自动换行并在末尾加上省略号呢?

我查资料的时候搜到这个博客 

原贴的答案是这样的

  1. text {
  2. display: -webkit-box;
  3. word-break: break-all;
  4. text-overflow: ellipsis;
  5. white-space: nowrap;
  6. font-size: 32rpx;
  7. overflow: hidden;
  8. -webkit-box-orient: vertical;
  9. -webkit-line-clamp:2;
  10. }

然后自己试验了一下 发现这样写就可以达到效果了

  1. text{
  2. overflow:hidden; //超出一行文字自动隐藏
  3. text-overflow:ellipsis;//文字隐藏后添加省略号
  4. white-space:nowrap; //强制不换行
  5. }

二:在图片未能正确加载时 显示默认图片

用条件渲染 即wx:if,wx:elif,wx:else系列

eg1. 图片

  1. <block wx:for="{{data}}">
  2. <image wx:if="{{item.avatar == ''}}" class="avatar" src="/images/cinema.png"></image>
  3. <image wx:else="{{item.avatar}}" class="avatar" src="{{item.avatar}}" background-size="cover"></image>
  4. </block>

eg2. 文字 text、view标签是同样的效果

  1. <block wx:for="{{data}}">
  2. <view wx:if="{{item.entity_name==null}}" class="label"></view>
  3. <view wx:else="{{item.entity_name}}" class="label">{{item.entity_name}}</view>
  4. </block>

三:开发showToast消息提示接口

作者:阿灿,来自授权地址 
相信在学习小程序文档或者已经创建项目的开发员们,都很清楚小程序的wx.showToast接口只提供了两种icon【success和loading】展示形式,那假如我想要的是不要图标只要存粹的文字提醒呢?或者是我不要微信单方面提供的那种图片呢?想要自己指定的情况呢?这时候要怎么办..

这几天我根据wx.showToast接口提供的参数,跟着写了个消息提醒模板,

  • 1、如果没有指定icon图标地址,那么就是单纯的显示文字提示,否则就是图标+文字的模式,这时候就要确保icon指向的图片地址是正确的啦。

  • 2、如果没有指定duration提示的延迟时间,默认是1.5s,而我设置的最大值10s是不会自动隐藏消息提示的,除非手动hideToast. 单位:毫秒

  • 3、如果没有指定mask遮罩,默认是跟着显示的,防止触摸穿透

  • 4、如果没有指定cb回调函数,默认直接显示消息提醒,否则可以在等消息提示结束后即刻处理一些其他业务:例如地址跳转,改变订单状态等等

以下是整个模板代码结构:

showToast.wxml:

  1. <template name="showToast">
  2. <block wx:if="{{showToast.isShow? showToast.isShow: false}}">
  3. <view class="toast-bg" wx:if="{{showToast.mask==false? false : true}}"></view>
  4. <view class="toast-center">
  5. <view class="toast">
  6. <image class="toast-icon" src="{{showToast.icon}}" mode="scaleToFill" wx:if="{{showToast.icon}}" />
  7. <text class="toast-text">{{showToast.title}}</text>
  8. </view>
  9. </view>
  10. </block>
  11. </template>

showToast.wxss:

  1. /*showToast*/
  2. .toast-bg {
  3. position: fixed;
  4. top: 0;
  5. bottom: 0;
  6. z-index: 999999;
  7. width: 100%;
  8. height: 100%;
  9. background: rgba(0, 0, 0, .2);
  10. }
  11. /*水平居中必备样式*/
  12. .toast-center {
  13. position: fixed;
  14. z-index: 9999999;
  15. width: 100%;
  16. height: 100%;
  17. text-align: center;
  18. }
  19. .toast {
  20. display: inline-block;
  21. padding: 20rpx 40rpx;
  22. max-width: 600rpx;
  23. font-size: 28rpx;
  24. color: #fff;
  25. background: rgba(0, 0, 0, .5);
  26. border-radius: 10rpx;
  27. text-align: center;
  28. }
  29. /*垂直居中必备样式*/
  30. .toast-center::after{
  31. content: '';
  32. display: inline-block;
  33. width: 0;
  34. height: 100%;
  35. vertical-align: middle;
  36. }
  37. .toast .toast-icon {
  38. display: block;
  39. margin: 0 auto 10rpx auto;
  40. width: 50rpx;
  41. height: 50rpx;
  42. }

showToast.js:

  1. /*
  2. 显示toast提示
  3. title: 提示的内容 必填
  4. icon: 图标,//请指定正确的路径,选填
  5. duration: 提示的延迟时间,单位毫秒,默认:1500, 10000永远存在除非手动清除 选填
  6. mask: 是否显示透明蒙层,防止触摸穿透,默认:true 选填
  7. cb: 接口调用成功的回调函数 选填
  8. */
  9. function showToast(obj) {
  10. if (typeof obj == 'object' && obj.title) {
  11. if (!obj.duration || typeof obj.duration != 'number') { obj.duration = 1500; }//默认1.5s后消失
  12. var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
  13. obj.isShow = true;//开启toast
  14. if (obj.duration < 10000) {
  15. setTimeout(function () {
  16. obj.isShow = false;
  17. obj.cb && typeof obj.cb == 'function' && obj.cb();//如果有成功的回调则执行
  18. that.setData({
  19. 'showToast.isShow': obj.isShow
  20. });
  21. }, obj.duration);
  22. }
  23. that.setData({
  24. showToast: obj
  25. });
  26. } else {
  27. console.log('showToast fail:请确保传入的是对象并且title必填');
  28. }
  29. }
  30. /**
  31. *手动关闭toast提示
  32. */
  33. function hideToast() {
  34. var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
  35. if (that.data.showToast) {
  36. that.setData({
  37. 'showToast.isShow': false
  38. });
  39. }
  40. }
  41. module.exports = {
  42. showToast: showToast,
  43. hideToast: hideToast
  44. }

接下来是模板的引用和测试:

test.wxml:

  1. <import src="../../template/showToast.wxml" />
  2. <template is="showToast" data="{{showToast: showToast}}" />
  3. <!--上面两句话是放置模板的路径和传入的data! data传入方式写死固定-->
  4. <view bindtap="testToast" data-test="1">只传title,单纯文字提醒</view>
  5. <view bindtap="testToast" data-test="2">指定图标,图标+文字提醒</view>
  6. <view bindtap="testToast" data-test="3">指定duration,控制toast 3s消失</view>
  7. <view bindtap="testToast" data-test="31">指定duration=10s,手动2s后关闭toast</view>
  8. <view bindtap="testToast" data-test="4">指定mask,控制toast遮罩</view>
  9. <view bindtap="testToast" data-test="5">指定cb, 控制回调处理业务</view>

test.js:

  1. var feedbackApi=require('../../common/feedbackApi');//引入消息提醒暴露的接口
  2. Page({
  3. data:{
  4. },
  5. testToast: function(e){
  6. var test=e.target.dataset.test;
  7. if(test==1){
  8. feedbackApi.showToast({title: 'test shoToast title'})//调用
  9. }
  10. if(test==2){
  11. feedbackApi.showToast({
  12. title: 'test shoToast title',
  13. icon: '/pages/templateImg/loading.gif'
  14. })
  15. }
  16. if(test==3){
  17. feedbackApi.showToast({
  18. title: 'test shoToast title',
  19. duration: 3000
  20. })
  21. }
  22. if(test==31){
  23. feedbackApi.showToast({
  24. title: 'test shoToast title',
  25. duration: 10000
  26. })
  27. setTimeout(function(){
  28. feedbackApi.hideToast();
  29. }, 2000)
  30. }
  31. if(test==4){
  32. feedbackApi.showToast({
  33. title: 'test shoToast title',
  34. mask: false
  35. })
  36. }
  37. if(test==5){
  38. feedbackApi.showToast({
  39. title: 'test shoToast title',
  40. cb: function(){
  41. console.log('回调进来了,可以制定业务啦')
  42. }
  43. })
  44. }
  45. },
  46. onLoad: function(e){
  47. wx.setNavigationBarTitle({
  48. title: 'test showToast'
  49. })
  50. }
  51. })

演示如下:

以上,就是整个showToast模板的测试结果!如果有不同解决方案,欢迎留言指出~

最新评论

A.半生এ᭄° 发表于 2022-7-5 22:57
免费源码合集网

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表