-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
205 lines (189 loc) · 4.45 KB
/
util.js
File metadata and controls
205 lines (189 loc) · 4.45 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* 判断是否为数组
*/
var is_array = function(value) {
return Object.prototype.toString.apply(value) === '[object Array]';
};
/**
* 去空格方法
*/
String.prototype.trim = function() {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
/**
* 替换被{}包含的字符串
*/
function substitute(str, sub) {
return str.replace(/\{(.+?)\}/g, function($0, $1) {
return $1 in sub ? sub[$1] : $0;
});
}
/**
* 懒加载
*/
function imgLazyLoad() {
var imgs = $('.J_shop-list img'),
WIN = $(window);
WIN.on('scroll', function() {
imgs.each(function() {
var self = $(this),
imgSrc = self.attr('data-src');
if (self.offset().top < WIN.height() + WIN.scrollTop() && imgSrc) {
self.removeAttr('data-src');
}
});
}).trigger('scroll');
}
/**
* 获取cookie
*/
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
/**
* 获取地址栏参数
*/
function getQueryStringByName(name) {
var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
if (result == null || result.length < 1) {
return "";
}
return result[1];
}
/**
* 数组篇
*/
/**
* 字符串处理
*/
function StringBuffer() {
var buffer = [],
size = 0;
//追加字符串
this.append = function(str) {
if(str !== null){
buffer.push(str);
size ++;
}
};
//返回字符串
this.toString = function() {
return buffer.join("");
};
//清空
this.clear = function(key) {
size = 0;
buffer = [];
};
//返回数组大小
this.size = function() {
return size;
};
//返回数组
this.toArray = function() {
return buffer;
};
//倒序返回字符串
this.doReverse = function() {
var str = buffer.join('');
str = str.split('');
return str.reverse().join('');
};
}
/**
* 指定下标添加元素
*/
Array.prototype.insert = function(index, item) {
this.splice(index, 0, item);
};
/**
* 把数组转换成特定符号分割的字符串
*/
function arrayToString(arr, separator) {
if (!separator)
separator = "";
return arr.join(separator);
}
/**
* 查找数组包含的字符串
*/
function arrayFindString(arr, string) {
var str = arr.join("");
return str.indexOf(string);
}
/**
* 构造一个单位矩阵
*/
Array.identity = function(n) {
var i, mat = Array.matrix(n, n, 0);
for (i = 0; i < n; i++) {
mat[i][i] = 1;
}
return mat;
};
/**
* 构造一个 x*y 默认值为z的矩阵数组
*/
Array.matrix = function(x, y, z) {
var a, i, j, mat = [];
for (i = 0; i < x; i++) {
a = [];
for (j = 0; j < y; j++) {
a[j] = z;
}
mat[i] = a;
}
return mat;
};
/**
* 去除相邻的重复字符串
*/
Array.prototype.unique = function() {
var res = [],
json = {};
for (var i = 0; i < this.length; i++) {
if (!json[this[i]]) {
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
};
/**
* [Clock]
* @param clockDiv [传入的div对象]
*/
function Clock(clockDiv) {
this.clockDiv = clockDiv;
this.getCurrentDate = function() {
//获取当前日期
var currDate = new Date();
//分别获取 年、月、日、时、分、秒
var currDateTime = currDate.getFullYear();
//getYear();在chrome和IE9以上的版本输出的是距离1900多少年而非实际年数
//改为用getFullYear();
currDateTime += "-";
currDateTime += (currDate.getMonth() + 1);
currDateTime += "-";
currDateTime += currdate.getDate();
currDateTime += " ";
currDateTime += currDate.getHours();
currdateTime += ":";
currDateTime += currDate.getMinutes();
currDateTime += ":";
currDateTime += currDate.getSeconds();
//将当前时间赋值到div对象中
this.clockDiv.innerHTML = currDateTime;
};
}