vue2 新闻消息向上无缝滚动

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

代码如下:

复制代码
<template>
  <div class="news">
    <div :class="{ anim: animate }" @mouseenter="stop()" @mouseleave="up()">
      <div
        @click="handleClick(item)"
        class="news_name"
        v-for="item in newsList"
        :key="item.id"
      >
        {{ item.name }}
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      timer: null,
      animate: false,
      newsList: [
        { id: 1, name: "华为11111" },
        { id: 2, name: "Redmi K30 5G" },
        { id: 3, name: "小米CC9 Pro" },
        { id: 4, name: "Redmi 8" },
        { id: 5, name: "Redmi 8A" },
        { id: 6, name: "Redmi Note8 Pro" },
        { id: 7, name: "Redmi Note8" },
        { id: 8, name: "Redmi Note8" },
      ],
    };
  },
  mounted() {
    this.scrollUp(); // 开启滚动效果
  },
  methods: {
    // 查看详情
    handleClick(item) {
      console.log(item);
    },
    // 滚动
    scrollUp() {
      this.timer = setInterval(() => {
        this.animate = true; // 向上滚动的时候需要添加动画
        setTimeout(() => {
          this.newsList.push(this.newsList[0]); // 将数组的第一个元素添加到数组最后一个
          this.newsList.shift(); // 删除数组的第一个元素
          this.animate = false;
        }, 500);
      }, 4000);
    },
    //鼠标移上去停止
    stop() {
      clearInterval(this.timer);
    },
    //鼠标离开继续
    up() {
      this.scrollUp();
    },
  },
  beforeDestroy() {
    this.stop();
  },
};
</script>
<style scoped>
.news {
  width: 100%;
  height: 90px;
  background-color: #fff;
  margin-top: 50px;
  overflow: hidden;
}
.news_name {
  line-height: 30px;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  transition: top 0.5s;
}
.anim {
  transition: all 0.5s;
  margin-top: -30px;
}
</style>
相关推荐
工边页字5 分钟前
LLM 系统设计核心:为什么必须压缩上下文?有哪些工程策略
前端·人工智能·后端
嚣张丶小麦兜13 分钟前
react的理解
前端·react.js·前端框架
重庆穿山甲17 分钟前
身份证照片自动裁剪(OpenCV 四边形检测 + 透视矫正)
前端·后端
跟着珅聪学java17 分钟前
Electron + Vue 现代化“新品展示“和“快捷下单“菜单
开发语言·前端·javascript
何贤18 分钟前
用 Three.js 写了一个《我的世界》,结果老外差点给我众筹做游戏?
前端·开源·three.js
C_心欲无痕30 分钟前
使用 XLSX.js 导出 Excel 文件
开发语言·javascript·excel
踩着两条虫35 分钟前
AI 驱动的 Vue3 应用开发平台 深入探究(七):双向代码转换之 Vue源码到DSL解析
前端·vue.js·ai编程
专业流量卡36 分钟前
用ai去看源码
前端·react.js
踩着两条虫38 分钟前
AI 驱动的 Vue3 应用开发平台 深入探究(六):双向代码转换之DSL到Vue代码生成
前端·vue.js·ai编程
前端老兵AI38 分钟前
React vs Vue 2026年怎么选?9年前端的真实建议
vue.js·react.js