customer-navbar.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //获取应用实例
  2. const app = getApp()
  3. Component({
  4. options: {
  5. multipleSlots: true
  6. },
  7. properties: {
  8. title: { //导航栏标题
  9. type: String,
  10. value: '诺信云'
  11. },
  12. color: { //标题字体颜色
  13. type: String,
  14. value: '#fff'
  15. },
  16. transparent: { //是否透明
  17. type: Boolean,
  18. value: false
  19. },
  20. background: { //背景色
  21. type: String,
  22. value: '#007dff'
  23. },
  24. border: { //导航栏下边框样式
  25. type: String,
  26. value: 'border-bottom:none'
  27. },
  28. cusPath: { //用户自定义返回操作
  29. type: Boolean,
  30. value: false
  31. },
  32. backPath: { //左侧返回按钮点击后的返回路径(没有的话默认返回上一页)
  33. type: Boolean,
  34. value: false
  35. },
  36. backImage: { //左侧箭头图片
  37. type: String,
  38. value: '../../images/icon-back.svg'
  39. },
  40. loading: { //是否显示导航栏loading
  41. type: Boolean,
  42. value: false
  43. },
  44. dbclickBackTop: { //双击标题返回顶部功能是否开启
  45. type: Boolean,
  46. value: false
  47. },
  48. leftType: { //标题显示方式normal(安卓居左,ios居右),android(无论ios还是安卓,全部居左),ios(无论安卓还是ios,全部居中)
  49. type: String,
  50. value: 'normal'
  51. }
  52. },
  53. data: {
  54. showBack: false, //是否显示返回按钮
  55. },
  56. attached: function () {
  57. let systemInfo = app.globalData.systemInfo
  58. if (systemInfo) {
  59. this.setNavbar(systemInfo)
  60. } else {
  61. wx.getSystemInfo({
  62. success: res => {
  63. this.setNavbar(res)
  64. }
  65. })
  66. }
  67. //是否显示返回按钮(如果是第一页,就不显示,如果不是第一页,就显示)
  68. let pages = getCurrentPages()
  69. if (pages.length > 1) {
  70. this.setData({
  71. showBack: true
  72. })
  73. }
  74. },
  75. methods: {
  76. //点击返回按钮
  77. goBack() {
  78. if (this.data.cusPath === false) {
  79. if (this.data.backPath) {
  80. wx.switchTab({
  81. url: this.data.backPath
  82. })
  83. } else {
  84. wx.navigateBack()
  85. }
  86. } else {
  87. this.triggerEvent('back')
  88. }
  89. },
  90. //双击返回顶部
  91. doubleClick(e) {
  92. if (!this.data.dbclickBackTop) {
  93. return
  94. }
  95. if (this.timeStamp && (e.timeStamp - this.timeStamp < 300)) {
  96. this.timeStamp = 0
  97. wx.pageScrollTo({
  98. scrollTop: 0,
  99. duration: 300
  100. })
  101. } else {
  102. this.timeStamp = e.timeStamp
  103. }
  104. },
  105. //设置自定义导航信息
  106. setNavbar(systemInfo) {
  107. let isSupport = !!wx.getMenuButtonBoundingClientRect //是否支持获取胶囊按钮信息
  108. let rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null //获取胶囊按钮信息
  109. let ios = !!(systemInfo.system.toLowerCase().search('ios') + 1) //是否是ios系统
  110. let topBarHeight = ios ? (44 + statusBarHeight) : (48 + statusBarHeight) //获取导航栏高度
  111. let statusBarHeight = systemInfo.statusBarHeight //获取状态栏高度
  112. let leftStyle
  113. if (ios) {
  114. if (this.data.leftType !== 'android') {
  115. leftStyle = isSupport ? 'width:' + (systemInfo.windowWidth - rect.left) + 'px' : ''
  116. } else {
  117. leftStyle = 'margin-right:18px;'
  118. }
  119. } else {
  120. if (this.data.leftType !== 'ios') {
  121. leftStyle = 'margin-right:18px;'
  122. } else {
  123. leftStyle = isSupport ? 'width:' + (systemInfo.windowWidth - rect.left) + 'px' : ''
  124. }
  125. }
  126. this.setData({
  127. ios,
  128. topBarHeight,
  129. statusBarHeight,
  130. innerWidth: isSupport ? 'width:' + rect.left + 'px' : '',
  131. innerPaddingRight: isSupport ? 'padding-right:' + (systemInfo.windowWidth - rect.left) + 'px' : '',
  132. leftStyle
  133. })
  134. let eventDetail = {
  135. topBarHeight,
  136. statusBarHeight
  137. }
  138. this.triggerEvent('getNavbarInfo', eventDetail)
  139. }
  140. }
  141. })