Vue2實(shí)現(xiàn)全局水印效果的示例代碼
最近寫項(xiàng)目遇到一個(gè)需求,全局顯示水印,不管在哪個(gè)路由都要顯示。
想要實(shí)現(xiàn)的效果:

新建shuiyin.js文件
// 定義水印函數(shù)
const addWatermark = ({
container = document.body, // 水印添加到的容器,默認(rèn)為 body
width = "200px", // 水印 canvas 的寬度
height = "100px", // 水印 canvas 的高度
textAlign = "center", // 水印文字的對(duì)齊方式
textBaseline = "middle", // 水印文字的基線
font = "16px Microsoft Yahei", // 水印文字的字體
fillStyle = "rgba(184, 184, 184, 0.6)", // 水印文字的填充樣式
content = "我是水印", // 水印文字的內(nèi)容
rotate = -30, // 水印文字的旋轉(zhuǎn)角度
zIndex = 10000, // 水印的 z-index 值
}) => {
// 生成水印 canvas
const canvas = document.createElement("canvas");
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
const ctx = canvas.getContext("2d");
ctx.textAlign = textAlign;
ctx.textBaseline = textBaseline;
ctx.font = font;
ctx.fillStyle = fillStyle;
ctx.rotate((Math.PI / 180) * rotate);
ctx.fillText(content, parseFloat(width) / 2, parseFloat(height) / 1);
// 將 canvas 轉(zhuǎn)換為 base64 URL
const base64Url = canvas.toDataURL("image/png");
console.log(base64Url);
const __wm = document.querySelector(".__wm");
const watermarkDiv = __wm || document.createElement("div");
const styleStr = `
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
z-index: ${zIndex};
pointer-events: none;
background: url('${base64Url}') left top repeat;
`;
watermarkDiv.setAttribute("style", styleStr);
watermarkDiv.classList.add("__wm");
// 則創(chuàng)建一個(gè) div 并設(shè)置樣式和類名
if (!__wm) {
container.style.position = "relative";
container.insertBefore(watermarkDiv, container.firstChild);
}
// 監(jiān)聽容器變化,當(dāng)容器發(fā)生變化時(shí)重新調(diào)用 addWatermark 函數(shù)
const { MutationObserver } = window;
if (MutationObserver) {
let mo = new MutationObserver(function () {
const __wm = document.querySelector(".__wm");
// 只在__wm元素變動(dòng)才重新調(diào)用__canvasWM
if ((__wm && __wm.getAttribute("style") !== styleStr) || !__wm) {
// 避免一直觸發(fā)
mo.disconnect();
mo = new MutationObserver(() => {});
addWatermark({
container: document.getElementById("app"),
width: "200px",
height: "100px",
textAlign: "center",
textBaseline: "middle",
font: "16px Microsoft Yahei",
fillStyle: "rgba(184, 184, 184, 0.3 )",
content,
rotate: -30,
zIndex: 10000,
});
}
});
mo.observe(container, {
attributes: true,
subtree: true,
childList: true,
});
}
};
export default addWatermark;main.js中全局注冊(cè)
import addWatermark from "@/utils/shuiyin"; Vue.use(addWatermark);
到此這篇關(guān)于Vue2實(shí)現(xiàn)全局水印效果的示例代碼的文章就介紹到這了,更多相關(guān)Vue全局水印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue數(shù)組遍歷方法forEach和map的原理解析和實(shí)際應(yīng)用
這篇文章主要介紹了詳解vue數(shù)組遍歷方法forEach和map的原理解析和實(shí)際應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
vue實(shí)現(xiàn)簡(jiǎn)潔文件上傳進(jìn)度條功能
這篇文章主要介紹了vue實(shí)現(xiàn)簡(jiǎn)潔文件上傳進(jìn)度條功能,實(shí)現(xiàn)原理是通過performance.now()獲取動(dòng)畫的時(shí)間戳,用于創(chuàng)建流暢的動(dòng)畫,結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
vue-admin-box第一步npm?install時(shí)報(bào)錯(cuò)的處理
這篇文章主要介紹了vue-admin-box第一步npm?install時(shí)報(bào)錯(cuò)的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue中嵌入可浮動(dòng)的第三方網(wǎng)頁窗口的示例詳解
本文介紹了在Vue2項(xiàng)目中嵌入可浮動(dòng)的第三方網(wǎng)頁窗口的實(shí)現(xiàn)方法,包括使用iframe、div+script和dialog元素等方式,并提供了一個(gè)實(shí)戰(zhàn)Demo,展示了如何在Vue組件中動(dòng)態(tài)加載和控制浮窗的顯示,需要的朋友可以參考下2025-02-02

