scroll-choose.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="scroll-choose-all">
  3. <view class="middleLine"></view>
  4. <scroll-view class="scroll-choose" scroll-x="true" upper-threshold="5" lower-threshold="5"
  5. :scroll-left="scrollLeftInit" show-scrollbar="false" @scroll="scroll" @scrolltoupper="upper"
  6. @scrolltolower="lower" scroll-with-animation="true">
  7. <view class="scroll-con" :style="{width: scrollWid}">
  8. <view class="topLine">
  9. <view class="allLine" :style="{'marginRight': maginL + 'px'}" :class="item.type" v-for="(item,index) in scrollList" :key="index"></view>
  10. </view>
  11. <view class="bottomNum" :style="{'paddingLeft': allNumLeft}">
  12. <text class="allNum" :style="{width: (maginL + 2) * 10 + 'px',left: -((maginL + 2) * 5) + 'px'}" v-for="(item,index) in scrollNumList" :key="index">
  13. {{item}}
  14. </text>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'ScrollChoose',
  23. props: {
  24. //起始值和终止值差距不要过大,否则会影响页面性能(暂不支持设置小数)
  25. /**
  26. * 初始值(注意:初始值应在起始值和终止值之间)
  27. */
  28. scrollLeft: {
  29. type: Number,
  30. default: 0
  31. },
  32. /**
  33. * 滚动区域起始值(注意:起始值不能大于终止值)
  34. */
  35. scrollStart: {
  36. type: Number,
  37. default: 0
  38. },
  39. /**
  40. * 滚动区域终止值
  41. */
  42. scrollEnd: {
  43. type: Number,
  44. default: 100
  45. },
  46. /**
  47. * 线间距
  48. */
  49. maginL: {
  50. type: Number,
  51. default: 5
  52. },
  53. },
  54. data() {
  55. return {
  56. scrollList:[],
  57. scrollNumList:[],
  58. scrollWid: '100vw',
  59. scrollLeftInit: 0,
  60. allNumLeft: ''
  61. }
  62. },
  63. mounted() {
  64. this.init();
  65. },
  66. computed:{
  67. },
  68. methods: {
  69. init(){
  70. for(let i = this.scrollStart; i < (this.scrollEnd + 1); i++){
  71. let _line = {};
  72. if(i%5 == 0){
  73. if(i%10 == 0){
  74. this.scrollNumList.push(i);
  75. _line.type = 'LLine'
  76. }else{
  77. _line.type = 'MLine'
  78. }
  79. }else{
  80. _line.type = 'SLine'
  81. }
  82. this.scrollList.push(_line);
  83. }
  84. this.scrollWid = uni.upx2px(750) + (this.scrollEnd - this.scrollStart) * (this.maginL + 2) + 'px';
  85. if(this.scrollStart % 10 != 0){
  86. if(this.scrollStart > 0){
  87. this.allNumLeft = (10 - this.scrollStart % 10) * (this.maginL + 2) + uni.upx2px(372) + 'px';
  88. }else{
  89. this.allNumLeft = Math.abs(this.scrollStart % 10) * (this.maginL + 2) + uni.upx2px(372) + 'px';
  90. }
  91. }
  92. setTimeout(()=>{
  93. this.setNowLeft();
  94. },100)
  95. },
  96. setNowLeft(){
  97. this.scrollLeftInit = (this.scrollLeft - this.scrollStart) * (this.maginL + 2);
  98. },
  99. upper: function(e) {
  100. setTimeout(()=>{
  101. this.$emit('scroll',this.scrollStart);
  102. },50)
  103. },
  104. lower: function(e) {
  105. setTimeout(()=>{
  106. this.$emit('scroll',this.scrollEnd);
  107. },50)
  108. },
  109. scroll: function(e) {
  110. this.$emit('scroll',Math.round(e.detail.scrollLeft/(this.maginL + 2)) + this.scrollStart);
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss">
  116. @charset "UTF-8";
  117. .scroll-choose-all{
  118. width: 750rpx;
  119. height: 70px;
  120. background: #f8f8f8;
  121. margin: 10px 0;
  122. border-bottom: 1px solid #ccc;
  123. border-top: 1px solid #ccc;
  124. position: relative;
  125. }
  126. .middleLine{
  127. position: absolute;
  128. width: 4px;
  129. height: 40px;
  130. background: #83DC42;
  131. left: 375rpx;
  132. margin-left: -2px;
  133. z-index: 1;
  134. }
  135. .scroll-choose{
  136. width: 750rpx;
  137. height: 70px;
  138. .scroll-con{
  139. height: 70px;
  140. overflow: hidden;
  141. .topLine{
  142. height: 30px;
  143. padding: 0 372rpx;
  144. }
  145. .bottomNum{
  146. height: 30px;
  147. padding: 0 0 0 372rpx;
  148. width: 100%;
  149. // display: flex;
  150. // flex-wrap: nowrap;
  151. .allNum{
  152. display: inline-block;
  153. position: relative;
  154. // width: 70px;
  155. // left: -35px;
  156. text-align: center;
  157. }
  158. }
  159. .allLine{
  160. display: inline-block;
  161. // margin-right: 5px;
  162. width: 2px;
  163. background: #999;
  164. position: relative;
  165. }
  166. .allLine:last-child{
  167. margin-right: 0px !important;
  168. }
  169. .LLine{
  170. height: 30px;
  171. }
  172. .MLine{
  173. height: 20px;
  174. top: -10px;
  175. }
  176. .SLine{
  177. height: 15px;
  178. top: -15px;
  179. }
  180. }
  181. }
  182. </style>