当前位置: 首页 > news >正文

微信小程序中实现气泡提示框、图片css加载动画及容错处理

一.小程序中实现:悬浮提示框 / 气泡提示框
  • 1.效果图

  • 封装组件完整代码

    Component({properties:{bgColor:{type: String, value:'#000000'}, textColor:{type: String, value:'#ffffff'}, content:{type: String, value:'将会员系统的积分合并到先见'}}, data:{show:false}, methods:{toggleTip(){// 显示提示框 this.setData({show:true});//3秒后自动隐藏 setTimeout(()=>{this.setData({show:false});},3000);}}})<viewclass="tip-wrapper"><!-- 你的感叹号图标 --><imageclass="info-icon"src="https://images.hysound.com.cn/wechat/AB/img_04.png"mode="aspectFit"bindtap="toggleTip"></image><!-- 提示气泡 --><viewclass="bubble {{ show ? 'show' : 'hide' }}"style="background:{{ bgColor }};color:{{ textColor }}">{{content}}<!-- 箭头在底部偏左 --><viewclass="arrow"style="border-top-color:{{ bgColor }}"/></view></view>.tip-wrapper{position: relative;display: inline-block;vertical-align: middle;bottom: 5rpx;}.info-icon{width: 38rpx;height: 38rpx;margin-left: 6rpx;display: block;}.bubble{position: absolute;bottom: calc(38rpx + 18rpx);/* 总共上移了4rpx */ left: -16rpx;padding: 20rpx 28rpx;border-radius: 8rpx;font-size: 28rpx;white-space: nowrap;z-index:999;background:#000;color:#fff;transition: opacity0.3s ease, visibility0.3s ease;}.arrow{position: absolute;bottom: -18rpx;left: 28rpx;width:0;height:0;border-left: 12rpx solid transparent;border-right: 12rpx solid transparent;border-top: 18rpx solid#000;}.bubble.show{opacity:1;visibility: visible;}.bubble.hide{opacity:0;visibility: hidden;}
  • 3.父组件中josn中引入,wxml中使用

二、图片css加载动画,及图片未显示的错误处理
  • 1.效果图:

  • 2.完整代码

    Component({properties:{src:{type: String, value:'', observer(newVal){if(newVal){console.log('子组件:src更新', newVal);// 真机调试能看到 this.setData({imgLoaded: false, imgError:false});}}}, mode:{type: String, value:'aspectFill'// 修复:换成aspectFill,适配固定宽高}, width:{type: String, value:'300rpx'}, height:{type: String, value:'200rpx'}, radius:{type: Number, value:12}}, data:{imgLoaded: false, imgError:false}, methods:{onImgLoad(){console.log('子组件:图片加载成功');// 真机日志能看到 this.setData({imgLoaded:true});this.triggerEvent('load',{success:true});},onImgError(){console.log('子组件:图片加载失败');this.setData({imgError:true});this.triggerEvent('error',{success:false});}}});
    <view class="img-container"style="width:{{width}};height:{{height}};border-radius:{{radius}}rpx;"><view class="content-wrapper"wx:if="{{!imgLoaded || imgError}}"><view class="loading-dots"><span class="dot"></span></view><view class="error-placeholder"wx:if="{{imgError}}"><text class="error-text">图片加载失败</text></view></view><image class="content-img"src="{{src}}"mode="{{mode}}"bindload="onImgLoad"binderror="onImgError"lazy-load style="opacity: {{imgLoaded && !imgError ? 1 : 0}};"></image></view>
    .img-container{position: relative;background-color:#ffffff;overflow: hidden;display: flex;align-items: center;justify-content: center;margin:0auto;}.content-wrapper{display: flex;flex-direction:column;align-items: center;justify-content: center;width:100%;height:100%;gap: 12rpx;/* 增大动画和文字间距,更美观 */}/* 三点加载动画核心 - 优化版 */ .loading-dots{display: flex;gap: 10rpx;/* 圆点间距加宽,视觉更舒展 */ align-items: center;justify-content: center;}/* 三个圆点基础样式 - 调整大小+颜色深浅 */ .loading-dots .dot, .loading-dots::before, .loading-dots::after{content:'';width: 14rpx;/* 缩小圆点,更精致 */ height: 14rpx;border-radius:50%;background-color:#88bc07; /* 主题绿 */opacity:0.8;/* 初始透明度,动画更自然 */ /* 优化动画曲线,更丝滑 */ animation: dot-flash1.2s infinite ease-in-out both;}/* 动画延迟调整 - 节奏更舒服 */ .loading-dots::before{animation-delay: 0s;}.loading-dots .dot{animation-delay:0.25s;}.loading-dots::after{animation-delay:0.5s;}/* 优化动画关键帧 - 缩放+透明度结合 */ @keyframes dot-flash{0%,100%{opacity:0.4;/* 初始/结束透明度更低 */ transform: scale(0.9);/* 缩小幅度减小,更柔和 */}50%{opacity:1;/* 高亮时完全不透明 */ transform: scale(1.3);/* 放大幅度适中,不夸张 */}}/* 图片样式 */ .content-img{position: absolute;top:0;left:0;width:100%;height:100%;object-fit: cover;/* 图片显示过渡动画 */ transition: opacity0.4s ease-in-out;}/* 加载失败占位 - 优化文字样式 */ .error-placeholder{display: flex;align-items: center;justify-content: center;}.error-text{font-size: 26rpx;/* 增大文字,更易读 */ color:#888; /* 调整文字颜色,不刺眼 */font-weight:400;}

    父组件中使用:

http://www.jsqmd.com/news/507533/

相关文章:

  • 2026化工领域钢衬四氟储罐优质厂家推荐榜 - 资讯焦点
  • 标签多伯努利滤波器(LMB):多目标跟踪中的身份一致性保障机制
  • 深入解析set_max_delay与set_min_delay在异步电路时序约束中的关键作用
  • 工业设计赋能:CYBER-VISION零号协议解读与生成SolidWorks设计注释
  • 壹方设计电话查询:获取联系方式的实用建议 - 品牌推荐
  • 摒弃有害厨具,京尚黑科技陶瓷锅,开启高端健康烹饪时代
  • SpringBoot如何快速精通?
  • Cortex-M3 特色功能——位带操作(Bit-Band)
  • 商店盗窃行为破窗行为商品安防检测数据集VOC+YOLO格式2173张1类别
  • 2026环保合规纸塑染料助剂优质厂家推荐 - 资讯焦点
  • Qwen3.5-9B图文对话精彩案例:上传电路图问原理、传PPT页问逻辑漏洞
  • 免费获取乡镇级GeoJson边界数据的3种实用方法
  • 气熙B7空气净化器电话查询:使用前需了解的关键注意事项 - 品牌推荐
  • 长寿命检测开关的新选择:ALPS SPVT210101 VS 国产替代 TONEVEE KFC-VT-318BY 高达2万次寿命
  • 履霜坚冰,龙战于野:当权力失去约束,欲望成为人性的灾难
  • AES算法解剖课:用MATLAB逐行还原字节代换/行位移的数学之美
  • PyTorch中autograd.Function.apply的5个实战技巧(附自定义ReLU实现)
  • Acme .NET 工具类库:一站式解决.NET开发高频场景问题
  • 室内要素识别建筑物内部地面墙壁天花板识别分割数据集labelme格式1031张3类别
  • 2026年消防桥架厂家推荐:防火/镀锌/大跨度/节能桥架专业供应商精选——郑州畅通机电有限公司 - 品牌推荐官
  • 2026 SRM 系统深度测评:鲸采云凭自定义适配多行业采购场景
  • 2024年Java vs Go vs Python:企业级开发选谁更香?实战代码对比
  • [特殊字符] 当 AI 拥有「过目不忘」:OpenClaw 记忆系统完全指南
  • 2026年无锡抖音代运营TOP5名单出炉,行业格局数据公布 - 精选优质企业推荐榜
  • 郑州私人订制月子服务哪家? - 中媒介
  • 深度学习实战:LSTM与Attention机制融合优化城市交通流量预测
  • **reZonator**软件使用教程
  • 三角算法 200 刻度有规律
  • 日本三井NMN和其他高纯NMN相比,真正差异不只在数字 - 资讯焦点
  • ANSYS Workbench模态仿真入门:5步搞定方形薄板自由边界振动分析