描述

模仿ios浏览器底部弹框效果。

遮罩层淡入淡出,弹框高度根据内容自适应。

效果


源码

popup-bottom.wxml

  1. <!-- popup-bottom.wxml -->
  2. <view class="wrap" hidden="{{!myVisible}}">
  3. <view class="mask {{visible ? 'mask-show' : ''}}" bindtap="_onCancel"></view>
  4. <view class="box" id="modal-box" animation="{{animationData}}">
  5. <slot />
  6. </view>
  7. </view>

popup-bottom.js

  1. // popup-bottom.js
  2. Component({
  3. properties: {
  4. myVisible: {
  5. type: Boolean,
  6. value: false,
  7. observer: '_visibleChange',
  8. },
  9. },
  10. data: {
  11. visible: false,
  12. animation: null,
  13. animationData: null,
  14. },
  15. ready: function () {
  16. const animation = wx.createAnimation({
  17. duration: 200,
  18. timingFunction: "linear",
  19. delay: 0,
  20. });
  21. this.setData({
  22. animation,
  23. })
  24. },
  25. methods: {
  26. _visibleChange: function (newVal, oldVal, changedPath) {
  27. if (oldVal === false && newVal === true) {
  28. setTimeout(function () {
  29. this._onShow();
  30. }.bind(this), 0)
  31. }
  32. },
  33. _onShow: function () {
  34. const __this = this;
  35. const query = wx.createSelectorQuery().in(this);
  36. query.select('#modal-box').boundingClientRect(function (res) {
  37. const { animation } = __this.data;
  38. animation.translateY(-res.height).step();
  39. __this.setData({
  40. visible: true,
  41. animationData: animation.export(),
  42. })
  43. }).exec();
  44. },
  45. _onCancel: function () {
  46. const { animation } = this.data;
  47. animation.translateY(0).step();
  48. this.setData({
  49. visible: false,
  50. animationData: animation.export(),
  51. })
  52. setTimeout(function () {
  53. this.triggerEvent('myOnCancel');
  54. }.bind(this), 200)
  55. },
  56. },
  57. })

popup-bottom.wxss

  1. /* popup-bottom.wxss */
  2. .wrap {
  3. position: fixed;
  4. left: 0;
  5. top: 0;
  6. z-index: 99999;
  7. width: 100vw;
  8. height: 100vh;
  9. }
  10. .mask {
  11. position: fixed;
  12. top: 0;
  13. left: 0;
  14. z-index: 1;
  15. width: 100%;
  16. height: 100%;
  17. background: #000;
  18. opacity: 0;
  19. transition: 0.2s;
  20. }
  21. .mask-show {
  22. opacity: 0.4;
  23. }
  24. .box {
  25. position: fixed;
  26. top: 100vh;
  27. left: 0;
  28. z-index: 2;
  29. width: 100%;
  30. min-height: 100rpx;
  31. background: #fff;
  32. }

popup-bottom.json

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }

使用

test.wxml

  1. <button bindtap="handleShow">点我弹出popup</button>
  2. <popup-bottom myVisible="{{visible}}" bindmyOnCancel="handleCancel">
  3. <view>我是内容</view>
  4. <view>我是内容</view>
  5. <view>我是内容</view>
  6. <view>我是内容</view>
  7. <view>我是内容</view>
  8. </popup-bottom>

test.js

  1. Page({
  2. data: {
  3. visible: false,
  4. },
  5. handleShow: function () {
  6. this.setData({ visible: true });
  7. },
  8. handleCancel: function () {
  9. this.setData({ visible: false });
  10. },
  11. })

test.json

  1. {
  2. "navigationBarTitleText": "底部弹框",
  3. "usingComponents": {
  4. "popup-bottom": "/components/popup-bottom/popup-bottom"
  5. }
  6. }

