carousel.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view class="carousel_view" :style="{ height: layHeight + 'px' }">
  3. <view class="carousel_rongqi" :style="{ top:top+'px' }">
  4. <slot :data="showList" />
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. props:{
  11. // item的class类
  12. itemClass:{
  13. type: String,
  14. default: "item"
  15. },
  16. // 定时器执行时间
  17. itemTime:{
  18. type: Number,
  19. default: 10
  20. },
  21. // 每次定时器执行时位移数
  22. diffv:{
  23. type: Number,
  24. default: 1
  25. },
  26. // 列表数据
  27. list:{
  28. type: Array,
  29. default: ()=>[]
  30. },
  31. // 显示数据
  32. showNum:{
  33. type: Number,
  34. default: 5
  35. },
  36. // 停顿时间
  37. standstill:{
  38. type: Number,
  39. default: 0
  40. },
  41. },
  42. data() {
  43. return {
  44. layHeight: 0,
  45. itemHeight: 0,
  46. index: 0,
  47. top: 0
  48. };
  49. },
  50. computed:{
  51. actualShowNum(){
  52. return this.showNum + 1
  53. },
  54. showList(){
  55. var ayuan = []
  56. while (ayuan.length == 0 || ayuan.length%this.showNum) {
  57. ayuan.push(...this.list)
  58. }
  59. var list = ayuan.slice(this.index, this.actualShowNum)
  60. var len = this.actualShowNum - list.length
  61. if(len > 0){
  62. return [...list, ...ayuan.slice(0, len)]
  63. }else{
  64. return [...list]
  65. }
  66. }
  67. },
  68. mounted(){
  69. this.lunbo()
  70. },
  71. methods:{
  72. init(){
  73. if(this.timeId) {
  74. clearTimeout(this.timeId)
  75. }
  76. if(this.index == this.list.length - 1){
  77. this.index = 0
  78. }else{
  79. this.index ++
  80. }
  81. this.$nextTick(()=>{
  82. this.top = 0
  83. var layHeight = 0
  84. var elements = document.querySelectorAll(`.carousel_rongqi .${this.itemClass}`);
  85. elements.forEach((element,index) => {
  86. if(index == 0){
  87. this.itemHeight = element.clientHeight
  88. }
  89. if(index < elements.length - 1){
  90. layHeight += element.clientHeight
  91. }
  92. });
  93. this.layHeight = layHeight
  94. this.timeId = setTimeout(this.lunbo, (this.standstill + this.itemTime))
  95. })
  96. },
  97. lunbo(){
  98. if(this.timeId) {
  99. clearTimeout(this.timeId)
  100. }
  101. var chazhi = parseInt(Math.abs(this.top + this.itemHeight))
  102. if(chazhi==0){
  103. this.timeId = setTimeout(this.init, this.itemTime)
  104. }else if(chazhi < this.diffv){
  105. this.top = this.itemHeight * -1
  106. this.timeId = setTimeout(this.lunbo, this.itemTime)
  107. }else{
  108. this.top -= this.diffv
  109. this.timeId = setTimeout(this.lunbo, this.itemTime)
  110. }
  111. },
  112. }
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. .carousel_view{
  117. width: 100%;
  118. position: relative;
  119. overflow: hidden;
  120. }
  121. .carousel_rongqi{
  122. width: 100%;
  123. height: auto;
  124. position: absolute;
  125. left: 0;
  126. right: 0;
  127. }
  128. </style>