-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransparent.js
More file actions
163 lines (156 loc) · 5.5 KB
/
transparent.js
File metadata and controls
163 lines (156 loc) · 5.5 KB
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
(function (window) {
"use strict";
var ZYTransparent = function(params) {
this.extend(this.params, params);
this.scrollByElem = window;
if (!this.scrollByElem) {
throw new Error("监听滚动的元素不存在");
}
this.isNativeScroll = false;
if (this.scrollByElem === window) {
this.isNativeScroll = true;
}
this._style = this.params.element.style;
this._bgColor = this._style.backgroundColor;
var color = this.getColor(this.getStyles(this.params.element, 'backgroundColor'));
if (color.length) {
this._R = color[0];
this._G = color[1];
this._B = color[2];
this._A = parseFloat(color[3]);
this.lastOpacity = this._A;
this._bufferFn = this.buffer(this.handleScroll, this.params.duration, this);
this.init();
} else {
throw new Error("元素背景颜色必须为RGBA");
}
};
ZYTransparent.prototype = {
params: {
element: false,
top: 0, // 距离顶部高度(到达该高度即触发)
offset: 150, // 滚动透明距离档设定top值后offset也会随着top向下延伸
duration: 16 // 过渡时间
},
init: function() {
var self = this;
if(!self.params.element){
return;
}
this.scrollByElem.addEventListener('scroll', this._bufferFn);
if (this.isNativeScroll) { //原生scroll
this.scrollByElem.addEventListener('touchmove', this._bufferFn);
}
},
handleScroll: function(e) {
var y = window.scrollY;
if (!this.isNativeScroll && e && e.detail) {
y = -e.detail.y;
}
var opacity = (y - this.params.top) / this.params.offset + this._A;
opacity = Math.min(Math.max(this._A, opacity), 1);
this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + opacity + ')';
if (this.lastOpacity !== opacity) {
this.trigger(this.params.element, 'alpha', {
alpha: opacity
});
this.lastOpacity = opacity;
}
},
trigger: function(element, eventType, eventData) {
element.dispatchEvent(new CustomEvent(eventType, {
detail: eventData,
bubbles: true,
cancelable: true
}));
return this;
},
buffer: function(fn, ms, context) {
var timer;
var lastStart = 0;
var lastEnd = 0;
var ms = ms || 150;
var that = this;
function run() {
if (timer) {
timer.cancel();
timer = 0;
}
lastStart = +new Date();
fn.apply(context || this, arguments);
lastEnd = +new Date();
}
return this.extend(function() {
if (
(!lastStart) || // 从未运行过
(lastEnd >= lastStart && +new Date() - lastEnd > ms) || // 上次运行成功后已经超过ms毫秒
(lastEnd < lastStart && +new Date() - lastStart > ms * 8) // 上次运行或未完成,后8*ms毫秒
) {
run();
} else {
if (timer) {
timer.cancel();
}
timer = that.later(run, ms, null, arguments);
}
}, {
stop: function() {
if (timer) {
timer.cancel();
timer = 0;
}
}
});
},
later: function(fn, when, context, data) {
var that = this;
when = when || 0;
var m = fn;
var d = data;
var f;
var r;
if (typeof fn === 'string') {
m = context[fn];
}
f = function() {
m.apply(context, d instanceof Array ? d : [d]);
};
r = setTimeout(f, when);
return {
id: r,
cancel: function() {
clearTimeout(r);
}
};
},
getStyles: function(element, property) {
var styles = element.ownerDocument.defaultView.getComputedStyle(element, null);
if (property) {
return styles.getPropertyValue(property) || styles[property];
}
return styles;
},
getColor: function(colorStr) {
var rgbaRegex = /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d*(?:\.\d+)?)\)$/;
var matches = colorStr.match(rgbaRegex);
if (matches && matches.length === 5) {
return [
matches[1],
matches[2],
matches[3],
matches[4]
];
}
return [];
},
extend: function (a, b) { // a 代表默认参数, b 代表传的参数
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
};
window.ZYTransparent = ZYTransparent;
})(window);