Vue自定義實(shí)現(xiàn)一個(gè)消息通知組件
一、效果描述
在JS中使用一個(gè)Message函數(shù),彈出一個(gè)自定義的消息框。
效果體驗(yàn):緩若江海凝清光
二、實(shí)現(xiàn)方式
1.新建一個(gè)消息組件
2.新建一個(gè)js文件,新建一個(gè)需要導(dǎo)出函數(shù)
3.在函數(shù)中新建一個(gè)Vue實(shí)例,并將消息組件掛載上去。
4.在需要使用到的地方導(dǎo)入
三、代碼展示
1.消息組件messageOne
<template>
<div
:class="yangshi == 0 ? 'message messageIn' : 'message messageOut'"
v-show="meShow"
:style="{
backgroundColor: tranColor,
color: getComplementColor(tranColor),
}"
@click="handleColse"
>
<div class="textBox">{{ message }}</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from "vue";
const props = defineProps({
message: String,
color: String,
});
const message = computed(() => props.message);
const emits = defineEmits(["click"]);
// 傳輸?shù)念伾?
const tranColor = computed(() => props.color);
const meShow = ref(true);
const yangshi = ref(0);
const changeShow = () => {
setTimeout(() => {
yangshi.value = 1;
}, 2500);
setTimeout(() => {
meShow.value = false;
}, 3000);
};
// 判斷顏色格式
const isRgbColor = (color: string) => {
// RGB格式的正則表達(dá)式
const rgbRegex = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
const rgbaRegex =
/^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1}|0\.\d+)\)$/;
// 檢查RGB或RGBA格式
if (rgbRegex.test(color) || rgbaRegex.test(color)) {
return "rgb";
}
// 十六進(jìn)制格式的正則表達(dá)式
const hexRegex = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
// 檢查十六進(jìn)制格式
if (hexRegex.test(color)) {
return "hex";
}
// 如果都不是,返回false
return false;
};
// hex轉(zhuǎn)rgb
const hexToRgb = (hex: string) => {
// 去除字符串前面的 '#'
hex = hex.replace("#", "");
// 如果顏色代碼只有三位(例如:#fff),則轉(zhuǎn)換為六位(例如:#ffffff)
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
// 將十六進(jìn)制顏色拆分為RGB三個(gè)分量
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
// 返回RGB對(duì)象或字符串,根據(jù)需要調(diào)整
// return {
// r: r,
// g: g,
// b: b
// };
// 如果需要返回字符串格式,可以使用以下代碼
return `rgb(${r}, ${g}, $)`;
};
// 獲取補(bǔ)色
const getComplementColor = (rgbString: string | undefined) => {
if (!rgbString) return;
let a = isRgbColor(rgbString);
if (a == "hex") {
rgbString = hexToRgb(rgbString);
}
// 正則表達(dá)式用于匹配rgb格式中的數(shù)值
const rgbRegex = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
const result = rgbString.match(rgbRegex);
// 如果沒有匹配到有效的rgb格式,則返回錯(cuò)誤
if (!result) {
throw new Error(
'Invalid RGB color format. Expected "rgb(R, G, B)" format.'
);
}
// 提取紅色、綠色和藍(lán)色的數(shù)值
const r = parseInt(result[1], 10);
const g = parseInt(result[2], 10);
const b = parseInt(result[3], 10);
// 計(jì)算補(bǔ)色的RGB值
const complementR = 255 - r;
const complementG = 255 - g;
const complementB = 255 - b;
// 格式化補(bǔ)色為"rgb(R, G, B)"字符串
const complementColor = `rgb(${complementR}, ${complementG}, ${complementB})`;
return complementColor;
};
const handleColse = () => {
emits("click");
};
changeShow();
</script>
<style scoped>
.message {
color: rgb(36, 21, 40);
min-width: 200px;
width: auto;
height: 70px;
background-color: rgba(17, 153, 20, 0.9);
position: absolute;
top: 50px;
left: 50vw;
transform: translateX(-50%);
display: flex;
justify-content: center;
align-items: center;
padding-left: 15px;
padding-right: 15px;
border-radius: 20px;
box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.2);
}
.messageIn {
animation: mShow 0.5s;
}
.messageOut {
animation: mNoShow 0.5s;
animation-fill-mode: forwards;
}
@keyframes mShow {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes mNoShow {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
</style>2.TS文件messageOne.ts
import { createApp } from "vue";
import MessageOne from "./messageOne.vue";
export function showMessageOne(message: string, onClick: any, color?: string) {
const div = document.createElement("div");
document.body.appendChild(div);
// 自定義掛載的組件和傳輸?shù)膮?shù)
const app = createApp(MessageOne, {
message,
color,
onClick() {
onClick(() => {
app.unmount();
div.remove();
});
},
});
app.mount(div);
}3.使用
<script setup lang="ts">
import { showMessageOne } from "../../components/messageOne";
const ClickButton = () => {
showMessageOne(
"消息通知",
(close: any) => {
close();
},
'#000'
);
};
</script>到此這篇關(guān)于Vue自定義實(shí)現(xiàn)一個(gè)消息通知組件的文章就介紹到這了,更多相關(guān)Vue消息通知組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目報(bào)錯(cuò):parseComponent問題及解決
這篇文章主要介紹了Vue項(xiàng)目報(bào)錯(cuò):parseComponent問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue2.0 資源文件assets和static的區(qū)別詳解
這篇文章主要介紹了vue2.0 資源文件assets和static的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Vue實(shí)現(xiàn)自動(dòng)檢測(cè)以及版本的更新
當(dāng)用戶在當(dāng)前站點(diǎn)停留時(shí)間比較長(zhǎng),中途站點(diǎn)進(jìn)行升級(jí)更新之后,用戶如果不刷新頁面就任然停留在舊的頁面里,如何讓用戶收到一個(gè)提示,引導(dǎo)用戶進(jìn)行更新操作呢?下面給大家介紹如何站點(diǎn)更新如何在生產(chǎn)環(huán)境提示用戶更新,進(jìn)行頁面刷新操作,核心原理其實(shí)很簡(jiǎn)單2023-03-03
vue.js過濾器+ajax實(shí)現(xiàn)事件監(jiān)聽及后臺(tái)php數(shù)據(jù)交互實(shí)例
這篇文章主要介紹了vue.js過濾器+ajax實(shí)現(xiàn)事件監(jiān)聽及后臺(tái)php數(shù)據(jù)交互,結(jié)合實(shí)例形式分析了vue.js前臺(tái)過濾器與ajax后臺(tái)數(shù)據(jù)交互相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
vue-element-admin按鈕級(jí)權(quán)限管控的實(shí)現(xiàn)
開發(fā)離不開權(quán)限,不同的用戶登錄,根據(jù)不同的權(quán)限,可以訪問不同的管理目錄,本文主要介紹了vue-element-admin按鈕級(jí)權(quán)限管控的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04

