-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.js
107 lines (93 loc) · 2.8 KB
/
style.js
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
/*
样式相关
Thanks:Coco
*/
/**
* [genRandomColor description] 产生随机颜色
* @return {String} 随机rgb格式的颜色像素值
*/
function genRandomColor() {
return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
}
/**
* [genRandomHEX description] 生成随机HEX色值
* @return {String} 随机HEX色值
*/
function genRandomHEX() {
return "#" + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0");
}
/**
* [addStyle description] 动态添加样式
* @param css {String} css代码
* @return {Null} 无返回
*/
function addStyle(css) {
var style = document.createElement("style");
style.type = "text/css";
try {
style.appendChild(document.createTextNode(css));
} catch (ex) {
style.styleSheet.cssText = css;
}
document.getElementsByTagName('head')[0].appendChild(style);
}
/**
* [addStyle description] requestAnimationFrame初始化
* @return {Null} 无返回
*/
function initRequestAnimationFrame(){
window.requestAnimationFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
setTimeout(callback, 1000 / 60);
}
})();
}
/**
* [hexToRgba description] 将hex格式像素值转换成rgba格式
* @param color {String} hex格式像素值
* @param opacity {Number} 透明度
* @return {String} 返回rgba格式像素值
*/
function hexToRgba(color, opacity) {
color = color.replace('#', '');
if (color.length === 3) {
color = color.charAt(0) + color.charAt(0) + color.charAt(1) + color.charAt(1) + color.charAt(2) + color.charAt(2);
}
var r = parseInt(color.slice(0, 2), 16);
var g = parseInt(color.slice(2, 4), 16);
var b = parseInt(color.slice(4, 6), 16);
return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + opacity + ')';
}
/**
* [getPageHeight description] 获取页面高度
* @return {Number} 页面高度
*/
function getPageHeight(){
var g = document, a = g.body, f = g.documentElement, d = g.compatMode == "BackCompat"
? a
: g.documentElement;
return Math.max(f.scrollHeight, a.scrollHeight, d.clientHeight);
}
/**
* [getPageWidth description] 获取页面宽度
* @return {Number} 页面宽度
*/
function getPageWidth(){
var g = document, a = g.body, f = g.documentElement, d = g.compatMode == "BackCompat"?
a: g.documentElement;
return Math.max(f.scrollWidth, a.scrollWidth, d.clientWidth);
}
/**
* [getPageViewWidth description] 获取页面可视宽度
* @return {Number} 页面可视宽度
*/
function getPageViewWidth(){
var d = document, a = d.compatMode === "BackCompat" ?
d.body: d.documentElement;
return a.clientWidth;
}