userNameSwiper.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view class="user-swiper">
  3. <view class="box" id="box" :style="{marginLeft: `${distance}px`}">
  4. <view class="user-list" v-for="(item,index) in combinationUserList" :key="index">
  5. <image v-show="init" class="header-img" :src="item.avatar" mode=""></image>
  6. <text v-show="init" class="user-name">***** 拼团成功</text>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. init: true,
  16. distance: 0,
  17. boxWidth: 0,
  18. timer: null // 定时器名称
  19. }
  20. },
  21. props: {
  22. combinationUserList: {
  23. type: Array,
  24. default: () => {
  25. []
  26. }
  27. }
  28. },
  29. watch: {
  30. combinationUserList() {
  31. setTimeout(e => {
  32. const query = uni.createSelectorQuery().in(this);
  33. query.select('#box').boundingClientRect(data => {
  34. this.width = data.width
  35. this.move()
  36. }).exec();
  37. }, 1000)
  38. }
  39. },
  40. activated() {
  41. this.move()
  42. },
  43. methods: {
  44. move() {
  45. // 设置位移
  46. this.timer = setInterval(() => {
  47. console.log(this.distance)
  48. this.$set(this, 'distance', this.distance - this.width / this.combinationUserList.length)
  49. // 如果位移超过宽度,则回到起点
  50. if (this.distance === -this.width) {
  51. this.init = false
  52. this.distance = 0
  53. setTimeout(e => {
  54. this.init = true
  55. }, 800)
  56. }
  57. }, 2500)
  58. }
  59. },
  60. destroyed() {
  61. clearInterval(this.timer);
  62. this.timer = null;
  63. },
  64. deactivated() {
  65. clearInterval(this.timer);
  66. this.timer = null;
  67. }
  68. }
  69. </script>
  70. <style lang="scss" scoped>
  71. .user-swiper {
  72. width: 260rpx;
  73. overflow: hidden;
  74. border-radius: 25rpx;
  75. background: rgba(#000000, 0.3);
  76. height: 50rpx;
  77. display: flex;
  78. align-items: center;
  79. .box {
  80. width: max-content;
  81. display: flex;
  82. align-items: center;
  83. flex-wrap: nowrap;
  84. transition: all .8s;
  85. .user-list {
  86. display: flex;
  87. align-items: center;
  88. justify-content: center;
  89. width: 260rpx;
  90. border-radius: 25rpx;
  91. padding: 8rpx 18rpx;
  92. color: #fff;
  93. font-size: 22rpx;
  94. .header-img {
  95. width: 34rpx;
  96. height: 34rpx;
  97. border-radius: 50%;
  98. border: 1px solid #FFFFFF;
  99. margin-right: 6rpx;
  100. }
  101. }
  102. }
  103. }
  104. </style>