Vue實(shí)現(xiàn)頁(yè)面添加滿屏水印和去除水印功能
1. 前言
在一些特殊的應(yīng)用場(chǎng)景中,可能需要在網(wǎng)頁(yè)上添加水印以保護(hù)版權(quán)或標(biāo)識(shí)信息。本文將介紹如何在Vue項(xiàng)目中添加滿屏水印并實(shí)現(xiàn)去除水印的功能。
2. 添加滿屏水印
2.1 創(chuàng)建水印組件
首先,我們需要?jiǎng)?chuàng)建一個(gè)水印組件,該組件會(huì)在頁(yè)面上生成一個(gè)滿屏的水印。
<!-- Watermark.vue --> <template> <div class="watermark" ref="watermark"></div> </template> <script> export default { props: { text: { type: String, required: true } }, mounted() { this.addWatermark(this.text); }, methods: { addWatermark(text) { const watermarkDiv = this.$refs.watermark; const canvas = document.createElement('canvas'); canvas.width = 200; canvas.height = 200; const ctx = canvas.getContext('2d'); ctx.rotate(-20 * Math.PI / 180); ctx.font = '20px Arial'; ctx.fillStyle = 'rgba(200, 200, 200, 0.50)'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(text, canvas.width / 2, canvas.height / 2); watermarkDiv.style.backgroundImage = `url(${canvas.toDataURL('image/png')})`; watermarkDiv.style.pointerEvents = 'none'; watermarkDiv.style.position = 'fixed'; watermarkDiv.style.top = 0; watermarkDiv.style.left = 0; watermarkDiv.style.width = '100%'; watermarkDiv.style.height = '100%'; watermarkDiv.style.zIndex = 9999; } } }; </script> <style scoped> .watermark { background-repeat: repeat; } </style>
2.2 在頁(yè)面中使用水印組件
將水印組件引入到需要添加水印的頁(yè)面,并在模板中使用。
<template> <div> <Watermark text="這是一個(gè)水印"></Watermark> <!-- 你的其他頁(yè)面內(nèi)容 --> </div> </template> <script> import Watermark from './components/Watermark.vue'; export default { components: { Watermark } }; </script>
示例效果
3. 去除水印
為了去除水印,我們可以使用一個(gè)簡(jiǎn)單的方法來(lái)控制水印組件的顯示和隱藏。
3.1 修改Watermark組件以支持動(dòng)態(tài)顯示
在水印組件中添加一個(gè)v-if
指令,以便動(dòng)態(tài)控制其顯示和隱藏。
<!-- Watermark.vue --> <template> <div class="watermark" v-if="visible" ref="watermark"></div> </template> <script> export default { props: { text: { type: String, required: true }, visible: { type: Boolean, default: true } }, mounted() { this.addWatermark(this.text); }, methods: { addWatermark(text) { const watermarkDiv = this.$refs.watermark; const canvas = document.createElement('canvas'); canvas.width = 200; canvas.height = 200; const ctx = canvas.getContext('2d'); ctx.rotate(-20 * Math.PI / 180); ctx.font = '20px Arial'; ctx.fillStyle = 'rgba(200, 200, 200, 0.50)'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(text, canvas.width / 2, canvas.height / 2); watermarkDiv.style.backgroundImage = `url(${canvas.toDataURL('image/png')})`; watermarkDiv.style.pointerEvents = 'none'; watermarkDiv.style.position = 'fixed'; watermarkDiv.style.top = 0; watermarkDiv.style.left = 0; watermarkDiv.style.width = '100%'; watermarkDiv.style.height = '100%'; watermarkDiv.style.zIndex = 9999; } } }; </script>
3.2 動(dòng)態(tài)控制水印顯示
在頁(yè)面中通過(guò)數(shù)據(jù)綁定控制水印的顯示和隱藏。
<template> <div> <button @click="toggleWatermark">切換水印</button> <Watermark :text="watermarkText" :visible="watermarkVisible"></Watermark> <!-- 你的其他頁(yè)面內(nèi)容 --> </div> </template> <script> import Watermark from './components/Watermark.vue'; export default { components: { Watermark }, data() { return { watermarkText: '這是一個(gè)水印', watermarkVisible: true }; }, methods: { toggleWatermark() { this.watermarkVisible = !this.watermarkVisible; } } }; </script>
4. 總結(jié)
通過(guò)本文的介紹,我們學(xué)習(xí)了如何在Vue項(xiàng)目中添加滿屏水印,并實(shí)現(xiàn)動(dòng)態(tài)控制水印的顯示和隱藏。使用Canvas生成水印圖像,再通過(guò)CSS樣式實(shí)現(xiàn)水印的全屏覆蓋,可以有效保護(hù)網(wǎng)頁(yè)內(nèi)容。
以上就是Vue實(shí)現(xiàn)頁(yè)面添加滿屏水印和去除水印功能的詳細(xì)內(nèi)容,更多關(guān)于Vue頁(yè)面添加和去除水印的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue使用element-ui的el-input監(jiān)聽(tīng)不了回車事件的解決方法
小編在使用element-ui時(shí),el-input組件監(jiān)聽(tīng)不了回車事件,怎么回事呢?下面小編給大家?guī)?lái)了vue使用element-ui的el-input監(jiān)聽(tīng)不了回車事件的解決方法,一起看看吧2018-01-01一文帶你搞懂Vue中Provide/Inject的使用與高級(jí)應(yīng)用
這篇文章將詳細(xì)介紹如何在?Vue.js?中使用?provide?和?inject?模式,并探討其在實(shí)際應(yīng)用中的高級(jí)用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11vue3 開(kāi)始時(shí)間與結(jié)束時(shí)間比較驗(yàn)證(結(jié)束時(shí)間需要大于開(kāi)始時(shí)間)
本文通過(guò)示例代碼介紹了vue3 開(kāi)始時(shí)間與結(jié)束時(shí)間比較驗(yàn)證(結(jié)束時(shí)間需要大于開(kāi)始時(shí)間)的相關(guān)操作,代碼簡(jiǎn)單易懂,感興趣的朋友跟隨小編一起看看吧2024-07-07el-table渲染慢卡頓問(wèn)題最優(yōu)解決方案
本文主要介紹了el-table渲染慢卡頓問(wèn)題最優(yōu)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能
本文通過(guò)兩種思路給大家介紹vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能,感興趣的朋友跟隨小編一起看看吧2019-04-04Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)
這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11