当前位置:首页 > VUE

vue实现公告滚动

2026-01-17 07:24:25VUE

实现公告滚动的几种方法

使用CSS动画实现横向滚动

通过CSS的animation@keyframes实现无限循环滚动效果。适用于简单的文字公告。

<template>
  <div class="notice-container">
    <div class="notice-content">{{ noticeText }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      noticeText: '这是一条重要公告,请仔细阅读。'
    }
  }
}
</script>

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

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

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

使用Vue的transition-group实现垂直滚动

适合多条公告的上下轮播效果。

vue实现公告滚动

<template>
  <div class="notice-box">
    <transition-group name="scroll" tag="ul">
      <li v-for="(item, index) in notices" :key="index">{{ item }}</li>
    </transition-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      notices: ['公告1:系统维护通知', '公告2:新功能上线', '公告3:用户协议更新'],
      currentIndex: 0
    }
  },
  mounted() {
    setInterval(this.scrollNotice, 2000);
  },
  methods: {
    scrollNotice() {
      this.currentIndex = (this.currentIndex + 1) % this.notices.length;
      this.notices.push(this.notices.shift());
    }
  }
}
</script>

<style>
.notice-box {
  height: 30px;
  overflow: hidden;
}

.scroll-move {
  transition: transform 0.5s ease;
}
</style>

使用第三方库vue-seamless-scroll

专业级滚动组件,支持丰富配置。

vue实现公告滚动

安装依赖:

npm install vue-seamless-scroll

使用示例:

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

<script>
import vueSeamlessScroll from 'vue-seamless-scroll'
export default {
  components: { vueSeamlessScroll },
  data() {
    return {
      notices: ['公告A', '公告B', '公告C']
    }
  },
  computed: {
    option() {
      return {
        direction: 1, // 0向下 1向上 2向左 3向右
        step: 0.5, // 步长
        limitMoveNum: 2, // 开始滚动的数据量
        hoverStop: true // 悬停暂停
      }
    }
  }
}
</script>

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

注意事项

  1. 移动端适配需要考虑touch事件和响应式布局
  2. 大量数据滚动时注意性能优化
  3. 动态更新公告内容时需处理好过渡动画
  4. 考虑添加暂停/继续控制功能提升用户体验

标签: 公告vue
分享给朋友:

相关文章

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ {…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:…