最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue 封裝公告滾動的方法

 更新時間:2025年01月10日 09:40:34   作者:博客zhu虎康  
文章介紹了如何在需求系統(tǒng)中創(chuàng)建一個位于頁面上方的公告展示組件,展示了如何在App.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-cli3打包dist路徑問題修改打包配置

    使用vue-cli3打包dist路徑問題修改打包配置

    這篇文章主要介紹了使用vue-cli3打包dist路徑問題修改打包配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • el-date-picker默認(rèn)結(jié)束為當(dāng)前時分秒的操作方法

    el-date-picker默認(rèn)結(jié)束為當(dāng)前時分秒的操作方法

    在element?ui中的日期時間選擇組件中默認(rèn)是00:00,現(xiàn)在需求是點擊默認(rèn)結(jié)束時間為當(dāng)前時分秒,查了很多資料寫的都不準(zhǔn)確?,今天給大家分享el-date-picker默認(rèn)結(jié)束為當(dāng)前時分秒的操作方法,感興趣的朋友一起看看吧
    2024-01-01
  • Vue 模板中 ref 的自動解包機(jī)制及常見誤區(qū)

    Vue 模板中 ref 的自動解包機(jī)制及常見誤區(qū)

    從Vue 3的Composition API開始,ref成為了處理響應(yīng)式數(shù)據(jù)的核心方式之一,本文給大家介紹Vue模板中ref的自動解包機(jī)制及常見誤區(qū),感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • Vue中Vue.extend()的使用及解析

    Vue中Vue.extend()的使用及解析

    這篇文章主要介紹了Vue中Vue.extend()的使用及解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue項目中Element UI組件未注冊的問題原因及解決方法

    Vue項目中Element UI組件未注冊的問題原因及解決方法

    在 Vue 項目中使用 Element UI 組件庫時,開發(fā)者可能會遇到一些常見問題,例如組件未正確注冊導(dǎo)致的警告或錯誤,本文將詳細(xì)探討這些問題的原因、解決方法,以及如何在需要時撤銷相關(guān)操作,需要的朋友可以參考下
    2025-01-01
  • vue過濾器filter的使用方法詳解

    vue過濾器filter的使用方法詳解

    這篇文章主要給大家介紹了關(guān)于vue過濾器filter的使用方法,Vue.js的過濾器(Filter)是一種可重用的功能,用于對文本進(jìn)行格式化,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 在 Vue 3 中設(shè)置 `@` 指向根目錄的幾種常見方法匯總

    在 Vue 3 中設(shè)置 `@` 指向根目錄的幾種常見方法匯總

    在 Vue 3 項目開發(fā)中,為了方便管理和引用文件路徑,設(shè)置 @ 指向根目錄是一項常見的需求,下面給大家分享在Vue3中設(shè)置 `@` 指向根目錄的方法匯總,感興趣的朋友一起看看吧
    2024-06-06
  • vue中ts無法識別引入的vue文件,提示找不到xxx.vue模塊的解決

    vue中ts無法識別引入的vue文件,提示找不到xxx.vue模塊的解決

    這篇文章主要介紹了vue中ts無法識別引入的vue文件,提示找不到xxx.vue模塊的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue實現(xiàn)添加標(biāo)簽demo示例代碼

    vue實現(xiàn)添加標(biāo)簽demo示例代碼

    本篇文章主要介紹了vue實現(xiàn)添加標(biāo)簽demo示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • vue依賴包報錯問題eslint\lib\cli-engine\cli-engine.js:421

    vue依賴包報錯問題eslint\lib\cli-engine\cli-engine.js:421

    這篇文章主要介紹了vue依賴包報錯問題eslint\lib\cli-engine\cli-engine.js:421,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論

达拉特旗| 南京市| 五常市| 鞍山市| 开鲁县| 武义县| 江门市| 天津市| 阿鲁科尔沁旗| 江城| 阿城市| 奉化市| 云霄县| 盐城市| 于都县| 五常市| 中宁县| 黑水县| 西平县| 台州市| 维西| 施秉县| 错那县| 砚山县| 黄陵县| 兴业县| 苏尼特右旗| 大庆市| 察雅县| 内黄县| 乐亭县| 新田县| 朔州市| 津市市| 陵川县| 黎平县| 于都县| 扎囊县| 石楼县| 贵州省| 敖汉旗|