-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.bannerRotate-1.4.js
187 lines (183 loc) · 5.5 KB
/
jquery.bannerRotate-1.4.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
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
// bannerRotate, Version 1.4
// Copyright (c) Oct 3, 2010 adardesign.com
// bannerRotate is freely distributable under the terms of an MIT-style license
// This means you are free to use the code, but please leave this copyright notice intact
// Added relToSrc performance....
(function ($) {
$.fn.bannerRotate = function (opt) {
var defaults = {
speed: 500,
secSpeed: 500,
interval: 4000,
shouldHoverSwitch: false,
builedNav: function (a) {
var i = 0,
container = $("<div class='controlsContainer'>").hide(),
navElements = "";
for (i = 0; i < a; i++) {
navElements += "<a href='#'>";
}
container.html(navElements);
return container;
},
stopAnimation: function (b) {
b.stop(true, true);
},
stopInterval: function (interv) {
if (!interv) {
return;
}
window.clearInterval(interv);
},
stopTimeout: function (timeOut) {
if (!timeOut) {
return;
}
window.clearTimeout(timeOut);
},
addHoverUsability: {
active: true,
getMiliseconds: function () {
return new Date().getTime();
},
timeLapsed: function (initTime) {
var timeNow = this.getMiliseconds();
return timeNow - initTime;
}
}
},
options = $.extend(defaults, opt);
return this.each(function (i, el) {
// EB added Dec 22 2010, in case of 1 banner only
if ($(this).children().length > 1) {
var bannerContainer = $(el),
banners = bannerContainer.children(),
bannersLength = banners.length,
relToSrced,
relToSrc = function (init) {
if (relToSrced) {
return false;
}
var rel,
img,
innerRelToSrc = function(collection){
collection.each(function(){
var jThis = $(this);
if(jThis.is("a")){
rel = jThis.attr("rel")
rel && jThis.css("background-image", rel)
}else{
// if its a img itself
if(jThis.is("img")){
img = jThis;
}else{
img = jThis.find("img");
}
rel = img.attr("rel");
rel && img.attr("src", rel)
}
})
}
if(init){
innerRelToSrc(banners.eq(0))
return
}else{
innerRelToSrc(banners.slice(1))
relToSrced = true;
}
},
sourceFirst = relToSrc(true),
bannerNav = bannerContainer.append((options.builedNav(bannersLength).fadeIn(options.secSpeed))).find(".controlsContainer a"),
next = 1,
last = 0,
indexOfClicked,
updateValues = function (clicked) {
if (clicked) {
last = indexOfClicked;
next = (last + 1) % bannersLength;
} else {
last = next;
next = (next + 1) % bannersLength;
}
},
initTime = 0,
fadeIntervalID, newSchedFadeInterval, newSschedcallDoFade, fadeTimeout,
fadeInterval = function () {
if (options.addHoverUsability.active) {
do{
initTime = options.addHoverUsability.getMiliseconds();
} while (0);
}
banners.eq(next).fadeIn(options.speed);
banners.eq(last).fadeOut(options.speed);
bannerNav.eq(last).removeClass("active");
bannerNav.eq(next).addClass("active");
updateValues();
},
schedFadeInterval = function () {
fadeIntervalID = setInterval(fadeInterval, options.interval);
};
// prevents children elements to trigger mouseout from rotating banner
//TODO: figure out why jQuery 1.4.3 doesnt respond when false passed in as the callback
banners.children().bind("mouseover", function (e) {
e.preventDefault();
});
bannerContainer.hover(function () {
options.stopInterval(fadeIntervalID);
options.stopAnimation(banners);
}, function () {
if (options.addHoverUsability.active) {
if (newSchedFadeInterval) {
options.stopTimeout(newSchedFadeInterval);
options.stopTimeout(newSschedcallDoFade);
}
var timeLapsed = options.addHoverUsability.timeLapsed(initTime);
if (timeLapsed >= options.interval) {
schedFadeInterval();
fadeInterval();
} else {
newSchedFadeInterval = setTimeout(fadeInterval, options.interval - timeLapsed);
newSschedcallDoFade = setTimeout(schedFadeInterval, (options.interval - timeLapsed) + options.speed);
}
} else {
schedFadeInterval();
}
})
// delegate (to boost performance)
.find(".controlsContainer").delegate("a", "click", function () {
!relToSrced && relToSrc(false);
var clickedNav = $(this);
if (clickedNav.hasClass("active")) {
return false;
};
clickedNav.siblings().removeClass("active");
clickedNav.addClass("active");
indexOfClicked = bannerNav.index(clickedNav);
banners.eq(last).fadeOut(options.secSpeed);
banners.eq(indexOfClicked).fadeIn(options.secSpeed, function () {
banners.eq(indexOfClicked).css("opacity", 1);
});
if (options.addHoverUsability.active) {
initTime = options.addHoverUsability.getMiliseconds();
}
updateValues(true);
return false;
}).delegate("a", "mouseover", function () {
if (options.shouldHoverSwitch) {
$(this).click();
}
});
}
// init things to do
bannerNav.eq(0).addClass("active");
banners.not(":eq(0)").hide();
schedFadeInterval();
setTimeout(function(){relToSrc()}, options.interval / 2);
if (options.addHoverUsability.active) {
do {
initTime = options.addHoverUsability.getMiliseconds();
} while (0);
}
});
};
})(jQuery);