Scroll.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div :style="{ width: width + 'px', height: height + 'px' }" class="container">
  3. <slot/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. props: {
  9. width: {
  10. type: Number,
  11. required: true
  12. },
  13. height: {
  14. type: Number,
  15. required: true
  16. },
  17. duration: {
  18. type: Number,
  19. required: true
  20. },
  21. span:{
  22. type: Number,
  23. required: true
  24. }
  25. },
  26. data() {
  27. return {
  28. timer: null,
  29. el: null,
  30. x: 0,
  31. y: 0,
  32. scrollHeight: 0
  33. }
  34. },
  35. mounted() {
  36. this.el = document.querySelector(".container")
  37. if (!this.el) return;
  38. this.scrollHeight = this.el.scrollHeight + 17
  39. this.timer = setInterval(() => {
  40. this.updatePos()
  41. // console.log(this.x, this.y);
  42. this.el.scrollTo(this.x, this.y)
  43. }, this.duration)
  44. },
  45. beforeDestroy() {
  46. clearInterval(this.timer)
  47. },
  48. methods: {
  49. updatePos() {
  50. this.y += this.span
  51. if(this.y > this.scrollHeight)
  52. this.y = 0
  53. }
  54. }
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. /* 隐藏垂直滚动条 */
  59. .container::-webkit-scrollbar {
  60. width: 0;
  61. }
  62. /* 隐藏水平滚动条 */
  63. .container::-webkit-scrollbar {
  64. height: 0;
  65. }
  66. .container {
  67. overflow-y: auto;
  68. scroll-behavior: smooth;
  69. }
  70. </style>