js给网页添加文字水印。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Watermark Example</title> <style> body { position: relative; z-index: 1; } #watermark { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 9999; } </style> </head> <body> <div id="watermark"></div> <script> function addWatermark(text) { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); // 设置 Canvas 大小 canvas.width = 300; canvas.height = 200; // 设置字体样式 ctx.font = "20px Arial"; ctx.fillStyle = "rgba(200, 200, 200, 0.3)"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; // 设置旋转角度 ctx.rotate(-20 * Math.PI / 180); ctx.fillText(text, canvas.width / 2, canvas.height / 2); // 将 Canvas 作为背景图片 document.getElementById('watermark').style.backgroundImage = `url(${canvas.toDataURL("image/png")})`; document.getElementById('watermark').style.backgroundRepeat = "repeat"; } // 添加水印文本 addWatermark("Watermark Text"); </script> </body> </html>