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單行文本溢出會(huì)出現(xiàn)title提示自定義指令
這篇文章主要為大家介紹了vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Vue終端取消vue、prettier警告warn問(wèn)題
這篇文章主要介紹了Vue終端取消vue、prettier警告warn問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10詳解vue-cli 腳手架項(xiàng)目-package.json
本篇文章主要介紹了詳解vue-cli 腳手架項(xiàng)目-package.json,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07vue router學(xué)習(xí)之動(dòng)態(tài)路由和嵌套路由詳解
本篇文章主要介紹了vue router 動(dòng)態(tài)路由和嵌套路由,詳細(xì)的介紹了動(dòng)態(tài)路由和嵌套路由的使用方法,有興趣的可以了解一下2017-09-09VUE中如何調(diào)用高德地圖獲取當(dāng)前位置(VUE2.0和3.0通用)
使用uniapp開發(fā)微信小程序時(shí),多多少少會(huì)遇到獲取當(dāng)前位置的詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于VUE中如何調(diào)用高德地圖獲取當(dāng)前位置(VUE2.0和3.0通用)的相關(guān)資料,需要的朋友可以參考下2023-04-04vue 內(nèi)聯(lián)樣式style中的background用法說(shuō)明
這篇文章主要介紹了vue 內(nèi)聯(lián)樣式style中的background用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue-resource攔截器設(shè)置頭信息的實(shí)例
下面小編就為大家?guī)?lái)一篇vue-resource攔截器設(shè)置頭信息的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10