<div id="water-wrapper"></div>
<div>
<div>111111111111111111111111111111111111111111111111111111111111111111111111111111111111</div>
<div>111111111111111111111111111111111111111111111111111111111111111111111111111111111111</div>
<div>111111111111111111111111111111111111111111111111111111111111111111111111111111111111</div>
<div>111111111111111111111111111111111111111111111111111111111111111111111111111111111111</div>
<div>111111111111111111111111111111111111111111111111111111111111111111111111111111111111</div>
</div>
jquery版本
function water() {
const watermarkText = '不爱吃糖的程序媛'; // 水印文字内容
const wrap = document.querySelector('#water-wrapper');
const html = document.querySelector('html');
const body = document.querySelector('body');
html.style.margin = "0"
html.style.padding = "0"
body.style.margin = "0"
body.style.padding = "0"
wrap.style.width = "100vw"
wrap.style.height = "100vh"
wrap.style.display = "flex"
wrap.style.position = "absolute"
wrap.style.zIndex = "999999"
wrap.style.flexDirection = "row"
wrap.style.justifyContent = "space-around"
wrap.style.flexWrap = "wrap"
wrap.style.pointerEvents = "none"
wrap.style.fontSize = "16px"
wrap.style.color = "rgba(184, 184, 184, 0.6)"
wrap.style.overflow = "hidden"
const wrapWidth = wrap.offsetWidth; // 容器的宽度
const wrapHeight = wrap.offsetHeight; // 容器的高度
const tempSpan = document.createElement('span');
tempSpan.innerText = watermarkText;
tempSpan.style.visibility = 'hidden';
document.body.appendChild(tempSpan);
const watermarkWidth = tempSpan.getBoundingClientRect().width + 160; // 水印文本的实际宽度
const watermarkHeight = tempSpan.getBoundingClientRect().height + 120; // 水印文本的实际高度
document.body.removeChild(tempSpan);
const numCols = Math.floor(wrapWidth / watermarkWidth); // 计算每行可容纳的方格数量
const numRows = Math.floor(wrapHeight / watermarkHeight); // 计算可容纳的行数
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < numCols; j++) {
const watermark = document.createElement('div');
watermark.classList.add('watermark');
watermark.innerText = watermarkText;
watermark.style.padding = "60px 80px"
watermark.style.transform = "rotate(-45deg)"
watermark.style.transformOrigin = "center center"
wrap.appendChild(watermark);
}
}
}
water()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有