vue輕松實(shí)現(xiàn)水印效果
前言:
vue項(xiàng)目中使用水印效果,可指定容器
效果圖:
1、不指定容器
2、指定容器
實(shí)現(xiàn)方法:
1、新建一個(gè)配置文件 watermark.js ,可放util,也可放別的地方
let watermark = {} let setWatermark = (text, sourceBody) => { let id = Math.random()*10000+'-'+Math.random()*10000+'/'+Math.random()*10000 if (document.getElementById(id) !== null) { document.body.removeChild(document.getElementById(id)) } let can = document.createElement('canvas') can.width = 150 can.height = 120 let cans = can.getContext('2d') cans.rotate(-20 * Math.PI / 180) cans.font = '15px Vedana' cans.fillStyle = 'rgba(0, 0, 0, .5)' cans.textAlign = 'left' cans.textBaseline = 'Middle' cans.fillText(text, can.width / 20, can.height ) let water_div = document.createElement('div') water_div.id = id water_div.style.pointerEvents = 'none' water_div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat' if(sourceBody){ water_div.style.width = '100%' water_div.style.height = '100%' sourceBody.appendChild(water_div) }else{ water_div.style.top = '3px' water_div.style.left = '0px' water_div.style.position = 'fixed' water_div.style.zIndex = '100000' water_div.style.width = document.documentElement.clientWidth + 'px' water_div.style.height = document.documentElement.clientHeight + 'px' document.body.appendChild(water_div) } return id } /** * 該方法只允許調(diào)用一次 * @param: * @text == 水印內(nèi)容 * @sourceBody == 水印添加在哪里,不傳就是body * */ watermark.set = (text, sourceBody) => { let id = setWatermark(text, sourceBody) setInterval(() => { if (document.getElementById(id) === null) { id = setWatermark(text, sourceBody) } }, 2000) window.onresize = () => { setWatermark(text, sourceBody) } } export default watermark
2、在main.js中全局配置
// 水印 import watermark from './utils/watermark.js' Vue.prototype.$watermark = watermark
3、頁(yè)面中使用-全屏水印
this.$watermark.set("浩星2731")
4、頁(yè)面中使用-指定容器
<el-button @click="addWatermark">點(diǎn)我添加水印</el-button> <div ref="content" style="width: 500px;height: 500px;border: 1px solid #ccc;"> addWatermark(){ this.$watermark.set("浩星2731",this.$refs.content) }
5、如果覺(jué)得字體之間的距離太大了,改這個(gè)屬性就行了
can.width = 150 can.height = 120
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript的MVVM庫(kù)Vue.js入門(mén)學(xué)習(xí)筆記
這篇文章主要介紹了JavaScript的MVVM庫(kù)Vue.js入門(mén)學(xué)習(xí)筆記,Vue.js是一個(gè)新興的js庫(kù),主要用于實(shí)現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件,需要的朋友可以參考下2016-05-05VUE跳轉(zhuǎn)外部鏈接與網(wǎng)頁(yè)的方法示例
這篇文章主要給大家介紹了關(guān)于VUE跳轉(zhuǎn)外部鏈接與網(wǎng)頁(yè)的方法,記錄一下在vue項(xiàng)目中如何實(shí)現(xiàn)跳轉(zhuǎn)到一個(gè)新頁(yè)面,需要的朋友可以參考下2023-06-06VUE解決 v-html不能觸發(fā)點(diǎn)擊事件的問(wèn)題
今天小編就為大家分享一篇VUE解決 v-html不能觸發(fā)點(diǎn)擊事件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10vue directive定義全局和局部指令及指令簡(jiǎn)寫(xiě)
這篇文章主要介紹了vue directive定義全局和局部指令及指令簡(jiǎn)寫(xiě),本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11解決vue創(chuàng)建項(xiàng)目使用vue-router和vuex報(bào)錯(cuò)Object(...)is not a&nb
這篇文章主要介紹了解決vue創(chuàng)建項(xiàng)目使用vue-router和vuex報(bào)錯(cuò)Object(...)is not a function問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02proxy代理不生效以及vue?config.js不生效解決方法
在開(kāi)發(fā)Vue項(xiàng)目過(guò)程中,使用了Proxy代理進(jìn)行數(shù)據(jù)劫持,但是在實(shí)際運(yùn)行過(guò)程中發(fā)現(xiàn)代理并沒(méi)有生效,也就是說(shuō)數(shù)據(jù)并沒(méi)有被劫持,這篇文章主要給大家介紹了關(guān)于proxy代理不生效以及vue?config.js不生效解決方法的相關(guān)資料,需要的朋友可以參考下2023-11-11vue中實(shí)現(xiàn)圖片壓縮 file文件的方法
這篇文章主要介紹了vue中實(shí)現(xiàn)圖片壓縮 file文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05