当前位置:   article > 正文

vue2 新闻消息向上无缝滚动_vue2实现列表内容向上循环滚动效果,而且是每次滚动一条内容

vue2实现列表内容向上循环滚动效果,而且是每次滚动一条内容

这是很久以前项目中用到的功能,目前要达到的效果是新闻逐条向上滚动,没有使用第三方插件,vue2版本的,vue3可以自行改造,适合新闻列表模块。后续也会出其他功能块,每个功能块都很简洁,复制粘贴就能用到项目中,节约时间

代码如下:

  1. <template>
  2. <div class="news">
  3. <div :class="{ anim: animate }" @mouseenter="stop()" @mouseleave="up()">
  4. <div
  5. @click="handleClick(item)"
  6. class="news_name"
  7. v-for="item in newsList"
  8. :key="item.id"
  9. >
  10. {{ item.name }}
  11. </div>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. timer: null,
  20. animate: false,
  21. newsList: [
  22. { id: 1, name: "华为11111" },
  23. { id: 2, name: "Redmi K30 5G" },
  24. { id: 3, name: "小米CC9 Pro" },
  25. { id: 4, name: "Redmi 8" },
  26. { id: 5, name: "Redmi 8A" },
  27. { id: 6, name: "Redmi Note8 Pro" },
  28. { id: 7, name: "Redmi Note8" },
  29. { id: 8, name: "Redmi Note8" },
  30. ],
  31. };
  32. },
  33. mounted() {
  34. this.scrollUp(); // 开启滚动效果
  35. },
  36. methods: {
  37. // 查看详情
  38. handleClick(item) {
  39. console.log(item);
  40. },
  41. // 滚动
  42. scrollUp() {
  43. this.timer = setInterval(() => {
  44. this.animate = true; // 向上滚动的时候需要添加动画
  45. setTimeout(() => {
  46. this.newsList.push(this.newsList[0]); // 将数组的第一个元素添加到数组最后一个
  47. this.newsList.shift(); // 删除数组的第一个元素
  48. this.animate = false;
  49. }, 500);
  50. }, 4000);
  51. },
  52. //鼠标移上去停止
  53. stop() {
  54. clearInterval(this.timer);
  55. },
  56. //鼠标离开继续
  57. up() {
  58. this.scrollUp();
  59. },
  60. },
  61. beforeDestroy() {
  62. this.stop();
  63. },
  64. };
  65. </script>
  66. <style scoped>
  67. .news {
  68. width: 100%;
  69. height: 90px;
  70. background-color: #fff;
  71. margin-top: 50px;
  72. overflow: hidden;
  73. }
  74. .news_name {
  75. line-height: 30px;
  76. text-overflow: ellipsis;
  77. overflow: hidden;
  78. white-space: nowrap;
  79. transition: top 0.5s;
  80. }
  81. .anim {
  82. transition: all 0.5s;
  83. margin-top: -30px;
  84. }
  85. </style>

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

闽ICP备14008679号