navigation-bar.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Component({
  2. options: {
  3. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  4. },
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. extClass: {
  10. type: String,
  11. value: ''
  12. },
  13. title: {
  14. type: String,
  15. value: ''
  16. },
  17. background: {
  18. type: String,
  19. value: ''
  20. },
  21. color: {
  22. type: String,
  23. value: ''
  24. },
  25. back: {
  26. type: Boolean,
  27. value: true
  28. },
  29. loading: {
  30. type: Boolean,
  31. value: false
  32. },
  33. homeButton: {
  34. type: Boolean,
  35. value: false,
  36. },
  37. animated: {
  38. // 显示隐藏的时候opacity动画效果
  39. type: Boolean,
  40. value: true
  41. },
  42. show: {
  43. // 显示隐藏导航,隐藏的时候navigation-bar的高度占位还在
  44. type: Boolean,
  45. value: true,
  46. observer: '_showChange'
  47. },
  48. // back为true的时候,返回的页面深度
  49. delta: {
  50. type: Number,
  51. value: 1
  52. },
  53. },
  54. /**
  55. * 组件的初始数据
  56. */
  57. data: {
  58. displayStyle: ''
  59. },
  60. lifetimes: {
  61. attached() {
  62. const rect = wx.getMenuButtonBoundingClientRect()
  63. wx.getSystemInfo({
  64. success: (res) => {
  65. const isAndroid = res.platform === 'android'
  66. const isDevtools = res.platform === 'devtools'
  67. this.setData({
  68. ios: !isAndroid,
  69. innerPaddingRight: `padding-right: ${res.windowWidth - rect.left}px`,
  70. leftWidth: `width: ${res.windowWidth - rect.left }px`,
  71. safeAreaTop: isDevtools || isAndroid ? `height: calc(var(--height) + ${res.safeArea.top}px); padding-top: ${res.safeArea.top}px` : ``
  72. })
  73. }
  74. })
  75. },
  76. },
  77. /**
  78. * 组件的方法列表
  79. */
  80. methods: {
  81. _showChange(show: boolean) {
  82. const animated = this.data.animated
  83. let displayStyle = ''
  84. if (animated) {
  85. displayStyle = `opacity: ${
  86. show ? '1' : '0'
  87. };transition:opacity 0.5s;`
  88. } else {
  89. displayStyle = `display: ${show ? '' : 'none'}`
  90. }
  91. this.setData({
  92. displayStyle
  93. })
  94. },
  95. back() {
  96. const data = this.data
  97. if (data.delta) {
  98. wx.navigateBack({
  99. delta: data.delta
  100. })
  101. }
  102. this.triggerEvent('back', { delta: data.delta }, {})
  103. }
  104. },
  105. })