tm-scroll.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="tm-scroll relative" :style="{width:`${width}rpx`}">
  3. <scroll-view scroll-anchoring @scroll="onscrollEvent" :style="{width:`${width}rpx`}" scroll-x>
  4. <view class="fulled " style="white-space: nowrap;">
  5. <slot></slot>
  6. </view>
  7. </scroll-view>
  8. <view v-if="showDot" style="height: 24rpx;"></view>
  9. <view v-if="showDot" class="absolute tm-scroll-bar fulled flex-center">
  10. <view class="tm-scroll-bar-active grey-lighten-2 round-24 overflow"
  11. :class="[$tm.vx.state().tmVuetify.black?'bk':'']">
  12. <view :style="{marginLeft:`${left}px`}" class="tm-scroll-bar-active-bar round-24"
  13. :class="[color_tmeme]"></view>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. /**
  20. * 横向滚动
  21. * @property {Number} width = [] 默认650,单位rpx,宽度
  22. * @property {String} color = [] 默认primary,任意主题色名称
  23. * @property {Boolean|String} show-dot = [] 默认true,是否显示指示点。
  24. */
  25. export default {
  26. name: "tm-scroll",
  27. props: {
  28. width: {
  29. type: Number | String,
  30. default: 650,
  31. },
  32. color: {
  33. type: String,
  34. default: 'primary',
  35. },
  36. // 跟随主题色的改变而改变。
  37. fllowTheme: {
  38. type: Boolean | String,
  39. default: true
  40. },
  41. //是否显示指示点。
  42. showDot: {
  43. type: Boolean | String,
  44. default: true
  45. }
  46. },
  47. computed: {
  48. color_tmeme: function() {
  49. if (this.$tm.vx.state().tmVuetify.color !== null && this.$tm.vx.state().tmVuetify.color && this
  50. .fllowTheme) {
  51. return this.$tm.vx.state().tmVuetify.color;
  52. }
  53. return this.color;
  54. },
  55. },
  56. data() {
  57. return {
  58. left: 0,
  59. totlal_w: 0,
  60. totlal_L: 0,
  61. }
  62. },
  63. methods: {
  64. onscrollEvent(e) {
  65. // clearTimeout(this.timeid)
  66. let t = this;
  67. let dbw = uni.upx2px(100);
  68. let dbw_active_w = dbw * 0.4;
  69. let aw = uni.upx2px(t.width);
  70. let totlal_w = e.detail.scrollWidth - aw;
  71. let totlal_L = e.detail.scrollLeft;
  72. if (totlal_L <= 0) {
  73. totlal_L = 0;
  74. }
  75. if (totlal_L >= totlal_w) {
  76. totlal_L = totlal_w;
  77. }
  78. let bl = totlal_L / totlal_w;
  79. let ml = (dbw - dbw_active_w) * bl;
  80. t.left = ml;
  81. }
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .tm-scroll {
  87. .tm-scroll-bar {
  88. bottom: 0;
  89. left: 0;
  90. .tm-scroll-bar-active {
  91. height: 10rpx;
  92. width: 100rpx;
  93. .tm-scroll-bar-active-bar {
  94. height: 10rpx;
  95. width: 40%;
  96. }
  97. }
  98. }
  99. }
  100. </style>