当前位置:首页 > VUE

vue实现新闻滚动

2026-01-19 15:29:18VUE

Vue 实现新闻滚动效果

新闻滚动效果通常用于展示动态更新的新闻标题或内容,可以通过多种方式实现。以下是几种常见的实现方法:

使用 CSS 动画实现基础滚动

通过 CSS 的 animation@keyframes 实现简单的横向或纵向滚动效果。

<template>
  <div class="news-scroll-container">
    <div class="news-scroll-content">
      {{ newsContent }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsContent: "这里是滚动新闻内容,可以是动态更新的数据..."
    };
  }
};
</script>

<style>
.news-scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.news-scroll-content {
  display: inline-block;
  animation: scroll 10s linear infinite;
}

@keyframes scroll {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}
</style>

使用 Vue 动态数据绑定实现无缝滚动

通过动态更新数据实现无缝滚动效果,适用于需要从后端获取新闻数据的场景。

<template>
  <div class="news-scroll-container">
    <div 
      class="news-scroll-content" 
      :style="{ transform: `translateX(${scrollPosition}px)` }"
    >
      <span v-for="(item, index) in newsList" :key="index">
        {{ item.title }} &nbsp;&nbsp;&nbsp;
      </span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsList: [
        { title: "新闻标题1" },
        { title: "新闻标题2" },
        { title: "新闻标题3" }
      ],
      scrollPosition: 0,
      scrollInterval: null
    };
  },
  mounted() {
    this.startScrolling();
  },
  beforeDestroy() {
    this.stopScrolling();
  },
  methods: {
    startScrolling() {
      this.scrollInterval = setInterval(() => {
        this.scrollPosition -= 1;
        if (this.scrollPosition < -300) {
          this.scrollPosition = 0;
        }
      }, 30);
    },
    stopScrolling() {
      clearInterval(this.scrollInterval);
    }
  }
};
</script>

<style>
.news-scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}

.news-scroll-content {
  display: inline-block;
}
</style>

使用第三方库实现复杂滚动效果

对于更复杂的滚动需求,可以使用第三方库如 vue-marqueevue-awesome-marquee

安装 vue-marquee:

npm install vue-marquee-text-component

使用示例:

<template>
  <marquee-text :duration="20" :repeat="5">
    <span v-for="(item, index) in newsList" :key="index">
      {{ item.title }} &nbsp;&nbsp;&nbsp;
    </span>
  </marquee-text>
</template>

<script>
import MarqueeText from 'vue-marquee-text-component';

export default {
  components: {
    MarqueeText
  },
  data() {
    return {
      newsList: [
        { title: "新闻标题1" },
        { title: "新闻标题2" },
        { title: "新闻标题3" }
      ]
    };
  }
};
</script>

垂直滚动实现方法

如果需要垂直滚动效果,可以调整 CSS 或使用专门的垂直滚动组件。

vue实现新闻滚动

<template>
  <div class="vertical-scroll-container">
    <div class="vertical-scroll-content">
      <div v-for="(item, index) in newsList" :key="index" class="news-item">
        {{ item.title }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsList: [
        { title: "新闻标题1" },
        { title: "新闻标题2" },
        { title: "新闻标题3" }
      ]
    };
  }
};
</script>

<style>
.vertical-scroll-container {
  height: 100px;
  overflow: hidden;
}

.vertical-scroll-content {
  animation: vertical-scroll 10s linear infinite;
}

@keyframes vertical-scroll {
  0% { transform: translateY(0); }
  100% { transform: translateY(-100%); }
}

.news-item {
  height: 30px;
  line-height: 30px;
}
</style>

以上方法可以根据实际需求进行调整,如滚动速度、方向、暂停交互等功能的添加。

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

相关文章

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供的…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现换页

vue实现换页

Vue实现页面切换的方法 在Vue中实现页面切换通常可以通过Vue Router来完成。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是几种常见的实现方式:…

vue实现接口轮询

vue实现接口轮询

实现接口轮询的基本方法 在Vue中实现接口轮询通常通过setInterval或setTimeout结合异步请求完成。以下是一个基础实现示例: data() { return { poll…

vue如何实现tap

vue如何实现tap

实现Tap事件的方法 在Vue中实现类似移动端的tap事件(轻触)可以通过以下几种方式完成。原生移动端浏览器通常通过touchstart和touchend事件组合模拟tap,而Vue中可以封装自定义指…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 动态数据绑定展示 通过 Vue 的响应式特性,实时展示数据变化。例如,表单输入与预览同步: <template> <div> <…