Vue實現(xiàn)頁面添加滿屏水印和去除水印功能
1. 前言
在一些特殊的應(yīng)用場景中,可能需要在網(wǎng)頁上添加水印以保護版權(quán)或標(biāo)識信息。本文將介紹如何在Vue項目中添加滿屏水印并實現(xiàn)去除水印的功能。
2. 添加滿屏水印
2.1 創(chuàng)建水印組件
首先,我們需要創(chuàng)建一個水印組件,該組件會在頁面上生成一個滿屏的水印。
<!-- 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 在頁面中使用水印組件
將水印組件引入到需要添加水印的頁面,并在模板中使用。
<template>
<div>
<Watermark text="這是一個水印"></Watermark>
<!-- 你的其他頁面內(nèi)容 -->
</div>
</template>
<script>
import Watermark from './components/Watermark.vue';
export default {
components: {
Watermark
}
};
</script>
示例效果

3. 去除水印
為了去除水印,我們可以使用一個簡單的方法來控制水印組件的顯示和隱藏。
3.1 修改Watermark組件以支持動態(tài)顯示
在水印組件中添加一個v-if指令,以便動態(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 動態(tài)控制水印顯示
在頁面中通過數(shù)據(jù)綁定控制水印的顯示和隱藏。
<template>
<div>
<button @click="toggleWatermark">切換水印</button>
<Watermark :text="watermarkText" :visible="watermarkVisible"></Watermark>
<!-- 你的其他頁面內(nèi)容 -->
</div>
</template>
<script>
import Watermark from './components/Watermark.vue';
export default {
components: {
Watermark
},
data() {
return {
watermarkText: '這是一個水印',
watermarkVisible: true
};
},
methods: {
toggleWatermark() {
this.watermarkVisible = !this.watermarkVisible;
}
}
};
</script>
4. 總結(jié)
通過本文的介紹,我們學(xué)習(xí)了如何在Vue項目中添加滿屏水印,并實現(xiàn)動態(tài)控制水印的顯示和隱藏。使用Canvas生成水印圖像,再通過CSS樣式實現(xiàn)水印的全屏覆蓋,可以有效保護網(wǎng)頁內(nèi)容。
以上就是Vue實現(xiàn)頁面添加滿屏水印和去除水印功能的詳細內(nèi)容,更多關(guān)于Vue頁面添加和去除水印的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue使用element-ui的el-input監(jiān)聽不了回車事件的解決方法
小編在使用element-ui時,el-input組件監(jiān)聽不了回車事件,怎么回事呢?下面小編給大家?guī)砹藇ue使用element-ui的el-input監(jiān)聽不了回車事件的解決方法,一起看看吧2018-01-01
一文帶你搞懂Vue中Provide/Inject的使用與高級應(yīng)用
這篇文章將詳細介紹如何在?Vue.js?中使用?provide?和?inject?模式,并探討其在實際應(yīng)用中的高級用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)
本文通過示例代碼介紹了vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)的相關(guān)操作,代碼簡單易懂,感興趣的朋友跟隨小編一起看看吧2024-07-07
Vue props 單向數(shù)據(jù)流的實現(xiàn)
這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

