navigation-bar.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. const wxDeviceInfo = wx.getDeviceInfo()
  64. const wxWindowInfo = wx.getWindowInfo()
  65. const isAndroid = wxDeviceInfo.platform === 'android'
  66. const isDevtools = wxDeviceInfo.platform === 'devtools'
  67. this.setData({
  68. ios: !isAndroid,
  69. innerPaddingRight: `padding-right: ${wxWindowInfo.windowWidth - rect.left}px;`,
  70. leftWidth: `width: ${wxWindowInfo.windowWidth - rect.left }px;`,
  71. safeAreaTop: isDevtools || isAndroid ? `height: calc(var(--height) + ${wxWindowInfo.safeArea.top}px); padding-top: ${wxWindowInfo.safeArea.top}px;` : ``
  72. })
  73. },
  74. },
  75. /**
  76. * 组件的方法列表
  77. */
  78. methods: {
  79. _showChange(show: boolean) {
  80. const animated = this.data.animated
  81. let displayStyle = ''
  82. if (animated) {
  83. displayStyle = `opacity: ${
  84. show ? '1' : '0'
  85. };transition:opacity 0.5s;`
  86. } else {
  87. displayStyle = `display: ${show ? '' : 'none'}`
  88. }
  89. this.setData({
  90. displayStyle
  91. })
  92. },
  93. back() {
  94. const data = this.data
  95. if (data.delta) {
  96. wx.navigateBack({
  97. delta: data.delta
  98. })
  99. }
  100. this.triggerEvent('back', { delta: data.delta }, {})
  101. }
  102. },
  103. })