原文链接:http://www.cnblogs.com/henw/archive/2011/12/19/2293585.html
需求:自定义一个弹出登陆框的界面,主要特点有隐藏hide(),显示show(),浏览器窗口改变大小触发事件resize(),计算屏幕居中位置等功能。
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
|
//设置物体居中
Base.prototype.center=
function
(width,height){
var
top=(document.documentElement.clientHeight-height)/2;
var
left=(document.documentElement.clientWidth-width)/2;
for
(
var
i=0;i<
this
.elements.length;i++){
this
.elements[i].style.top=top+
'px'
;
this
.elements[i].style.left=left+
'px'
;
}
return
this
;
}
//当浏览器改变窗口大小的时候,触发事件
Base.prototype.resize=
function
(){
window.onresize=fn;
return
this
;
}
/*
前台调用
var login=$().getId('login');
//登录框
login.center(350,250).resize(function(){
login.center(350,250);
});
//弹出登录框
$().getClass('login').click(function(){
login.css('display','block');
});
//登录框关闭按钮
$().getClass('close').click(function(){
login.css('display','none');
});
*/
|
基础库:
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
|
//前台调用
var
$ =
function
(_this) {
return
new
Base(_this);
}
//基础库
function
Base(_this) {
//创建一个数组,来保存获取的节点和节点数组
this
.elements = [];
if
(_this != undefined) {
//_this是一个对象,undefined也是一个对象,区别与typeof返回的带单引号的'undefined'
this
.elements[0] = _this;
}
}
//获取ID节点
Base.prototype.getId =
function
(id) {
this
.elements.push(document.getElementById(id));
return
this
;
};
//获取元素节点数组
Base.prototype.getTagName =
function
(tag) {
var
tags = document.getElementsByTagName(tag);
for
(
var
i = 0; i < tags.length; i ++) {
this
.elements.push(tags[i]);
}
return
this
;
};
//获取CLASS节点数组
Base.prototype.getClass =
function
(className, idName) {
var
node =
null
;
if
(arguments.length == 2) {
node = document.getElementById(idName);
}
else
{
node = document;
}
var
all = node.getElementsByTagName(
'*'
);
for
(
var
i = 0; i < all.length; i ++) {
if
(all[i].className == className) {
this
.elements.push(all[i]);
}
}
return
this
;
}
//获取某一个节点
Base.prototype.getElement =
function
(num) {
var
element =
this
.elements[num];
this
.elements = [];
this
.elements[0] = element;
return
this
;
};
//设置CSS
Base.prototype.css =
function
(attr, value) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
if
(arguments.length == 1) {
if
(
typeof
window.getComputedStyle !=
'undefined'
) {
//W3C
return
window.getComputedStyle(
this
.elements[i],
null
)[attr];
}
else
if
(
typeof
this
.elements[i].currentStyle !=
'undeinfed'
) {
//IE
return
this
.elements[i].currentStyle[attr];
}
}
this
.elements[i].style[attr] = value;
}
return
this
;
}
//添加Class
Base.prototype.addClass =
function
(className) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
if
(!
this
.elements[i].className.match(
new
RegExp(
'(\s|^)'
+className +
'(\s|$)'
))) {
this
.elements[i].className +=
' '
+ className;
}
}
return
this
;
}
//移除Class
Base.prototype.removeClass =
function
(className) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
if
(
this
.elements[i].className.match(
new
RegExp(
'(\s|^)'
+className +
'(\s|$)'
))) {
this
.elements[i].className =
this
.elements[i].className.replace(
new
RegExp(
'(\s|^)'
+className +
'(\s|$)'
),
' '
);
}
}
return
this
;
}
//添加link或style的CSS规则
Base.prototype.addRule =
function
(num, selectorText, cssText, position) {
var
sheet = document.styleSheets[num];
if
(
typeof
sheet.insertRule !=
'undefined'
) {
//W3C
sheet.insertRule(selectorText +
'{'
+ cssText +
'}'
, position);
}
else
if
(
typeof
sheet.addRule !=
'undefined'
) {
//IE
sheet.addRule(selectorText, cssText, position);
}
return
this
;
}
//移除link或style的CSS规则
Base.prototype.removeRule =
function
(num, index) {
var
sheet = document.styleSheets[num];
if
(
typeof
sheet.deleteRule !=
'undefined'
) {
//W3C
sheet.deleteRule(index);
}
else
if
(
typeof
sheet.removeRule !=
'undefined'
) {
//IE
sheet.removeRule(index);
}
return
this
;
}
//设置innerHTML
Base.prototype.html =
function
(str) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
if
(arguments.length == 0) {
return
this
.elements[i].innerHTML;
}
this
.elements[i].innerHTML = str;
}
return
this
;
}
//设置鼠标移入移出方法
Base.prototype.hover =
function
(over, out) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
this
.elements[i].onmouseover = over;
this
.elements[i].onmouseout = out;
}
return
this
;
};
//设置显示
Base.prototype.show =
function
() {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
this
.elements[i].style.display =
'block'
;
}
return
this
;
}
//设置隐藏
Base.prototype.hide =
function
() {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
this
.elements[i].style.display =
'none'
;
}
return
this
;
}
//设置物体居中
Base.prototype.center =
function
(width, height) {
var
top = (document.documentElement.clientHeight - height) / 2;
var
left = (document.documentElement.clientWidth - width) / 2;
for
(
var
i = 0; i <
this
.elements.length; i ++) {
this
.elements[i].style.top = top +
'px'
;
this
.elements[i].style.left = left +
'px'
;
}
return
this
;
}
//触发点击事件
Base.prototype.click =
function
(fn) {
for
(
var
i = 0; i <
this
.elements.length; i ++) {
this
.elements[i].onclick = fn;
}
return
this
;
}
//触发浏览器窗口事件
Base.prototype.resize =
function
(fn) {
window.onresize = fn;
return
this
;
}
|
本文转自 小旭依然 51CTO博客,原文链接:http://blog.51cto.com/xuyran/1886527