Vue2實現(xiàn)全局水印效果的示例代碼
更新時間:2023年07月27日 11:46:27 作者:fruge
這篇文章主要為大家學(xué)習(xí)介紹了如何利用Vue2實現(xiàn)全局水印的效果,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以了解下
最近寫項目遇到一個需求,全局顯示水印,不管在哪個路由都要顯示。
想要實現(xiàn)的效果:
新建shuiyin.js文件
// 定義水印函數(shù) const addWatermark = ({ container = document.body, // 水印添加到的容器,默認(rèn)為 body width = "200px", // 水印 canvas 的寬度 height = "100px", // 水印 canvas 的高度 textAlign = "center", // 水印文字的對齊方式 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)建一個 div 并設(shè)置樣式和類名 if (!__wm) { container.style.position = "relative"; container.insertBefore(watermarkDiv, container.firstChild); } // 監(jiān)聽容器變化,當(dāng)容器發(fā)生變化時重新調(diào)用 addWatermark 函數(shù) const { MutationObserver } = window; if (MutationObserver) { let mo = new MutationObserver(function () { const __wm = document.querySelector(".__wm"); // 只在__wm元素變動才重新調(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中全局注冊
import addWatermark from "@/utils/shuiyin"; Vue.use(addWatermark);
到此這篇關(guān)于Vue2實現(xiàn)全局水印效果的示例代碼的文章就介紹到這了,更多相關(guān)Vue全局水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue單行文本溢出會出現(xiàn)title提示自定義指令
這篇文章主要為大家介紹了vue單行文本溢出會出現(xiàn)title提示自定義指令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01vue router學(xué)習(xí)之動態(tài)路由和嵌套路由詳解
本篇文章主要介紹了vue router 動態(tài)路由和嵌套路由,詳細(xì)的介紹了動態(tài)路由和嵌套路由的使用方法,有興趣的可以了解一下2017-09-09VUE中如何調(diào)用高德地圖獲取當(dāng)前位置(VUE2.0和3.0通用)
使用uniapp開發(fā)微信小程序時,多多少少會遇到獲取當(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用法說明
這篇文章主要介紹了vue 內(nèi)聯(lián)樣式style中的background用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08