gui-switch-navigation2.vue 3.8 KB

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