当前位置:首页 > VUE

vue实现新闻滚动

2026-02-20 07:46:41VUE

Vue实现新闻滚动效果

新闻滚动是常见的网页动态效果,可通过Vue结合CSS动画或第三方库实现。以下是几种常见方法:

方法一:使用CSS动画和v-for

定义数据数组并通过CSS实现无限滚动动画:

<template>
  <div class="news-container">
    <div class="news-list" :style="{ transform: `translateY(${scrollPosition}px)` }">
      <div v-for="(item, index) in newsData" :key="index" class="news-item">
        {{ item.title }}
      </div>
      <!-- 复制首条数据实现无缝衔接 -->
      <div v-for="(item, index) in newsData" :key="'copy'+index" class="news-item">
        {{ item.title }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsData: [
        { title: "新闻标题1" },
        { title: "新闻标题2" },
        { title: "新闻标题3" }
      ],
      scrollPosition: 0
    }
  },
  mounted() {
    setInterval(() => {
      this.scrollPosition -= 1;
      if (-this.scrollPosition >= document.querySelector('.news-item').offsetHeight * this.newsData.length) {
        this.scrollPosition = 0;
      }
    }, 50);
  }
}
</script>

<style>
.news-container {
  height: 200px;
  overflow: hidden;
  position: relative;
}
.news-list {
  position: absolute;
  transition: transform 0.5s linear;
}
.news-item {
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #eee;
}
</style>

方法二:使用vue-seamless-scroll插件

vue实现新闻滚动

安装插件后快速实现无缝滚动:

npm install vue-seamless-scroll

组件实现:

vue实现新闻滚动

<template>
  <vue-seamless-scroll 
    :data="newsData" 
    :class-option="options" 
    class="seamless-wrap"
  >
    <ul>
      <li v-for="(item, index) in newsData" :key="index">
        {{ item.title }}
      </li>
    </ul>
  </vue-seamless-scroll>
</template>

<script>
import vueSeamlessScroll from 'vue-seamless-scroll'
export default {
  components: { vueSeamlessScroll },
  data() {
    return {
      newsData: [
        { title: "动态新闻1" },
        { title: "动态新闻2" },
        { title: "动态新闻3" }
      ]
    }
  },
  computed: {
    options() {
      return {
        step: 0.5, // 滚动步长
        limitMoveNum: 3, // 开始滚动的数据量
        hoverStop: true, // 悬停暂停
        direction: 1, // 0向下 1向上 2向左 3向右
        openWatch: true // 开启数据实时监控
      }
    }
  }
}
</script>

<style>
.seamless-wrap {
  height: 200px;
  overflow: hidden;
}
</style>

方法三:Marquee标签替代方案(兼容方案)

虽然HTML5已废弃marquee标签,但仍可通过CSS模拟:

<template>
  <div class="marquee">
    <div class="track" :style="{ transform: `translateX(${position}px)` }">
      <span v-for="(item, index) in newsData" :key="index">
        {{ item.title }} &nbsp;&nbsp;&nbsp;
      </span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsData: [...],
      position: 0,
      timer: null
    }
  },
  mounted() {
    this.startScroll()
  },
  methods: {
    startScroll() {
      this.timer = setInterval(() => {
        this.position -= 1
        if (-this.position > this.$el.querySelector('.track').scrollWidth / 2) {
          this.position = 0
        }
      }, 20)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style>
.marquee {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}
.track {
  display: inline-block;
  transition: transform 0.2s linear;
}
</style>

注意事项

  • 移动端需考虑touch事件处理
  • 大量数据时建议使用虚拟滚动技术
  • 暂停逻辑可绑定mouseenter/mouseleave事件
  • 动态更新数据时需要重置滚动位置

标签: 新闻vue
分享给朋友:

相关文章

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

vue 实现文字滚动

vue 实现文字滚动

Vue 实现文字滚动效果 使用CSS动画实现 通过CSS的@keyframes和transform属性实现文字滚动效果: <template> <div class="scro…

vue前端实现权限

vue前端实现权限

Vue 前端权限实现方案 路由权限控制 通过 Vue Router 的 beforeEach 钩子实现路由拦截,结合用户角色和权限列表进行校验。在路由配置中添加 meta 字段定义所需权限: con…

vue实现无限循环

vue实现无限循环

Vue实现无限循环的方法 在Vue中实现无限循环可以通过多种方式完成,以下是几种常见的方法: 使用CSS动画 通过CSS的animation属性结合@keyframes可以实现无限循环动画效果。…