-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsugar.js
More file actions
241 lines (229 loc) · 7.63 KB
/
Copy pathsugar.js
File metadata and controls
241 lines (229 loc) · 7.63 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
(function(window){
window.sugar = {}; //定义全局对象;
sugar.utils = {}; //sugar的工具函数集合;
sugar.utils.getStyle = function(obj, attr){
if(obj.currentStyle){
return obj.currentStyle[obj, attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}; // 获取元素属性;
sugar.utils.setCss = function(obj, json){
for(var i in json){
obj.style[i] = json[i];
}
} //批量设置元素属性;
sugar.utils.setAttr = function(obj, json){
for(var i in json){
obj[i] = json[i];
}
}
sugar.utils.getByClassName = function(obj,name){
var allEle = obj.getElementsByTagName('*');
var arr = [];
for(var i=0,len=allEle.length; i<len; i++){
var arrClass = allEle[i].className.split(' ');
for(var j=0; j<arrClass.length; j++){
if(arrClass[j] == name){
arr.push(allEle[i]);
}
}
}
return arr;
} //通过类名获取元素;
sugar.utils.pre = function(obj){
while(obj.previousSibling.nodeType != 1 && obj.previousSibling.nodeName != 'ul'){
obj = obj.previousSibling;
}
return obj;
} //获取上一个非文本元素
sugar.utils.next = function(obj){
while(obj.nextSibling.nodeType != 1 && obj.previousSibling.nodeName != 'ul'){
obj = obj.nextSibling;
}
return obj;
} //获取下一个非文本元素;
sugar.utils.addClass = function(obj, name){
var oClass = obj.className;
if(name.constructor == Array){
for(var i=0,len=name.length; i<len; i++){
oClass += ' ' + name[i];
}
}else if(typeof name == 'string'){
oClass += ' ' + name;
}
obj.className = oClass;
} //添加类名;
sugar.utils.removeClass = function(obj, name){
//name只支持string;
var oClass = obj.className;
var arrClass = oClass.split(' ');
var arr = [];
var newClass;
for(var i=0,len=arrClass.length; i<len; i++){
if(arrClass[i] != name){
arr.push(arrClass[i]);
}
}
newClass = arr.join(' ');
obj.className = newClass;
} //删除类名;
sugar.utils.hasClass = function(obj, name){
var oClass = obj.className;
var arrClass = oClass.split(' ');
for(var i=0;i<arrClass.length; i++){
if(arrClass[i] == name){
return true;
}
}
return false;
} //检测类名;
sugar.utils.posX = function(elem){
return elem.offsetParent?elem.offsetLeft + sugar.utils.posX(elem.offsetParent):elem.offsetLeft;
} //返回元素相对文档位置;
sugar.utils.posY = function(elem){
return elem.offsetParent?elem.offsetTop + sugar.utils.posY(elem.offsetParent):elem.offsetTop;
}
sugar.gallery = function(obj){
var getStyle = sugar.utils.getStyle;
var setCss = sugar.utils.setCss;
var setAttr = sugar.utils.setAttr;
var addClass = sugar.utils.addClass;
var removeClass = sugar.utils.removeClass;
var hasClass = sugar.utils.hasClass;
var getByClassName = sugar.utils.getByClassName; //设置命名依赖;
var iCurrent = 0; //记录当前是第几张;
var beDone = true; //记录是否运动完成;
var oImg = obj.getElementsByTagName('img')[0];
var currentImage = oImg;
var oDiv = obj.parentNode;
var aLi = obj.getElementsByTagName("li");
var listBox = document.createElement('div');
var preA = document.createElement('a');
var nextA = document.createElement('a');
var oFragment = document.createDocumentFragment(); //创建文档碎片,提高性能;
setCss(obj, {"width": oImg.offsetWidth * aLi.length + 'px', "height": oImg.offsetHeight + 'px'});
setCss(oDiv, {"width": oImg.offsetWidth + 'px', "height": oImg.offsetHeight + 'px'});
setAttr(preA, {"className": "listButton preButton", "href": "#"});
preA.style.height = 43 + 'px';
preA.style.top = (oDiv.offsetHeight - parseInt(preA.style.height))/2 + 'px';
nextA.className = 'nextButton listButton';
nextA.href = "#";
nextA.style.top = preA.style.top;
listBox.className = "listBox listButton";
for(var i=0,len=aLi.length; i<len; i++){
var aA = document.createElement('a');
aLi[i].className = 'list' + i;
aLi[i].style.left = oImg.offsetWidth * i + 'px';
aA.rel = 'listA' + i;
aA.style.display = 'block';
aA.href = '#';
oFragment.appendChild(aA);
aA.className = "listA";
};
listBox.appendChild(oFragment); // 添加a;
listBox.style.width = aLi.length * 16 + 'px';
listBox.style.left = (oDiv.offsetWidth - parseInt(listBox.style.width))/2 + 'px';
oDiv.appendChild(listBox);
oDiv.appendChild(preA);
oDiv.appendChild(nextA);
getByClassName(oDiv,'listA')[0].style.background = "url(img/bullets.png) no-repeat 0 -11px"; //为第一个圆点添加点击效果;
DD_belatedPNG.fix('.listA'); //消除IE6下面的PNG不透明;
DD_belatedPNG.fix('.sugar-gallery');
DD_belatedPNG.fix('.listButton');
var alistA = getByClassName(oDiv, 'listA');
var preButton = getByClassName(oDiv, 'preButton')[0];
var nextButton = getByClassName(oDiv, 'nextButton')[0];
for(var i=0,len=alistA.length; i<len; i++){
(function(){
var iNow = i;
alistA[iNow].onclick = function(){
if(beDone){
for(var j=0,len=alistA.length; j<len; j++){
alistA[j].style.background = "url(img/bullets.png) no-repeat 0 0";
};//先清除所有的圆点点击效果;
beDone = false;
getByClassName(oDiv,'listA')[iNow].style.background = "url(img/bullets.png) no-repeat 0 -11px"
sugar.animation.flexMove(obj, {"left": -(oDiv.offsetWidth * iNow)}, function(){
beDone = true;
iCurrent = iNow;
})
}
}
})(); //设置闭包;
}
preButton.onclick = function(){
preImg(currentImage); //运动到前一张;
};
nextButton.onclick = function(){
nextImg(currentImage); //运动到后一张;
}
function nextImg(oImg){
if(beDone){ //如果上一张运动完成,才可以继续下一次运动;
beDone = false;
getByClassName(oDiv,'listA')[iCurrent].style.background = "url(img/bullets.png) no-repeat 0 0";
if(iCurrent == aLi.length -1){
sugar.animation.flexMove(obj, {"left": 0}, function(){
beDone = true;
});
iCurrent = 0;
currentImage = aLi[iCurrent];
}else{
sugar.animation.flexMove(obj, {"left": obj.offsetLeft - oDiv.offsetWidth}, function(){
beDone = true;
});
iCurrent ++;
currentImage = aLi[iCurrent];
}
getByClassName(oDiv,'listA')[iCurrent].style.background = "url(img/bullets.png) no-repeat 0 -11px";
}
}
function preImg(oImg){
if(beDone){
beDone = false;
getByClassName(oDiv,'listA')[iCurrent].style.background = "url(img/bullets.png) no-repeat 0 0";
if(iCurrent == 0){
sugar.animation.flexMove(obj, {"left": -(obj.offsetWidth - oDiv.offsetWidth)}, function(){
beDone = true;
});
iCurrent = aLi.length - 1;
}else{
sugar.animation.flexMove(obj, {"left": obj.offsetLeft + oDiv.offsetWidth}, function(){
beDone = true;
});
iCurrent --;
}
getByClassName(oDiv,'listA')[iCurrent].style.background = "url(img/bullets.png) no-repeat 0 -11px";
}
}
}; //sugar轮播图;
sugar.animation = {};
sugar.animation.flexMove = function(obj, json, fn){
var getStyle = sugar.utils.getStyle;
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var bStop = true;
for(var attr in json){
var iCur;
if(attr == 'opacity'){
iCur = parseInt(parseFloat(getStyle(obj, attr))*100);
}else{
iCur = parseInt(getStyle(obj, attr));
}
var iSpeed = (json[attr] - iCur)/8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(json[attr]!=iCur) bStop = false;
if(attr == 'opacity'){
obj.style.opacity = (iCur + iSpeed) / 100;
obj.style.filter = 'alpha(opacity:' + (iCur + iSpeed) + ')';
}else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
if(bStop){
clearInterval(obj.timer);
if(fn) fn();
}
}, 1000/60);
}
})(window, undefined);