gui-switch-navigation2.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <scroll-view
  3. :scroll-with-animation="scorllAnimation"
  4. :scroll-x="true"
  5. :show-scrollbar="false"
  6. :class="['gui-scroll-x', isCenter ? 'gui-nav-center' : '']"
  7. :style="{width:width+'rpx'}"
  8. :scroll-left="scrollLeft">
  9. <view
  10. class="gui-scroll-x-item gui-columns gui-relative"
  11. :id="'tab-'+index+(random+'')"
  12. :style="{
  13. width:size+'rpx',
  14. height:(lineHeight+lineHeightSamll+25)+'rpx'
  15. }"
  16. v-for="(item, index) in itemsIn"
  17. :key="index"
  18. @tap="navchange"
  19. :data-index="index">
  20. <text
  21. class="gui-block gui-text-center"
  22. :class="[customClass, currentIndexIn == index ? activeClass : '']"
  23. :style="{
  24. lineHeight:lineHeight+'rpx', height:lineHeight+'rpx',
  25. fontSize:currentIndexIn == index ? activeFontSize : fontSize,
  26. fontWeight:currentIndexIn == index ? 'bold' : ''
  27. }">{{item.title}}</text>
  28. <text
  29. class="gui-block gui-text-center gui-third-text"
  30. :class="[customClass, currentIndexIn == index ? activeClass : '']"
  31. :style="{
  32. lineHeight:lineHeightSamll+'rpx',
  33. height:lineHeightSamll+'rpx',
  34. fontSize:fontSizeSmall
  35. }">{{item.desc}}</text>
  36. <view
  37. class="nav2-active-line-in gui-flex gui-row gui-justify-content-center gui-fade-in"
  38. v-if="currentIndexIn == index">
  39. <view class="nav2-active-line"
  40. :class="activeBorderClass"></view>
  41. </view>
  42. </view>
  43. </scroll-view>
  44. </template>
  45. <script>
  46. export default {
  47. name : "gui-switch-navigation2",
  48. props : {
  49. width : {type : Number, default : 690},
  50. isCenter : {type : Boolean, default : false},
  51. currentIndex : {type : Number, default : 0},
  52. size : {type : Number, default : 138},
  53. fontSize : {type : String, default : '28rpx'},
  54. activeFontSize : {type : String, default : '28rpx'},
  55. lineHeight : {type : Number, default : 52},
  56. fontSizeSmall : {type : String, default : '22rpx'},
  57. lineHeightSamll : {type : Number, default : 28},
  58. items : {type : Array, default : function () {return []}},
  59. customClass : {type : Array, default : function () {return ['gui-primary-text'];}},
  60. activeClass : {type : Array, default : function () {
  61. return ['gui-primary-color', 'gui-dark-text-level-1'];
  62. }},
  63. scorllAnimation : {type : Boolean, default : true},
  64. activeBorderClass : {type : Array, default : function () {return ['gui-primary-border-color'];}}
  65. },
  66. data() {
  67. return {
  68. currentIndexIn : 0,
  69. itemsIn : [],
  70. random : 1,
  71. scrollLeft : 0,
  72. scrllTimer : null
  73. }
  74. },
  75. created:function(){
  76. this.currentIndexIn = this.currentIndex;
  77. this.itemsIn = this.items;
  78. this.random = this.randomNum();
  79. },
  80. watch:{
  81. currentIndex : function(value){ this.currentIndexIn = value; },
  82. items : function(value){ this.itemsIn = value; },
  83. currentIndexIn : function(val){
  84. if(this.isCenter){return ;}
  85. if(this.scrllTimer != null){clearTimeout(this.scrllTimer);}
  86. this.scrllTimer = setTimeout(()=>{this.setLeft();}, 200);
  87. }
  88. },
  89. methods:{
  90. navchange : function (e){
  91. this.currentIndexIn = e.currentTarget.dataset.index;
  92. this.$emit('change', Number(e.currentTarget.dataset.index))
  93. },
  94. randomNum : function () {
  95. return parseInt(Math.random() * 1000);
  96. },
  97. setLeft : function () {
  98. if(this.isCenter){return ;}
  99. var itemWidth = Number(this.size);
  100. var left = (Number(this.currentIndexIn) + 1) * itemWidth - Number(this.width) / 2 - itemWidth / 2;
  101. var maxLeft = Number(this.itemsIn.length) * itemWidth - this.width;
  102. maxLeft = uni.upx2px(maxLeft);
  103. left = uni.upx2px(left);
  104. if(left > maxLeft){left = maxLeft;}
  105. if(left < 0){left = 0;}
  106. this.scrollLeft = left;
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped>
  112. .gui-nav-center{justify-content:center; text-align:center;}
  113. .nav2-active-line-in{height:18rpx; overflow:hidden; margin-top:6rpx;}
  114. .nav2-active-line{width:18rpx; height:18rpx; border-radius:20rpx;
  115. border-width:6rpx; border-style:solid; margin-top:-15rpx;}
  116. /* #ifdef APP-NVUE */
  117. .nav2-active-line{width:30rpx; height:30rpx;}
  118. /* #endif */
  119. </style>