1、html
<div class="watermark_box">
<div class="watermark"></div>
<img src="https://s.cn.bing.net/th?id=OHR.SwallowtailFlower_ZH-CN5950463168_1920x1080.jpg" />
</div>
2、JavaScript
function waterDocumentSvg({
width = '130px',
height = '50px',
x = '37px',
y = "30px",
opacity = '0.5',
rotate = -20,
fontSize = '17px',
content = '请勿外传',
zIndex = 9,
color = "#34363B"
} = {
}) {
let svgStr = `<svg xmlns="http://www.w3.org/2000/svg" width="${
width}" height="${
height}">
<text x="${
x}" y="${
y}" transform = "rotate(${
rotate},0,0)" opacity="${
opacity}" font-size="${
fontSize}" fill="${
color}">${
content}</text>
</svg>`,
base64Url = `data:image/svg+xml;base64,${
window.btoa(unescape(encodeURIComponent(svgStr)))}`,
dom = document.querySelector('.watermark') || document.createElement("div"),
styleStr = `z-index: ${
zIndex}; background-image: url('${
base64Url}')`;
dom.setAttribute('style', styleStr);
}
waterDocumentSvg({
width: '97px',
height: '70px',
x: '30px',
y: "26px",
opacity: '0.5',
rotate: 20,
fontSize: '24px',
content: '水 印',
zIndex: 9,
color: "#34363B"
});
3、css
.watermark_box {
margin-top: 70px;
margin-left: 70px;
width: calc(100vh - 300px);
height: calc(100vh - 500px);
position: relative;
}
.watermark_box>img {
width: 100%;
height: 100%;
}
.watermark {
position: absolute;
top: 0;
left: 0;
width: calc(100vh - 300px);
height: calc(100vh - 500px);
pointer-events: none;
}