[组件封装]微信小程序-底部弹框的更多相关文章

  1. 微信小程序底部弹框动画

    在写小程序的时候,一般会碰到底部弹出动画,就像下面这样的效果 直接进入正题 https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-animation.ht ...

  2. 关于微信小程序 modal弹框组件的介绍

    微信小程序 modal: 这里对微信小程序中 modal组件进行详细解析,我想开发微信小程序的小伙伴可以用到,这里小编就记录下modal的知识要点. modal modal类似于javascript中 ...

  3. [组件封装]微信小程序-图片批量上传照片墙

    描述 批量上传图片, 可设置最大上传个数, 可删除, 可设置默认值. 效果 源码 pictures-wall.wxml <view class="picturesWall"& ...

  4. 微信小程序之----弹框组件modal

    modal modal类似于javascript中的confirm弹框,默认情况下是一个带有确认取消的弹框,不过点击取消后弹框不会自动隐藏,需要通过触发事件调用函数来控制hidden属性. 官方文档 ...

  5. 【组件】微信小程序input搜索框的实现

    开发小程序的过程,是一个学习知识,解决问题的过程,每当实现了一个需求,总会有很大的成就感,每天记录一个开发过程中的细节.实现效果如下: 官方参考链接:https://developers.weixin ...

  6. 微信小程序 --- model弹框

    model弹框:在屏幕中间弹出,让你进行选择: 效果: 代码: <button type="primary" bindtap="btnclick"> ...

  7. [组件封装]微信小程序-日历

    描述 切换月份, 当天文案为今天, 日期背景变色, 日期红点标识, 点击选中日期. 效果 源码 calendar.wxml <view class="component"&g ...

  8. 微信小程序之弹框modal

    官方文档 <modal hidden="{{hidden}}" title="这里是title" confirm-text="自定义确定按钮&q ...

  9. 详解封装微信小程序组件及小程序坑(附带解决方案)

    一.序 上一篇介绍了如何从零开发微信小程序,博客园审核变智障了,每次代码都不算篇幅,好好滴一篇原创,不到3分钟从首页移出来了.这篇介绍一下组件封装和我的踩坑历程. 二.封装微信小程序可复用组件 首先模 ...

随机推荐

  1. python 写个冒泡排序吧

    冒泡排序 介绍: 冒泡排序(Bubble Sort,台湾译为:泡沫排序或气泡排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作 ...

  2. 吴裕雄--天生自然 R语言开发学习:时间序列(续三)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  3. python 同步与异步的性能区别及实例

    同步与异步的性能区别  1. #coding:utf-8 import gevent def task(pid): """ Some non-deterministic ...

  4. Docker容器时间同步问题

    具体操作: 为了保证容器和宿主机之间的时间同步,采用如下参数:-v /etc/localtime:/etc/localtime:ro但是在页面访问的时候时间依然相差8个小时: 该怎么破解! 回复: 1 ...

  5. 修改android项目sdk版本

    1.右键单击项目--->properties---->Resource----->Android在Project Bulid Target对话框中选择你需要的Android版本.2. ...

  6. Microsoft Translator发布粤语文本翻译

    今天,Microsoft Translator发布了粤语的文本翻译,新的语言增加将继续丰富微软翻译产品的生态系统*,让更多组织和个人能够快速且高效地实现翻译应用.在中国,大有约5500万人使用粤语(语 ...

  7. k3s原理分析丨如何搞定k3s node注册失败问题

    前 言 面向边缘的轻量级K8S发行版k3s于去年2月底发布后,备受关注,在发布后的10个月时间里,Github Star达11,000颗.于去年11月中旬已经GA.但正如你所知,没有一个产品是十全十美 ...

  8. 7-19 计算有n个字符串中最长的字符串长度 (40 分)

    编写程序,用于计算有n(1<n<10)个字符串中最长的字符串的长度.前导空格不要计算在内! 输入格式: 在第一行中输入n,接下的每行输入一个字符串 输出格式: 在一行中输出最长的字符串的长 ...

  9. vue中的自定义分页插件组件

    介绍一下,已经有很多的vue分页的组件了,大家都是大同小易,那么我就结合自身的使用,写出了一片文章 首先在新建一个分页模块 在模块中引入相应的代码,(内有详细的注释) template中 <di ...

  10. 大厂常问iOS面试题--性能优化篇

    1.造成tableView卡顿的原因有哪些? 1.最常用的就是cell的重用, 注册重用标识符 如果不重用cell时,每当一个cell显示到屏幕上时,就会重新创建一个新的cell 如果有很多数据的时候 ...