gui-switch-navigation.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-into-view="autoLeft"
  9. :scroll-left="scrollLeft">
  10. <view
  11. class="gui-scroll-x-items gui-columns"
  12. :id="'tab-'+index+(random+'')"
  13. :style="{
  14. width:size == 0 ? 'auto' : size+'rpx',
  15. marginRight:margin+'rpx',
  16. paddingLeft:padding,
  17. paddingRight:padding
  18. }"
  19. v-for="(item, index) in itemsIn"
  20. :key="index"
  21. @tap="change"
  22. :data-index="index">
  23. <view class="gui-flex gui-nowrap gui-align-items-start"
  24. :class="[textAlign == 'center' ? 'gui-justify-content-center' : '']">
  25. <text
  26. :class="[
  27. 'gui-block-text',
  28. 'gui-border-box',
  29. currentIndexIn == index ? 'nav-active' : ''
  30. ]"
  31. :style="{
  32. color:currentIndexIn == index ? activeColor : color,
  33. textAlign : textAlign, lineHeight:lineHeight,
  34. fontSize:currentIndexIn == index ? activeFontSize : fontSize,
  35. fontWeight:currentIndexIn == index ? activeFontWeight : ''}">{{item.name}}</text>
  36. <view v-if="item.tips">
  37. <text
  38. v-if="item.tips != 'point'"
  39. class="gui-nav-tips gui-block-text"
  40. :style="tipsStyle">{{item.tips}}</text>
  41. <text
  42. v-else
  43. class="gui-nav-tips gui-block-text"
  44. :style="tipsStyle+'; width:12rpx; height:12rpx; over-flow:hidden; padding:0rpx; margin-top:10rpx;'"></text>
  45. </view>
  46. </view>
  47. <view
  48. class="gui-flex gui-rows"
  49. :style="{justifyContent:activeDirection}">
  50. <view class="nav-active-line"
  51. :class="[currentIndexIn == index && animatie ?'gui-nav-scale':'']"
  52. :style="{
  53. backgroundImage:activeLineBg, width:activeLineWidth,
  54. height:activeLineHeight, borderRadius:activeLineRadius
  55. }"
  56. v-if="currentIndexIn == index"></view>
  57. </view>
  58. </view>
  59. </scroll-view>
  60. </template>
  61. <script>
  62. export default {
  63. name : "gui-switch-navigation",
  64. props : {
  65. width : {type : Number, default : 690},
  66. isCenter : {type : Boolean, default : false},
  67. currentIndex : {type : Number, default : 0},
  68. size : {type : Number, default : 120},
  69. fontSize : {type : String, default : '28rpx'},
  70. activeFontSize : {type : String, default : '28rpx'},
  71. items : {type : Array, default : function () {return []}},
  72. activeLineBg : {type : String, default : "linear-gradient(to right, #66BFFF,#3388FF)"},
  73. color : {type : String, default : "#333333"},
  74. activeColor : {type : String, default : "#333333"},
  75. activeLineHeight : {type : String, default : '6rpx'},
  76. activeLineWidth : {type : String, default : "36rpx"},
  77. activeLineRadius : {type : String, default : "0rpx"},
  78. activeDirection : {type : String, default : ""},
  79. activeFontWeight : {type : Number, default : 700},
  80. margin : {type : Number, default : 0},
  81. textAlign : {type : String, default : ''},
  82. lineHeight : {type : String, default : '50rpx'},
  83. padding : {type : String, default : '0rpx'},
  84. animatie : {type : Boolean, default : true},
  85. scorllAnimation : {type : Boolean, default : true},
  86. // #ifdef APP-NVUE
  87. tipsStyle : {
  88. type : String,
  89. default : 'background-color:#FF0000; padding-left:10rpx; padding-right:10rpx; color:#FFFFFF; font-size:22rpx',
  90. }
  91. // #endif
  92. // #ifndef APP-NVUE
  93. tipsStyle : {
  94. type : String,
  95. default : 'background-color:#FF0000; padding:0 10rpx; color:#FFFFFF; font-size:22rpx',
  96. }
  97. // #endif
  98. },
  99. data(){
  100. return {
  101. currentIndexIn : 0,
  102. itemsIn : [],
  103. random : 1,
  104. scrollLeft : 0,
  105. scrllTimer : null,
  106. autoLeft : ''
  107. }
  108. },
  109. created:function(){
  110. this.currentIndexIn = this.currentIndex;
  111. this.itemsIn = this.items;
  112. this.random = this.randomNum();
  113. },
  114. watch:{
  115. currentIndex : function(value){
  116. this.currentIndexIn = value;
  117. },
  118. currentIndexIn : function(val){
  119. if(this.scrllTimer != null){clearTimeout(this.scrllTimer);}
  120. this.scrllTimer = setTimeout(()=>{this.setLeft();}, 200);
  121. },
  122. items : function(value){ this.itemsIn = value; }
  123. },
  124. methods:{
  125. change : function (e){
  126. this.currentIndexIn = e.currentTarget.dataset.index;
  127. this.$emit('change', Number(e.currentTarget.dataset.index))
  128. },
  129. randomNum : function () {
  130. return parseInt(Math.random() * 1000);
  131. },
  132. setLeft : function () {
  133. if(this.size < 1){
  134. this.autoLeft = 'tab-'+this.currentIndexIn+''+this.random;
  135. return ;
  136. }
  137. var itemWidth = Number(this.margin) + Number(this.size);
  138. var left = (Number(this.currentIndexIn) + 1) * itemWidth - Number(this.width) / 2 - itemWidth / 2;
  139. var maxLeft = Number(this.itemsIn.length) * itemWidth - this.width;
  140. maxLeft = uni.upx2px(maxLeft - 30);
  141. left = uni.upx2px(left);
  142. if(left > maxLeft){left = maxLeft;}
  143. if(left < 0){left = 0;}
  144. this.scrollLeft = left;
  145. }
  146. }
  147. }
  148. </script>
  149. <style scoped>
  150. .nav-active-line{margin-top:6rpx;}
  151. .gui-nav-center{justify-content:center; text-align:center;}
  152. /* #ifndef APP-NVUE */
  153. @keyframes gui-nav-scale{0%{transform: scale(0.1);} 100%{transform: scale(1);}}
  154. .gui-nav-scale{animation:gui-nav-scale 350ms forwards;}
  155. /* #endif */
  156. .gui-nav-tips{text-align:center; line-height:30rpx; height:30rpx; border-radius:50rpx; margin-top:5rpx; margin-left:8rpx;}
  157. </style>