当前位置:   article > 正文

微信小程序自定义弹窗组件 action-sheet_微信插件 action

微信插件 action

最近公司在开发短剧小程序,短剧的剧集展示交互需要用到组件 action-sheet,小程序自带的有这个组件,但是这个组件支持的功能比较单一,相当于是一个选择表一样,不能自定义很多内容,只能自定义一个组件来使用。

 以下是这个组件的实现:

1. 组件的 index.js 代码;

  1. // components/action-sheet/index.js
  2. Component({
  3. data: {
  4. show: false,
  5. animation: "",
  6. maskAnimation: ''
  7. },
  8. properties: {
  9. actionShow: Boolean
  10. },
  11. observers: {
  12. 'actionShow': function (newValue) {
  13. this.setData({
  14. show: newValue,
  15. animation: newValue === true ? 'show-action-sheet' : 'hide-action-sheet',
  16. maskAnimation: newValue === true ? 'show-mask-animation' : 'hide-mask-animation'
  17. })
  18. }
  19. },
  20. methods: {
  21. cancelAction() {
  22. this.setData({
  23. animation: 'hide-action-sheet',
  24. maskAnimation: 'hide-mask-animation'
  25. })
  26. setTimeout(() => {
  27. this.setData({
  28. show: false
  29. })
  30. }, 300);
  31. }
  32. }
  33. })

2. 组件的 index.wxml 代码;

  1. <!-- components/action-sheet/index.wxss -->
  2. <view class="action-sheet {{maskAnimation}}" hidden="{{!show}}" bindtap="cancelAction">
  3. <view class="container {{animation}}">
  4. <slot></slot>
  5. </view>
  6. </view>

4. 组件的 index.wxss 代码;

  1. /* components/action-sheet/index.wxss */
  2. .action-sheet {
  3. position: fixed;
  4. top: 0;
  5. left: 0;
  6. right: 0;
  7. bottom: 0;
  8. opacity: 0;
  9. background-color: rgba(0, 0, 0, 0.3);
  10. }
  11. .container {
  12. position: absolute;
  13. left: 0;
  14. top: 100%;
  15. width: 100%;
  16. height: 70%;
  17. background-color: #fcf8f8;
  18. border-top-left-radius: 30rpx;
  19. border-top-right-radius: 30rpx;
  20. }
  21. .show-action-sheet {
  22. animation-name: show-animation;
  23. animation-duration: 0.25s;
  24. animation-timing-function: linear;
  25. animation-fill-mode: forwards;
  26. }
  27. .hide-action-sheet {
  28. animation-name: hide-animation;
  29. animation-duration: 0.25s;
  30. animation-timing-function: linear;
  31. animation-fill-mode: forwards;
  32. }
  33. .show-mask-animation {
  34. animation-name: show-mask;
  35. animation-duration: 0.25s;
  36. animation-timing-function: linear;
  37. animation-fill-mode: forwards;
  38. }
  39. .hide-mask-animation {
  40. animation-name: hide-mask;
  41. animation-duration: 0.25s;
  42. animation-timing-function: linear;
  43. animation-fill-mode: forwards;
  44. }
  45. @keyframes show-animation {
  46. from {
  47. transform: translateY(0);
  48. }
  49. to {
  50. transform: translateY(-100%);
  51. }
  52. }
  53. @keyframes hide-animation {
  54. from {
  55. transform: translateY(-100%);
  56. }
  57. to {
  58. transform: translateY(0);
  59. }
  60. }
  61. @keyframes show-mask {
  62. from {
  63. opacity: 0;
  64. }
  65. to {
  66. opacity: 1.0;
  67. }
  68. }
  69. @keyframes hide-mask {
  70. from {
  71. opacity: 1.0;
  72. }
  73. to {
  74. opacity: 0;
  75. }
  76. }

5. 组件的 index.json 配置;

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

6. 组件的使用;

在要使用组件的json文件中引入自定义的 action-sheet 组件:

  1. {
  2. "usingComponents": {
  3. "x-action-sheet": "./components/action-sheet/index"
  4. },
  5. "navigationStyle" : "custom"
  6. }

在wxml文件中使用组件:

  1. <x-action-sheet actionShow="{{rechargeActionShow}}">
  2. <!--自定义的内容-->
  3. </x-action-sheet>

rechargeActionShow 为控制 action-sheet 显示的变量。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/233803
推荐阅读
相关标签
  

闽ICP备14008679号