Vue 封裝公告滾動的方法
需求
系統(tǒng)中需要有一個公告展示,且這個公告位于頁面上方,每個頁面都要看到
分析
1. 創(chuàng)建公告組件Notice.vue
第一種
在你的項目的合適組件目錄下(比如components目錄)創(chuàng)建Notice.vue文件,內(nèi)容如下:
<template> <div class="notice-bar-container"> <div class="notice-bar" v-if="showNotice"> <div class="notice-content-wrapper"> <div class="notice-content" ref="noticeContent"> <span v-if="isScrolling">{{ noticeText }}</span> </div> </div> <button class="close-btn" @click="closeNotice">×</button> </div> </div> </template> <script setup> import { ref, onMounted, nextTick, onBeforeMount } from 'vue'; import { onMounted as onWindowMounted } from '@vue/runtime-core'; // 控制公告欄是否顯示的響應(yīng)式變量 const showNotice = ref(true); // 公告內(nèi)容 const noticeText = ref('這里是公告內(nèi)容示例,可替換哦'); const noticeContent = ref(null); // 用于存儲頁面寬度 const pageWidth = ref(window.innerWidth); // 用來存儲滾動定時器 const scrollInterval = ref(null); // 控制公告內(nèi)容是否開始滾動展示的變量 const isScrolling = ref(false); // 關(guān)閉公告的方法 const closeNotice = () => { showNotice.value = false; if (scrollInterval.value) { clearInterval(scrollInterval.value); } }; // 在組件掛載前獲取頁面寬度(首次) onBeforeMount(() => { pageWidth.value = window.innerWidth; }); // 當(dāng)窗口大小變化時,更新頁面寬度 onWindowMounted(() => { window.addEventListener('resize', () => { pageWidth.value = window.innerWidth; }); }); onMounted(() => { nextTick(() => { // 獲取滾動內(nèi)容的寬度 const contentWidth = noticeContent.value.offsetWidth; // 設(shè)置外層容器寬度為頁面寬度的50%,并開啟滾動 noticeContent.value.parentNode.parentNode.style.width = `${pageWidth.value * 0.5}px`; noticeContent.value.parentNode.parentNode.style.marginLeft = 'auto'; noticeContent.value.parentNode.parentNode.style.marginRight = 'auto'; noticeContent.value.parentNode.parentNode.style.overflow = 'hidden'; // 克隆一份內(nèi)容用于滾動銜接 const cloneNode = noticeContent.value.cloneNode(true); noticeContent.value.parentNode.appendChild(cloneNode); // 滾動動畫邏輯 let scrollDistance = pageWidth.value * 0.5; const scrollSpeed = 2; // 調(diào)整滾動速度,可按需修改 scrollInterval.value = setInterval(() => { scrollDistance -= scrollSpeed; if (scrollDistance < -contentWidth) { scrollDistance = pageWidth.value * 0.5; } noticeContent.value.style.transform = `translateX(${scrollDistance}px)`; // 隱藏頁面展示的文字,只展示滾動的文字 noticeContent.value.parentNode.style.overflow = 'hidden'; // 清除公告內(nèi)容區(qū)域可能存在的文本節(jié)點(除了綁定的 noticeText 內(nèi)容) const childNodes = noticeContent.value.childNodes; for (let i = 0; i < childNodes.length; i++) { if (childNodes[i].nodeType === 3 && childNodes[i].textContent.trim()!== noticeText.value.trim()) { noticeContent.value.removeChild(childNodes[i]); } } // 開始滾動時設(shè)置 isScrolling 為 true,展示公告內(nèi)容 isScrolling.value = true; }, 30); }); }); </script> <style scoped> .notice-bar-container { width: 100%; display: flex; justify-content: center; } .notice-bar { position: fixed; top: 0; background-color: #f8d7da; color: #721c24; padding: 10px; display: flex; justify-content: space-between; align-items: center; z-index: 999; } .notice-content-wrapper { flex: 1; overflow: hidden; } .notice-content { white-space: nowrap; display: inline-block; } .close-btn { background-color: transparent; border: none; color: inherit; font-size: 16px; cursor: pointer; } </style>
亮點:
- 通過showNotice這個ref來控制公告欄是否顯示,初始化為true表示默認(rèn)顯示。
- noticeText用來存放公告的具體文本內(nèi)容,這里提供了一個示例文本,實際使用時可以替換成真實的公告內(nèi)容來源(比如從接口獲取等)。
- closeNotice方法用于點擊關(guān)閉按鈕時隱藏公告欄,即將showNotice設(shè)置為false。
- 樣式部分,設(shè)置公告欄為固定定位在頁面頂部,設(shè)置了合適的背景色、文字顏色、內(nèi)邊距等,并且讓關(guān)閉按鈕靠右對齊,同時給了較高的z-index值確保它能顯示在頁面上方。
第二種
<template> <div v-if="visible" class="announcement-container"> <div class="announcement-content" :style="{ width: contentWidth + 'px' }"> <span>{{ message }}</span> </div> <button class="close-btn" @click="closeAnnouncement">x</button> </div> </template> <script setup> import { ref, onMounted } from 'vue'; const visible = ref(true); // 公告是否顯示 const message = ref("這是一條滾動公告,您可以設(shè)置任何內(nèi)容顯示在這里。"); const contentWidth = ref(0); // 動態(tài)計算公告內(nèi)容的寬度 // 頁面加載時計算公告的寬度 onMounted(() => { contentWidth.value = window.innerWidth / 2; // 公告寬度為頁面寬度的50% }); // 關(guān)閉公告的邏輯 const closeAnnouncement = () => { visible.value = false; }; </script> <style scoped> .announcement-container { position: fixed; top: 22%; left: 50%; transform: translateX(-50%); width: 50%; background-color: #ff9800; color: white; padding: 10px; display: flex; justify-content: space-between; align-items: center; z-index: 9999; font-size: 16px; border-radius: 5px; overflow: hidden; } .announcement-content { white-space: nowrap; overflow: hidden; animation: scroll-left 20s linear infinite; } @keyframes scroll-left { 0% { transform: translateX(100%); } 100% { transform: translateX(-100%); } } .close-btn { background: transparent; border: none; color: white; font-size: 20px; cursor: pointer; padding: 5px; margin-left: 10px; } </style>
2. 注冊全局組件
在你的項目的入口文件(比如main.js或者main.ts,如果是 Vue 3 + TypeScript 的話)中進(jìn)行全局組件注冊,示例如下:
import { createApp } from 'vue'; import App from './App.vue'; import Notice from './components/Notice.vue'; const app = createApp(App); // 注冊全局公告組件 app.component('Notice', Notice); app.mount('#app');
通過app.component方法將Notice組件注冊為全局組件,這樣在項目的任何頁面(組件)中都可以直接使用標(biāo)簽來展示公告欄了。
3. 使用
例如在App.vue或者其他頁面組件中使用:
<template> <div id="app"> <Notice /> <router-view></router-view> </div> </template>
到此這篇關(guān)于Vue 封裝公告滾動的方法的文章就介紹到這了,更多相關(guān)Vue 公告滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Element?UI實現(xiàn)復(fù)制當(dāng)前行數(shù)據(jù)的功能
這篇文章主要介紹了如何使用Vue?+?Element?UI?實現(xiàn)在列表的操作欄新增一個復(fù)制按鈕,復(fù)制當(dāng)前行的數(shù)據(jù)可以打開新增彈窗后亦可以跳轉(zhuǎn)到新增頁面,感興趣的小伙伴可以參考下2023-11-11JavaScript實現(xiàn)簡單的圖片切換功能(實例代碼)
這篇文章主要介紹了JavaScript實現(xiàn)簡單的圖片切換功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-04-04vue創(chuàng)建項目卡住不動,vue?create?project卡住不動的解決
這篇文章主要介紹了vue創(chuàng)建項目卡住不動,vue?create?project卡住不動的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10