svg輕鬆實現文字水印

1. 水印圖片生成採用svg,這樣能夠運行時生成名字或其餘信息的圖片css

svg模板html

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300px" height="160px" viewBox="0 0 300 160">
    <text x="-100" y="-30" fill='#000'  transform = "rotate(-35 240 -200)" fill-opacity='0.03' font-size='40'>I love SVG</text>
</svg>

必定要記得設置透明度(fill-opacity),不然水印會遮擋正文svg

svg須要轉碼: https://codepen.io/huashiyiqike/pen/rERBeJthis

2. 經過css覆蓋在整個頁面上url

水印元素放到頁面大小的容器的最後面spa

父元素:
position: relative;
水印元素
position: absolute; top: 0; bottom: 0; // 總體覆蓋 left: 0; width: 100%; height: 100%; z-index: 100; pointer-events: none; // 對鼠標穿透

3. 經過js拼接svg,並寫入模板或者htmlcode

js: 
this.watermarkstyle = `background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='200px' height='160px' viewBox='0 0 200 160'%3E %3Ctext x='-100' y='-30' fill='%23000' transform = 'rotate(-35 240 -200)' fill-opacity='0.04' font-size='40'%3E${ this.$store.state.authUser.username }%3C/text%3E %3C/svg%3E ")`

html:
<div class="watermark" :style="watermarkstyle"></div>
相關文章
相關標籤/搜索