ax-custom-title.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view v-if="visible" class="ax ax-custom-title" :style="{padding}">
  3. <view class="__body" :style="{height}">
  4. <slot>
  5. <view @click="back()">
  6. <text v-if="this.pages.length>1" class="icon-back"></text>
  7. <text class="title">{{title || titleText}}</text>
  8. </view>
  9. </slot>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import PagesJSON from "@/pages.json";
  15. export default {
  16. name:"ax-custom-title",
  17. props:{
  18. // 显示首页按钮
  19. title:{type:String,default:''},
  20. // PC模式下无插槽内容也任然显示组件
  21. unlock:{type:Boolean,default:false},
  22. },
  23. data() {
  24. return {
  25. titleText: '',
  26. visible: false,
  27. padding: '0px',
  28. height: 'auto'
  29. };
  30. },
  31. async created() {
  32. const sys = await this.systemInfo();
  33. this.visible = sys.windowHeight === sys.screenHeight;
  34. if(this.visible==false) return;
  35. var rect = {top:0,left:0,right:0,bottom:0,width:0,height:0}
  36. if(sys.uniPlatform.indexOf('mp-')===0) rect = uni.getMenuButtonBoundingClientRect();
  37. var interval = sys.windowWidth - (rect.right || sys.windowWidth-10);
  38. const data = {
  39. top: rect.top,
  40. left: interval,
  41. right: (rect.width || interval*-1) + interval * 2,
  42. bottom: interval,
  43. componentHeight: rect.top+rect.height+interval,
  44. statusBarHeight: sys.statusBarHeight
  45. }
  46. var top = `${data.top}px`;
  47. var left = `${data.left}px`;
  48. var right = `${data.right}px`;
  49. var bottom = `${data.bottom}px`;
  50. if(['windows','mac'].includes(sys.platform)){
  51. top = 0;
  52. bottom = 0;
  53. right = left;
  54. rect.height = 45;
  55. if(this.unlock == false && !this.$slots.default) this.visible = false;
  56. }
  57. this.padding = [top,right,bottom,left].join(' ');
  58. this.height = `${rect.height || 50}px`;
  59. const currentPage = Array.from(getCurrentPages()).pop();
  60. const pageConfig = PagesJSON.pages.find(i=>i.path==currentPage.route);
  61. this.titleText = pageConfig.style.navigationBarTitleText;
  62. this.$emit('display',data);
  63. },
  64. computed:{
  65. pages(){
  66. return getCurrentPages();
  67. }
  68. },
  69. methods:{
  70. systemInfo(){
  71. return new Promise((resolve,reject)=>{
  72. uni.getSystemInfo({
  73. success: res=>resolve(res),
  74. fail: err=>reject(err)
  75. });
  76. });
  77. },
  78. back(){
  79. uni.navigateBack({delta: 1});
  80. }
  81. }
  82. }
  83. </script>
  84. <style scoped>
  85. @import url(../ax/ax.css);
  86. .ax-title-bar{
  87. width: 100vw;
  88. color: #000;
  89. position: relative;
  90. z-index: 999999;
  91. }
  92. .__body{
  93. display: flex;
  94. align-items: center;
  95. justify-content: space-between;
  96. }
  97. .__body::after{
  98. content: '';
  99. }
  100. .icon-back{
  101. margin-right: 5px;
  102. }
  103. .icon-back::before{
  104. content: "";
  105. display: inline-block;
  106. width: 10px;
  107. height: 10px;
  108. border-width: 0 0 2px 2px;
  109. border-style: solid;
  110. border-color: #000;
  111. transform-origin: center center;
  112. transform: rotate(45deg) translateY(-3px) translateX(1px);
  113. }
  114. </style>