-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVQuery.js
More file actions
306 lines (264 loc) · 6.9 KB
/
Copy pathVQuery.js
File metadata and controls
306 lines (264 loc) · 6.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
function addEventLoad(obj) {
var onad = window.onload;
if (typeof window.onload != 'function') {
window.onload = obj;
} else {
window.onload = function() {
onad();
obj();
}
}
}
function myAddEvent(obj, sEv, fn) {
if (obj.attachEvent) {
obj.attachEvent('on' + sEv, function() {
if (false == fn.call(obj)) { //解决IE事件绑定的this问题!
event.cancelBubble = true; //阻止冒泡
return false;
}
});
} else {
obj.addEventListener(sEv, function() {
if (false == fn.call(obj)) {
event.cancelBubble = true; //阻止冒泡
event.preventDefault();
}
}, false);
}
}
function VQuery(vArg) {
this.elements = [];
switch (typeof vArg) {
case 'function':
myAddEvent(window, 'load', vArg);
break;
case 'string':
switch (vArg.charAt(0)) {
case "#":
this.elements.push(document.getElementById(vArg.slice(1)));
break;
case ".":
this.elements = document.getElementsByClassName(vArg.slice(1));
break;
default: //tagName
this.elements = document.getElementsByTagName(vArg);
}
break;
case 'object':
this.elements.push(vArg);
}
}
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
function getByClass(oParent, sClass) {
var aEle = oParent.getElementsByTagName('*');
var aResult = [];
for (var i = 0; i < aEle.length; i++) {
if (aEle[i].className == sClass) {
aResult.push(aEle[i]);
}
}
return aResult;
}
VQuery.prototype.click = function(fn) {
for (var i = 0; i < this.elements.length; i++) {
myAddEvent(this.elements[i], 'click', fn);
}
return this; //链式操作,返回this
}
function $(vArg) {
return new VQuery(vArg);
}
VQuery.prototype.show = function() {
for (var i = 0; i < this.elements.length; i++) {
this.elements[i].style.display = 'block';
}
return this; //链式操作,返回this
}
VQuery.prototype.hide = function() {
for (var i = 0; i < this.elements.length; i++) {
this.elements[i].style.display = 'none';
}
return this; //链式操作,返回this
}
VQuery.prototype.hover = function(fnOver, fnOut) {
for (var i = 0; i < this.elements.length; i++) {
myAddEvent(this.elements[i], 'mouseover', fnOver);
myAddEvent(this.elements[i], 'mouseout', fnOut);
}
return this; //链式操作,返回this
}
//支持这种形式$('div').css({width:'300px',height:'300px', background: 'green'})
VQuery.prototype.css = function(attr, value) {
if (arguments.length == 2) {
for (var i = 0; i < this.elements.length; i++) {
this.elements[i].style[attr] = value;
}
} else {
if (typeof attr == 'string') {
return getStyle(this.elements[0], attr);
} else {
for (var i = 0; i < this.elements.length; i++) {
for (var k in attr) {
this.elements[i].style[k] = attr[k];
}
}
}
}
return this; //链式操作,返回this
}
//toggel函数,循环执行,第一次点执行第一个函数,第二次执行第二个..
VQuery.prototype.toggle = function() {
var _arguments = arguments;
for (var i = 0; i < this.elements.length; i++) {;
(function(obj) {
var count = 0;
obj.onclick = function() {
_arguments[count % _arguments.length]();
count++;
}
}(this.elements[i]));
}
return this; //链式操作,返回this
}
//获取元素属性,也可以为元素属性赋值!
VQuery.prototype.attribute = function(attr, value) {
if (arguments.length == 2) {
for (var i = 0; i < this.elements.length; i++) {
this.elements[i][attr] = value;
}
} else {
return this.elements[0][attr];
}
return this; //链式操作,返回this
}
//假设选中了10个div,eq(5)就是获取第六个!
//返回的时候注意,这里是返回一个可以用JQuery选择的元素.
VQuery.prototype.eq = function(n) {
return $(this.elements[n]);
}
function appendArr(arr1, arr2) {
for (var i = 0; i < arr2.length; i++) {
arr1.push(arr2[i]);
}
}
//选择父级下的子元素..
VQuery.prototype.find = function(str) {
var aResult = [];
for (var i = 0; i < this.elements.length; i++) {
switch (str.charAt(0)) {
case '.':
var temp = this.elements[i].getElementsByClassName(str.slice(1))
//数组和其他HTMLCollaction之间不能使用concat
// aResult = aResult.concat(temp);
appendArr(aResult, temp);
break;
default:
temp = this.elements[i].getElementsByTagName(str);
// aResult = aResult.concat(temp);
appendArr(aResult, temp);
}
}
var VQuery_temp = $();
VQuery_temp.elements = aResult;
return VQuery_temp;
}
function getIndex(obj) {
var arr = obj.parentNode.children;
for (var i = 0; i < arr.length; i++) {
if (arr[i] == obj) {
return i;
}
}
}
VQuery.prototype.index = function() {
return getIndex(this.elements[0]);
}
// $(function(){
// $('input').click(function(){
// alert($(this).index());
// })
// })
//return false ---> 阻止冒泡和默认事件
VQuery.prototype.bind = function(sEv, fn) {
for (var i = 0; i < this.elements.length; i++) {
myAddEvent(this.elements[i], sEv, fn);
}
}
VQuery.prototype.drag = function() {
for (i = 0; i < this.elements.length; i++) {
temp(this.elements[i]);
}
function temp(oDiv) {
oDiv.onmousedown = function(ev) {
var oEvent = ev || event;
var disX = oEvent.clientX - oDiv.offsetLeft;
var disY = oEvent.clientY - oDiv.offsetTop;
document.onmousemove = function(ev) {
var oEvent = ev || event;
oDiv.style.left = oEvent.clientX - disX - 10 + 'px';
oDiv.style.top = oEvent.clientY - disY - 10 + 'px';
};
document.onmouseup = function() {
document.onmousemove = null;
document.onmouseup = null;
};
};
}
}
VQuery.prototype.extend = function(name, fn) {
VQuery.prototype[name] = fn;
}
VQuery.prototype.animate = function(json) {
for (i = 0; i < this.elements.length; i++) {
startMove(this.elements[i], json);
}
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, json, fn) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var bStop = true; //这一次运动就结束了——所有的值都到达了
for (var attr in json) {
//1.取当前的值
var iCur = 0;
if (attr == 'opacity') {
iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);
opacity_value = parseInt(parseFloat(json[attr]) * 100);
} else {
iCur = parseInt(getStyle(obj, attr));
opacity_value = parseInt(json[attr]);
}
//2.算速度
var iSpeed = (parseInt(opacity_value) - iCur) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
//3.检测停止
if (iCur != opacity_value) {
bStop = false;
}
if (attr == 'opacity') {
obj.style.filter = 'alpha(opacity:' + (iCur + iSpeed) + ')';
obj.style.opacity = (iCur + iSpeed) / 100;
} else {
obj.style[attr] = iCur + iSpeed + 'px';
}
}
if (bStop) {
clearInterval(obj.timer);
if (fn) {
fn();
}
}
}, 30)
}
}