欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue 封裝公告滾動(dòng)的方法

 更新時(shí)間:2025年01月10日 09:40:34   作者:博客zhu虎康  
文章介紹了如何在需求系統(tǒng)中創(chuàng)建一個(gè)位于頁(yè)面上方的公告展示組件,展示了如何在App.vue或其他頁(yè)面組件中使用這個(gè)公告組件,感興趣的朋友一起看看吧

需求

系統(tǒng)中需要有一個(gè)公告展示,且這個(gè)公告位于頁(yè)面上方,每個(gè)頁(yè)面都要看到

分析

1. 創(chuàng)建公告組件Notice.vue

第一種

在你的項(xiàng)目的合適組件目錄下(比如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);
// 用于存儲(chǔ)頁(yè)面寬度
const pageWidth = ref(window.innerWidth);
// 用來(lái)存儲(chǔ)滾動(dòng)定時(shí)器
const scrollInterval = ref(null);
// 控制公告內(nèi)容是否開始滾動(dòng)展示的變量
const isScrolling = ref(false);
// 關(guān)閉公告的方法
const closeNotice = () => {
  showNotice.value = false;
  if (scrollInterval.value) {
    clearInterval(scrollInterval.value);
  }
};
// 在組件掛載前獲取頁(yè)面寬度(首次)
onBeforeMount(() => {
  pageWidth.value = window.innerWidth;
});
// 當(dāng)窗口大小變化時(shí),更新頁(yè)面寬度
onWindowMounted(() => {
  window.addEventListener('resize', () => {
    pageWidth.value = window.innerWidth;
  });
});
onMounted(() => {
  nextTick(() => {
    // 獲取滾動(dòng)內(nèi)容的寬度
    const contentWidth = noticeContent.value.offsetWidth;
    // 設(shè)置外層容器寬度為頁(yè)面寬度的50%,并開啟滾動(dòng)
    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)容用于滾動(dòng)銜接
    const cloneNode = noticeContent.value.cloneNode(true);
    noticeContent.value.parentNode.appendChild(cloneNode);
    // 滾動(dòng)動(dòng)畫邏輯
    let scrollDistance = pageWidth.value * 0.5;
    const scrollSpeed = 2; // 調(diào)整滾動(dòng)速度,可按需修改
    scrollInterval.value = setInterval(() => {
      scrollDistance -= scrollSpeed;
      if (scrollDistance < -contentWidth) {
        scrollDistance = pageWidth.value * 0.5;
      }
      noticeContent.value.style.transform = `translateX(${scrollDistance}px)`;
      // 隱藏頁(yè)面展示的文字,只展示滾動(dòng)的文字
      noticeContent.value.parentNode.style.overflow = 'hidden';
      // 清除公告內(nèi)容區(qū)域可能存在的文本節(jié)點(diǎn)(除了綁定的 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]);
        }
      }
      // 開始滾動(dòng)時(shí)設(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>

亮點(diǎn):

  • 通過(guò)showNotice這個(gè)ref來(lái)控制公告欄是否顯示,初始化為true表示默認(rèn)顯示。
  • noticeText用來(lái)存放公告的具體文本內(nèi)容,這里提供了一個(gè)示例文本,實(shí)際使用時(shí)可以替換成真實(shí)的公告內(nèi)容來(lái)源(比如從接口獲取等)。
  • closeNotice方法用于點(diǎn)擊關(guān)閉按鈕時(shí)隱藏公告欄,即將showNotice設(shè)置為false。
  • 樣式部分,設(shè)置公告欄為固定定位在頁(yè)面頂部,設(shè)置了合適的背景色、文字顏色、內(nèi)邊距等,并且讓關(guān)閉按鈕靠右對(duì)齊,同時(shí)給了較高的z-index值確保它能顯示在頁(yè)面上方。

第二種

<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("這是一條滾動(dòng)公告,您可以設(shè)置任何內(nèi)容顯示在這里。");
const contentWidth = ref(0); // 動(dòng)態(tài)計(jì)算公告內(nèi)容的寬度
// 頁(yè)面加載時(shí)計(jì)算公告的寬度
onMounted(() => {
  contentWidth.value = window.innerWidth / 2; // 公告寬度為頁(yè)面寬度的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. 注冊(cè)全局組件

在你的項(xiàng)目的入口文件(比如main.js或者main.ts,如果是 Vue 3 + TypeScript 的話)中進(jìn)行全局組件注冊(cè),示例如下:

import { createApp } from 'vue';
import App from './App.vue';
import Notice from './components/Notice.vue';
const app = createApp(App);
// 注冊(cè)全局公告組件
app.component('Notice', Notice);
app.mount('#app');

通過(guò)app.component方法將Notice組件注冊(cè)為全局組件,這樣在項(xiàng)目的任何頁(yè)面(組件)中都可以直接使用標(biāo)簽來(lái)展示公告欄了。

3. 使用

例如在App.vue或者其他頁(yè)面組件中使用:

<template>
  <div id="app">
    <Notice />
    <router-view></router-view>
  </div>
</template>

到此這篇關(guān)于Vue 封裝公告滾動(dòng)的方法的文章就介紹到這了,更多相關(guān)Vue 公告滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue+Element?UI實(shí)現(xiàn)復(fù)制當(dāng)前行數(shù)據(jù)的功能

    Vue+Element?UI實(shí)現(xiàn)復(fù)制當(dāng)前行數(shù)據(jù)的功能

    這篇文章主要介紹了如何使用Vue?+?Element?UI?實(shí)現(xiàn)在列表的操作欄新增一個(gè)復(fù)制按鈕,復(fù)制當(dāng)前行的數(shù)據(jù)可以打開新增彈窗后亦可以跳轉(zhuǎn)到新增頁(yè)面,感興趣的小伙伴可以參考下
    2023-11-11
  • 詳解如何在vue項(xiàng)目中引入elementUI組件

    詳解如何在vue項(xiàng)目中引入elementUI組件

    這篇文章主要介紹了詳解如何在vue項(xiàng)目中引入elementUI組件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能(實(shí)例代碼)

    JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能(實(shí)例代碼)

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-04-04
  • Vue-不允許嵌套式的渲染方法

    Vue-不允許嵌套式的渲染方法

    今天小編就為大家分享一篇Vue-不允許嵌套式的渲染方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Vue中如何使用Map鍵值對(duì)傳參詳析

    Vue中如何使用Map鍵值對(duì)傳參詳析

    最近在做一個(gè)vue的項(xiàng)目,碰到一點(diǎn)關(guān)于Map鍵值對(duì)傳參的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Vue中如何使用Map鍵值對(duì)傳參的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • vue全局引入scss(mixin)

    vue全局引入scss(mixin)

    本文主要介紹了?vue全局引入scss,我們?cè)趯慥UE的時(shí)候,會(huì)使用scss,也會(huì)做一些通用樣式,方便使用,在寫好的通用樣式的時(shí)候,每次都要單文件導(dǎo)入,剛開始寫的時(shí)候,感覺還好,后面工程量大了后,就顯得麻煩,那么本文就全局導(dǎo)入scss樣式,下面來(lái)看詳細(xì)內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決

    vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決

    這篇文章主要介紹了vue創(chuàng)建項(xiàng)目卡住不動(dòng),vue?create?project卡住不動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue的列表之渲染,排序,過(guò)濾詳解

    Vue的列表之渲染,排序,過(guò)濾詳解

    這篇文章主要為大家詳細(xì)介紹了Vue的列表之渲染,排序,過(guò)濾,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • Vue Router4 嵌套路由的示例代碼

    Vue Router4 嵌套路由的示例代碼

    在 Vue Router 4 中,嵌套路由是一種非常重要的功能,它允許我們創(chuàng)建更復(fù)雜的 UI 結(jié)構(gòu),同時(shí)保持路由的清晰和易于管理,這篇文章主要介紹了Vue Router4 嵌套路由,需要的朋友可以參考下
    2024-04-04
  • vue中ref引用操作DOM元素的實(shí)現(xiàn)

    vue中ref引用操作DOM元素的實(shí)現(xiàn)

    本文主要介紹了vue中ref引用操作DOM元素的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評(píng)論