-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcascadingDivs.js
163 lines (136 loc) · 5.84 KB
/
cascadingDivs.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
/* Copyright (c) 2013 Giang Nguyen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Internal-use classes
const CASCADE_DIV_CLASS = 'cascade-div';
const SLIDED_DIV_CLASS = 'cascade-div-slided';
const SELECTED_DIV_CLASS = 'cascade-div-selected';
// True when a div is sliding, used for preventing multiple slides (which will mess up the whole thing)
// TODO: Can this be fixed?
var g_isSliding = false;
jQuery.prototype.cascadingDivs = function(options) {
// Optional parameters
var options = options || {};
const DIV_SELECTOR = options.divSelector || 'div';
const SLIDE_TIME = options.slideTime || 0.5;
// Initial variables
var divsContainer = jQuery(this);
var divs = divsContainer.children(DIV_SELECTOR);
var maxHeight = 0;
var maxWidth = 0;
divs.each(function(index, div){
div = jQuery(div);
if (div.height() > maxHeight) {
maxHeight = div.height();
}
if (div.width() > maxWidth) {
maxWidth = div.width();
}
});
var collapsedWidth = (divsContainer.width() - maxWidth) / (divs.length - 1);
// Arrange each div to overlap the next
divsContainer.css('position','relative');
divs.css('position','absolute');
var highestZIndex = divs.length - 1;
divs.each(function(index, div){
div = jQuery(div);
div.css('left', (collapsedWidth*index) + 'px');
div.css('z-index', highestZIndex - index);
});
// Stretch divs & container's height to be the same as the tallest div
divsContainer.height(maxHeight);
divs.height(maxHeight);
// Hide slided-out-of-container div parts if any
divsContainer.css('overflow','hidden');
// Mark internal-use CSS
divs.addClass(CASCADE_DIV_CLASS);
divs.first().addClass(SELECTED_DIV_CLASS);
// When click on a div
divs.click(function(e) {
var clickedDiv = jQuery(this);
// Ignore if selected or another div is still sliding
if (clickedDiv.hasClass(SELECTED_DIV_CLASS) || g_isSliding) {
return;
}
// Prepare some vars
var slideAmount = clickedDiv.width() - collapsedWidth;
var prevDiv = clickedDiv.prev();
var nextDiv = clickedDiv.next();
// Slide left/right divs, depending on which side of this div the currently selected one is on
// If currently selected div is on the left
if (prevDiv.hasClass(CASCADE_DIV_CLASS) && !prevDiv.hasClass(SLIDED_DIV_CLASS)) {
// Slide all divs on the left including the currently selected
slideDiv(prevDiv, slideAmount);
// If currently selected div is on the right
} else if (nextDiv.hasClass(CASCADE_DIV_CLASS) && clickedDiv.hasClass(SLIDED_DIV_CLASS)) {
// Unslide this div + all divs on the right except the currently selected
unslideDiv(clickedDiv, slideAmount);
}
// Mark this div as selected
jQuery('.'+SELECTED_DIV_CLASS).removeClass(SELECTED_DIV_CLASS);
clickedDiv.addClass(SELECTED_DIV_CLASS);
});
// Set sliding time
divs.css('transition','left '+SLIDE_TIME+'s');
divs.css('-webkit-transition','left '+SLIDE_TIME+'s');
// End-sliding callback
divs.bind( 'transitionend', onTransitionEnd );
divs.bind( 'webkitTransitionEnd', onTransitionEnd );
divs.bind( 'oTransitionEnd', onTransitionEnd );
}
function onTransitionEnd(e) {
// Allow selecting another div
g_isSliding = false;
}
function slideDiv(div, amount) {
// Slide the previous div first if exists & haven't reached thecurrently selected yet
if (div.prev().hasClass(CASCADE_DIV_CLASS) && !div.hasClass(SELECTED_DIV_CLASS)) {
slideDiv(div.prev(), amount);
}
// Slide this div
repositionDiv(div, -amount);
// Mark as slided
div.addClass(SLIDED_DIV_CLASS);
}
function unslideDiv(div, amount) {
// Unslide the next div first if exists & haven't reached thecurrently selected yet
if (div.next().hasClass(CASCADE_DIV_CLASS) && !div.next().hasClass(SELECTED_DIV_CLASS)) {
unslideDiv(div.next(), amount);
}
// Unslide this div
repositionDiv(div, amount);
// Mark as unslided
div.removeClass(SLIDED_DIV_CLASS);
}
function repositionDiv(div, amount) {
// Move this div
// Get the current position
var currentPos = div.css('left');
// Do some string processing
currentPos = currentPos.replace('px','');
if (currentPos == '') {
currentPos = 0;
} else {
currentPos = Number(currentPos);
}
// Change current position
div.css('left', (currentPos+amount)+'px');
// Prevent selecting another div while this one is sliding
g_isSliding = true;
}