当前位置:首页 > VUE

vue实现公告

2026-01-07 18:17:15VUE

Vue 实现公告功能的方法

公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式:

使用 marquee 标签实现滚动公告

<template>
  <marquee behavior="scroll" direction="left">
    {{ announcement }}
  </marquee>
</template>

<script>
export default {
  data() {
    return {
      announcement: '这是一条重要公告内容...'
    }
  }
}
</script>

使用 CSS 动画实现平滑滚动

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

<script>
export default {
  data() {
    return {
      announcement: '系统将于今晚23:00进行维护升级,请提前保存工作...'
    }
  }
}
</script>

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

.announcement-content {
  display: inline-block;
  animation: scroll 15s linear infinite;
}

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

实现多条公告轮播

<template>
  <div class="announcement-box">
    <transition name="fade" mode="out-in">
      <div :key="currentIndex" class="announcement-item">
        {{ announcements[currentIndex] }}
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      announcements: [
        '公告1:系统升级通知',
        '公告2:新功能上线',
        '公告3:维护时间调整'
      ],
      currentIndex: 0,
      interval: null
    }
  },
  mounted() {
    this.startRotation()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    startRotation() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.announcements.length
      }, 3000)
    }
  }
}
</script>

<style>
.announcement-box {
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  position: relative;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

结合后端数据的公告实现

<template>
  <div v-if="announcements.length > 0" class="announcement-list">
    <div v-for="(item, index) in announcements" :key="index" class="announcement-item">
      <span class="announcement-time">{{ item.time }}</span>
      <span class="announcement-text">{{ item.content }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      announcements: []
    }
  },
  async created() {
    try {
      const response = await this.$http.get('/api/announcements')
      this.announcements = response.data
    } catch (error) {
      console.error('获取公告失败:', error)
    }
  }
}
</script>

<style>
.announcement-list {
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 10px;
}

.announcement-item {
  padding: 8px 0;
  border-bottom: 1px dashed #ddd;
}

.announcement-item:last-child {
  border-bottom: none;
}

.announcement-time {
  color: #999;
  margin-right: 15px;
}
</style>

使用第三方库实现公告功能

可以安装 vue-notification 等插件快速实现:

vue实现公告

npm install vue-notification
<template>
  <button @click="showNotification">显示公告</button>
</template>

<script>
import Vue from 'vue'
import Notifications from 'vue-notification'

Vue.use(Notifications)

export default {
  methods: {
    showNotification() {
      this.$notify({
        title: '重要公告',
        text: '系统将于明日凌晨进行维护',
        type: 'warn',
        duration: 5000
      })
    }
  }
}
</script>

以上方法可根据实际需求选择或组合使用,实现不同风格的公告展示效果。

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

相关文章

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…

vue实现卡片

vue实现卡片

Vue 实现卡片组件的方法 使用 Vue 实现卡片组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 在 Vue 单文件组件中,可以通过模板和样式直接实现卡片效果…

vue实现滚动显示

vue实现滚动显示

Vue 实现滚动显示的几种方法 在 Vue 中实现滚动显示可以通过多种方式实现,以下是几种常见的方法: 使用 CSS 动画 通过 CSS 的 animation 属性结合 @keyframes 实现…

vue实现选择置顶

vue实现选择置顶

Vue 实现选择置顶功能 在 Vue 中实现选择置顶功能,可以通过操作数据数组或结合 CSS 样式来实现。以下是几种常见的实现方法: 数组排序法 通过操作数据数组,将被选中的项移动到数组首位: /…