当前位置:首页 > 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实现垂直滚动

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

<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

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

安装依赖:

npm install vue-seamless-scroll

使用示例:

vue实现公告滚动

<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实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…