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