watermark base on position fixed

just for backup of watermark.

Watermark Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* global Tiger */

Tiger.Watermark = function Watermark() {

var _wmObserver;
var _wmParentObserver;
var _windowsWidth = window.outerWidth;
var _windowsHeight = window.outerHeight;

/**
* create Dom of watermark's container
* @param {Tiger.Watermark} watermark
*/
var _createContainer = function (watermark) {
watermark._container = document.createElement('div');
$(watermark._container).appendTo('.tiger-main-container')
.addClass('tiger-watermark-container')
.addClass(watermark.options.className)
.attr('aria-hidden', true);
};

/**
* create watermark's Dom
* @param {Tiger.Watermark} watermark
* @param {Object} options
*/
var _createWatermark = function (watermark, options) {
options.rowSpaceing = options.rowSpaceing || 50;
options.colSpaceing = options.colSpaceing || 30;
options.width = options.width || 150;
options.height = options.height || 20;
let navbarHeight = $('.tiger-navbar').outerHeight() || 0;
let rows = parseInt((_windowsHeight - navbarHeight) / (options.height + options.rowSpaceing));
let cols = parseInt(_windowsWidth / (options.width + options.colSpaceing));
let offsetLeft = (_windowsWidth - options.width * cols - options.colSpaceing * (cols - 1)) / 2;
let offsetTop = navbarHeight / 2 + (_windowsHeight - options.height * rows - options.rowSpaceing * (rows - 1)) / 2;
for (let row = 0; row < rows; row++) {
let top = offsetTop + (options.rowSpaceing + options.height) * row;
for (let col = 0; col < cols; col++) {
let left = offsetLeft + (options.colSpaceing + options.width) * col;
$(document.createElement('div')).appendTo(watermark._container)
.addClass('tiger-watermark')
.css({
'left': `${left}px`,
'top': `${top}px`,
'width': `${options.width}px`,
'height': `${options.height}px`,
'transform': `rotate(${options.rotate}deg)`,
'opacity': `${options.opacity}`,
'font-size': `${options.fontSize}rem`
})
.html(options.content);
}
}
};

/**
* Re render watermark
* @param {Tiger.Watermark} watermark
* @param {Object} options
*/
var _render = function (watermark, options){
_wmObserver.disconnect();
$(watermark._container).empty();
_createWatermark(watermark, options);
_wmObserver.observe(watermark._container, {
attributes: true,
childList: true,
characterData: true,
subtree:true
});
};

/**
* Observe watermark and watermark's parentNode mutations
* @param {Tiger.Watermark} watermark
*/
var _addObserve = function(watermark){
//observe watermark element and its child element
_wmObserver = new MutationObserver(function(mutations, observer) {
_render(watermark, watermark.options);
});
_wmObserver.observe(watermark._container, {
attributes: true,
childList: true,
characterData: true,
subtree:true
});
//observe parent element, recreate if the element is deleted
_wmParentObserver = new MutationObserver(function(mutations) {
for(m of mutations){
if(m.type === 'childList' && m.removedNodes.length > 0
&& $('.tiger-watermark-container').length !== 1) {
$(watermark._container).appendTo('.tiger-main-container');
}
}
});
_wmParentObserver.observe(watermark._container.parentNode, {
childList: true,
subtree: true
});
};

/**
* windows resize listener
* @param {Tiger.Watermark} watermark
*/
var _addResizeListener = function(watermark){
$(window).on('resize', function(){
if(window.outerHeight !== _windowsHeight || window.outerWidth !== _windowsWidth){
_windowsHeight = window.outerHeight;
_windowsWidth = window.outerWidth;
_render(watermark, watermark.options);
}
});
};

/**
* Tiger.Watermark.<br/>
* create watermark for webpage and automatic adjust when windows resie.
* @param {Object} options
* @param {String} options.content watermark's text
* @param {Number} [options.width=150] watermark's width. unit: px
* @param {Number} [options.height=20] watermark's height. unit: px
* @param {Number} [options.RowSpaceing=50] row spaceing of watermarks. unit: px
* @param {Number} [options.colSpaceing=30] col spaceing of watermarks. unit: px
* @param {Number} [options.rotate=15] watermark's tangent angle. unit: deg
* @param {Number} [options.opacity=0.15] watermark's transparency
* @param {Number} [options.fontSize=0.85] watermark's fontSize. unit: rem
*/
function Watermark(options = {}) {
var _proto = Watermark.prototype;
this.options = options;
_createContainer(this);
_createWatermark(this, this.options);
_addObserve(this);
_addResizeListener(this);

/**
* upload watermark's text content
* @param {String} content watermark's text
*/
_proto.upload = function (content) {
if(!content){
return;
}
_wmParentObserver.disconnect();
_wmObserver.disconnect();
this.options.content = content;
$(this._container).find('.tiger-watermark')
.html(content);
_wmParentObserver.observe(this._container.parentNode, {
childList: true,
subtree: true
});
_wmObserver.observe(this._container, {
attributes: true,
childList: true,
characterData: true,
subtree:true
});

};

/**
* Re render watermark
* @param {Object} options
*/
_proto.render = function(options = {}){
_render(this, Object.assign(this.options, options));
}

/**
* Force remove watermark
*/
_proto.remove = function () {
_wmParentObserver.disconnect();
$(this._container).remove();
};
}
return Watermark;
}();

Style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* tiger-watermark */
.tiger-watermark-container{
display: block !important;
pointer-events: none !important;
}
.tiger-watermark{
position: fixed;
user-select: none !important;
z-index: 999999;
word-break: break-all;
overflow: hidden;
text-align: center;
transform: rotate(15deg);
font-size: 0.8rem;
opacity: 0.15;
}

Use

1
2
3
4
5
6
7
8
9
10
11
12
var wm = new Tiger.Watermark({
'content': '系统管理员 (admin)'
});

wm.upload('李四啊_ (Lruihao)');

wm.remder({
font-size: 0.8,
opacity: 0.25
});

wm.remove();
点击查看
左下角关注一下公众号吧!
0%