-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.rotatingMenu.js
177 lines (175 loc) · 5.08 KB
/
jquery.rotatingMenu.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
/*
* Class: rotatingMenu
* Author: John Lanz (http://pixelcone.com)
* Version: 1.0
*/
(function($) {
$.rotatingMenu = {
defaults: {
maxSpeed: 500,
minSpeed: 2000,
divId: "#rmenu"
}
};
$.fn.extend({
rotatingMenu:function(config) {
var config = $.extend({}, $.rotatingMenu.defaults, config);
var speed = config.minSpeed;
var divWidth = $('div', this).width(),
divHeight = $('div', this).height(),
menuWidth = $(this).width(),
menuHeight = $(this).height(),
leftLimit = menuWidth - (divWidth + 2);
//calculating the numver of div
var tdiv = $('div', this).length,
tdivhaf = Math.round(tdiv / 2),
dHalf = Math.round(tdivhaf/2),
tdiv_odd = (tdivhaf-1) % 2;
//getting the position and distance
var topPos = (menuHeight / 2) - (divHeight / 2),
topPos_distance = (tdiv_odd)? Math.floor(topPos / dHalf) : Math.floor(topPos / (dHalf-1)),
lPos = 0,
lPos_distance = Math.floor((menuWidth - (divWidth*(tdivhaf+1))) / tdivhaf + divWidth);
//getting div bottom total and distance
var tdiv_bottom = tdiv-tdivhaf,
bdiv_odd = (tdiv_bottom-1) % 2,
bottomPos = topPos,
bhalf = Math.round(tdiv_bottom/2) + tdivhaf,
bdiv_half = Math.round(tdiv_bottom/2),
tdiv_bottom_pos = Math.floor((menuWidth - (divWidth*(tdiv_bottom+1))) / tdiv_bottom + divWidth),
bPos_distance = (bdiv_odd)? Math.floor(bottomPos / bdiv_half) : Math.floor(bottomPos / (bdiv_half-1));
var count = 0,
leftAnim = false, //left animation,
zPos = topPos, //firt top position,
zHigh = 0,
zLow = 0,
mapPos = {}; //stores the position of the menu
var rdiv = $('div', this),
rData = new Array(),
rpos = new Array();
for(i=0; i<tdiv; i++){
ids = rdiv.eq(i).attr('id');
rData[i] = ids;
}
var rEvent = this; //copy 'this' to rEvent
var rm = {
//menu positioning
menuPosition : function(){
for (i=1; i<=tdiv; i++){
if (i <= tdivhaf){
$(rEvent).children(config.divId + i).css({top: topPos, left: lPos});
mapPos['menu'+i] = {top: topPos, left: lPos};
lPos += lPos_distance;
if (i <= dHalf){
if (i == dHalf && !tdiv_odd){
}else{
topPos += topPos_distance;
}
}else{
topPos -= topPos_distance;
}
}else{
$(rEvent).children(config.divId + i).css({top: bottomPos, left: lPos});
mapPos['menu'+i] = {top: bottomPos, left: lPos};
lPos -= tdiv_bottom_pos;
if (i <= bhalf){
if (i == bhalf && !bdiv_odd){
}else{
bottomPos -= bPos_distance;
}
}else{
bottomPos += bPos_distance;
}
}
}
},
//reset whether go to left or right animation
menuReset : function(cont){
count++;
if (count == tdiv || (typeof cont != "undefined" && cont)){
if (leftAnim){
for (i=0; i<tdiv; i++){
val1 = rData[i];
if (i > 0){
rData[i] = val;
}else{
rData[i] = rData[tdiv-1];
}
val = val1;
rpos[i] = rdiv.eq(i).position();
}
}else{
for (i=tdiv-1; i>=0; i--){
val1 = rData[i];
//console.log(i);
if (i < tdiv-1){
rData[i] = val;
}else{
rData[i] = rData[0];
}
val = val1;
rpos[i] = rdiv.eq(i).position();
}
}
zHigh = 100;
zLow = 50;
for(i=0; i<tdiv; i++){
if (leftAnim && ((rpos[i].top > zPos && rpos[i].left > 0) || rpos[i].left >= leftLimit)){
zHigh--;
rdiv.eq(i).attr('id', rData[i]).css('z-index', zHigh);
}else if(!leftAnim && (rpos[i].top > zPos || rpos[i].left == 0)){
zHigh--;
rdiv.eq(i).attr('id', rData[i]).css('z-index', zHigh);
}else{
zLow--;
rdiv.eq(i).attr('id', rData[i]).css('z-index', zLow);
}
}
count = 0;
if(!cont){
this.rmAnimate();
}
}
},
//animate
rmAnimate : function(){
for (i=1; i<=tdiv; i++){
$(rEvent).children(config.divId + i).animate({top: mapPos['menu'+i].top, left: mapPos['menu'+i].left}, speed, 'linear', function(){
rm.menuReset();
});
}
}
}
rm.menuPosition();
rm.menuReset(true);
rm.rmAnimate();
var rmPos = $(rEvent).offset();
var halfMenuW = menuWidth/2;
var mouseP = Math.round(rmPos.left + halfMenuW); // if >:right <:left
$(window).resize(function(){
rmPos = $(rEvent).offset();
mouseP = Math.round(rmPos.left + halfMenuW); // if >:right <:left
});
var mousePoints = Math.floor(halfMenuW/8);
var speedMeter = Math.floor((config.minSpeed - config.maxSpeed)/8);
$(this).mousemove(function(e){
mPoint = 0;
if (mouseP > e.clientX){
leftAnim = false;
mPoint = mouseP - e.clientX;
}else{
leftAnim = true;
mPoint = e.clientX - mouseP;
}
speed = config.minSpeed - (speedMeter * Math.round(mPoint/mousePoints));
});
$('div', this).hover(function(){
$(this).siblings('div').find('span').hide();
$('span', this).show().css('display', 'block');
},function(){
$(rEvent).find('span').hide();
});
return this;
}
});
})(jQuery);