当前位置:首页 > 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 动态数据绑定实现无缝滚动

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

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:

vue实现新闻滚动

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 或使用专门的垂直滚动组件。

<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实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Card…

vue实现全文检索

vue实现全文检索

Vue 实现全文检索的方法 客户端全文检索(适用于小型数据集) 使用 flexsearch 或 lunr.js 等轻量级库实现前端全文检索: import FlexSearch from 'fle…

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model…