Vue之全局水印的實(shí)現(xiàn)示例
【需求】系統(tǒng)內(nèi)頁面顯示水印,登錄頁面沒有水印(退出登錄時(shí),登錄頁面不會(huì)顯示水印)
1.創(chuàng)建水印Js文件/* * @Author: 劉小二 * @Date: 2021-07-15 14:43:27 * @LastEditTime: 2021-07-15 15:00:27 * @LastEditors: Please set LastEditors * @Description: 添加水印 * @FilePath: /huashijc_MeetingSys/src/common/warterMark.js */’use strict’ let watermark = {} let setWatermark = (str) => { let id = ’1.23452384164.123412415’ if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)) } let can = document.createElement(’canvas’) can.width = 250 can.height = 120 let cans = can.getContext(’2d’) cans.rotate(-15 * Math.PI / 150) cans.font = ’20px Vedana’ cans.fillStyle = ’rgba(200, 200, 200, 0.20)’ cans.textAlign = ’left’ cans.textBaseline = ’Middle’ cans.fillText(str, can.width / 8, can.height / 2) let div = document.createElement(’div’) div.id = id div.style.pointerEvents = ’none’ div.style.top = ’35px’ 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) }}const outWatermark = (id) => { if (document.getElementById(id) !== null) { const div = document.getElementById(id) div.style.display = ’none’ }}watermark.out = () => { const str = ’1.23452384164.123412415’ outWatermark(str)} export default watermark2.引入操作2.1 在App.vue中引用或其他頁面
// 1.在App.vue文件中,導(dǎo)入該文件import Watemark from ’@/common/watermark’;computed: { userName() { const name = this.$store.state.user.name return (name && name.length > 0) ? name : ’未獲取到用戶名’ }},mounted() { Watermark.set(this.userName)}// 2.在其他頁面引用import Watemark from ’@/common/watermark’;created() { Watermark.set(’admin’)}2.2 在router配置文件中引用
const outWatermark = (id) => { if (document.getElementById(id) !== null) { const div = document.getElementById(id) div.style.display = ’none’ }}router.afterEach((to) => { if(to.path == ’/’){ Watermark.out() // 清除水印 }else{ Watermark.set(’未獲取到用戶名’) // 設(shè)置水印title }});
到此這篇關(guān)于Vue之全局水印的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Vue 全局水印內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖3. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車4. Python 中如何使用 virtualenv 管理虛擬環(huán)境5. Python獲取B站粉絲數(shù)的示例代碼6. Python基于requests實(shí)現(xiàn)模擬上傳文件7. python利用opencv實(shí)現(xiàn)顏色檢測8. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題9. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)10. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效
