Vue實(shí)現(xiàn)頁面添加水印功能
創(chuàng)建watermark.js文件
let watermark = {} let setWatermark = (str) => { let id = '1.23452384164.123412416'; if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)); } //創(chuàng)建一個(gè)畫布 let can = document.createElement('canvas'); //設(shè)置畫布的長寬 can.width = 120; can.height = 120; let cans = can.getContext('2d'); //旋轉(zhuǎn)角度 cans.rotate(-15 * Math.PI / 180); cans.font = '18px Vedana'; //設(shè)置填充繪畫的顏色、漸變或者模式 cans.fillStyle = 'rgba(200, 200, 200, 0.40)'; //設(shè)置文本內(nèi)容的當(dāng)前對(duì)齊方式 cans.textAlign = 'left'; //設(shè)置在繪制文本時(shí)使用的當(dāng)前文本基線 cans.textBaseline = 'Middle'; //在畫布上繪制填色的文本(輸出的文本,開始繪制文本的X坐標(biāo)位置,開始繪制文本的Y坐標(biāo)位置) cans.fillText(str, can.width / 8, can.height / 2); let div = document.createElement('div'); div.id = id; div.style.pointerEvents = 'none'; div.style.top = '30px'; div.style.left = '0px'; div.style.position = 'fixed'; div.style.zIndex = '100000'; div.style.width = document.documentElement.clientWidth + 'px'; div.style.height = document.documentElement.clientHeight + 'px'; div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'; document.body.appendChild(div); return id; } // 該方法只允許調(diào)用一次 watermark.set = (str) => { let id = setWatermark(str); setInterval(() => { if (document.getElementById(id) === null) { id = setWatermark(str); } }, 500); window.onresize = () => { setWatermark(str); }; } export default watermark;
具體用法:
1.在App.vue文件中,導(dǎo)入該文件
import Watermark from '@/common/js/watermark'; //路徑不要寫錯(cuò)
2.在mounted函數(shù)中調(diào)用
mounted: function () { Watermark.set("水印內(nèi)容") }
注意內(nèi)容:
一般我們的水印內(nèi)容是當(dāng)前用戶的id或者姓名,在App.vue文件的mounted函數(shù)中有可能獲取不到Vuex的用戶信息。
我們一般在路由跳轉(zhuǎn)的時(shí)候來進(jìn)行用戶的登錄和權(quán)限的判斷,所以我們可以將調(diào)用水印的方法放在router.afterEach()方法中,在router.beforeEach()方法中判斷用戶的登錄和權(quán)限的判斷,所以router.afterEach()方法中一定可以獲取到vuex中的用戶信息。
示例:
router.beforeEach((to, from, next) => { // 登錄判斷 store.dispatch('getuser').then((user) => { if (to.path === '/') { if (user.isLeader) { next('/Summary'); } else { next('/list'); } } else { next(); } }).catch((error) => { if (error.message === 'noUser') { //返回登錄界面 } else { Dialog.confirm({ title: '服務(wù)器錯(cuò)誤', message: '獲取用戶信息失敗,可嘗試重新進(jìn)入系統(tǒng)。', showCancelButton: false, showConfirmButton: false }); } }); }); router.afterEach((to) => { /* 路由發(fā)生變化修改頁面title */ if (to.meta.title) { changTitle(to.meta.title); } Watermark.set(store.state.user.userId); });
以上這篇Vue實(shí)現(xiàn)頁面添加水印功能就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+node 實(shí)現(xiàn)視頻在線播放的實(shí)例代碼
這篇文章主要介紹了vue+node 實(shí)現(xiàn)視頻在線播放的實(shí)例代碼,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10vue3.0在子組件中觸發(fā)的父組件函數(shù)方式
這篇文章主要介紹了vue3.0在子組件中觸發(fā)的父組件函數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互
這篇文章主要介紹了Vue前端如何實(shí)現(xiàn)與后端進(jìn)行數(shù)據(jù)交互,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解
這篇文章主要為大家介紹了rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue項(xiàng)目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案
這篇文章主要介紹了vue項(xiàng)目打包后,由于html被緩存導(dǎo)致出現(xiàn)白屏的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Vue3.0 自己實(shí)現(xiàn)放大鏡效果案例講解
這篇文章主要介紹了Vue3.0 自己實(shí)現(xiàn)放大鏡效果案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07