mao-scroll.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <view>
  3. <view class="maoScroll-main" :style="'height:'+(lineHeight*showLine)+'rpx;'">
  4. <view :style="'margin-top:-'+marginTop+'rpx;'">
  5. <view v-for="(item,index) in showdata" :key="'maoScroll'+index" :style="'height:'+lineHeight+'rpx;'">
  6. <slot :line="item" />
  7. </view>
  8. </view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'maoScroll',
  15. data() {
  16. return {
  17. showdata: [],
  18. marginTop: 0,
  19. showLine: 0,
  20. }
  21. },
  22. props:{
  23. data: {
  24. type: Array,
  25. default: []
  26. },
  27. showNum: {
  28. type: Number,
  29. default: 3,
  30. },
  31. lineHeight: {
  32. type: Number,
  33. default: 60,
  34. },
  35. animationScroll: {
  36. type: Number,
  37. default: 500,
  38. },
  39. animation: {
  40. type: Number,
  41. default: 2000,
  42. }
  43. },
  44. methods: {
  45. init: function(){
  46. this.showLine = this.showNum < this.data.length ? this.showNum : this.data.length;
  47. for(let i = 0; i < this.data.length; i++){
  48. this.showdata.push(this.data[i]);
  49. }
  50. for(let i = 0; i < this.showLine; i++){
  51. this.showdata.push(this.data[i]);
  52. }
  53. setInterval(this.animationFunc, this.animation);
  54. },
  55. animationFunc: function(){
  56. if(this.marginTop >= this.data.length*this.lineHeight){
  57. this.marginTop = 0;
  58. }
  59. let stepTime = this.animationScroll/this.lineHeight;
  60. var step = 0;
  61. let self = this;
  62. var index = setInterval(function(){
  63. self.marginTop = self.marginTop + 1;
  64. step++;
  65. if (step >= self.lineHeight) {
  66. clearInterval(index);
  67. }
  68. }, stepTime);
  69. }
  70. },
  71. watch: {
  72. data(outdata, newdata) {
  73. this.init();
  74. }
  75. }
  76. }
  77. </script>
  78. <style>
  79. .maoScroll-main{width: 100%;overflow: hidden;}
  80. </style>