当前位置:   article > 正文

在Vue中,用于实现图片列表的自动滚动效果_vue实现图片滚动

vue实现图片滚动

div添加2个事件,用于暂停和继续; 

<divref="imgListBox"@mouseenter="handleImgListMouseEnter"
@mouseleave="handleImgListMouseLeave"
><img/></div>
  1. startAutoScroll(length) {
  2. this.handleImgListMouseEnter();
  3. let num = 0;
  4. let scrollDistance = 0;
  5. this.imgListTime = setInterval(() => {
  6. num++;
  7. if (num >= length - 4) num = 0;
  8. scrollDistance = 350 * num;
  9. this.animateScroll(scrollDistance);
  10. }, 2000);
  11. },
  12. handleImgListMouseEnter() {
  13. if (this.imgListTime) clearInterval(this.imgListTime);
  14. },
  15. handleImgListMouseLeave() {
  16. this.startAutoScroll(this.imgList.length);
  17. },
  18. animateScroll(scrollLeft) {
  19. let start = null;
  20. let that=this
  21. function step(timestamp) {
  22. if (!start) start = timestamp;
  23. const progress = timestamp - start;
  24. const scrollDistance = 350;
  25. const easeInOutQuad = t => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
  26. that.$refs.imgListBox.scrollLeft = (scrollLeft-350) + scrollDistance * easeInOutQuad(Math.min(progress / 1000, 1));
  27. if (progress < 1000) {
  28. requestAnimationFrame(step);
  29. }
  30. }
  31. requestAnimationFrame(step);
  32. },

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/251061?site
推荐阅读
相关标签
  

闽ICP备14008679号