您好,登錄后才能下訂單哦!
今天小編給大家分享一下Vue全局水印如何實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
【需求】系統內頁面顯示水印,登錄頁面沒有水印(退出登錄時,登錄頁面不會顯示水印)
/* * @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 } // 該方法只允許調用一次 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 watermark
// 1.在App.vue文件中,導入該文件 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") }
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("未獲取到用戶名") // 設置水印title } });
以上就是“Vue全局水印如何實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。