script.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <script src="__CDN__/assets/js/require{$Think.config.app_debug?'':'.min'}.js" data-main="__CDN__/assets/js/require-backend{$Think.config.app_debug?'':'.min'}.js?v={$site.version|htmlentities}"></script>
  2. <input type="hidden" value="{$Think.session.admin.username}" name="" id="username">
  3. <input type="hidden" value="{$Think.session.admin.company}" name="" id="company">
  4. <script>
  5. function watermark(element, config) {
  6. // 获取元素的坐标
  7. function getOffset(el) {
  8. if (el.offsetParent) {
  9. return {
  10. x: el.offsetLeft + getOffset(el.offsetParent).x,
  11. y: el.offsetTop + getOffset(el.offsetParent).y,
  12. };
  13. }
  14. return {
  15. x: el.offsetLeft,
  16. y: el.offsetTop,
  17. };
  18. }
  19. if (!element) return;
  20. // 默认配置
  21. const _config = {
  22. text1: '张三', //文本1
  23. text2: '13868686868', // 文本2
  24. start_x: 0, // x轴起始位置
  25. start_y: 0, // y轴起始位置
  26. space_x: 100, // x轴间距
  27. space_y: 50, // y轴间距
  28. width: 210, // 宽度
  29. height: 80, // 长度
  30. fontSize: 14, // 字体
  31. color: '#aaa', // 字色
  32. alpha: 0.4, // 透明度
  33. rotate: 15, // 倾斜度
  34. };
  35. // 替换默认配置
  36. if (arguments.length === 2 && typeof arguments[1] === "object") {
  37. const src = arguments[1] || {};
  38. for (let key in src) {
  39. if (src[key] && _config[key] && src[key] === _config[key]) {
  40. continue;
  41. } else if (src[key]) {
  42. _config[key] = src[key];
  43. }
  44. }
  45. }
  46. // 节点的总宽度
  47. const total_width = element.scrollWidth;
  48. // 节点的总高度
  49. const total_height = element.scrollHeight;
  50. // 创建文本碎片,用于包含所有的插入节点
  51. const mark = document.createDocumentFragment();
  52. // 水印节点的起始坐标
  53. const position = getOffset(element);
  54. let x = position.x + _config.start_x, y = position.y + _config.start_y;
  55. // 先循环y轴插入水印
  56. do {
  57. // 再循环x轴插入水印
  58. do {
  59. // 创建单个水印节点
  60. const item = document.createElement('div');
  61. item.className = 'watermark-item';
  62. // 设置节点的样式
  63. item.style.position = "absolute";
  64. item.style.zIndex = 99999;
  65. item.style.left = `${x}px`;
  66. item.style.top = `${y}px`;
  67. item.style.width = `${_config.width}px`;
  68. item.style.height = `${_config.height}px`;
  69. item.style.fontSize = `${_config.fontSize}px`;
  70. item.style.color = _config.color;
  71. item.style.textAlign = 'center';
  72. item.style.opacity = _config.alpha;
  73. item.style.filter = `alpha(opacity=${_config.alpha * 100})`;
  74. // item.style.filter = `opacity(${_config.alpha * 100}%)`;
  75. item.style.webkitTransform = `rotate(-${_config.rotate}deg)`;
  76. item.style.MozTransform = `rotate(-${_config.rotate}deg)`;
  77. item.style.msTransform = `rotate(-${_config.rotate}deg)`;
  78. item.style.OTransform = `rotate(-${_config.rotate}deg)`;
  79. item.style.transform = `rotate(-${_config.rotate}deg)`;
  80. item.style.pointerEvents = 'none'; //让水印不遮挡页面的点击事件
  81. // 创建text1水印节点
  82. const text1 = document.createElement('div');
  83. text1.appendChild(document.createTextNode(_config.text1));
  84. item.append(text1);
  85. // 创建text2水印节点
  86. const text2 = document.createElement('div');
  87. text2.appendChild(document.createTextNode(_config.text2));
  88. item.append(text2);
  89. // 添加水印节点到文本碎片
  90. mark.append(item);
  91. // x坐标递增
  92. x = x + _config.width + _config.space_x;
  93. // 超出文本右侧坐标停止插入
  94. } while (total_width + position.x > x + _config.width);
  95. // 重置x初始坐标
  96. x = position.x + _config.start_x;
  97. // y坐标递增
  98. y = y + _config.height + _config.space_y;
  99. // 超出文本底部坐标停止插入
  100. } while (total_height + position.y > y + _config.height);
  101. // 插入文档碎片
  102. element.append(mark);
  103. }
  104. var username = document.getElementById('username');
  105. var company = document.getElementById('company');
  106. //找到存放水印的盒子
  107. const element = document.getElementsByTagName('body')[0];
  108. watermark(element,{text1:username.value,text2:company.value});
  109. </script>