-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.sortScroll.js
202 lines (178 loc) · 8.48 KB
/
jquery.sortScroll.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* jQuery sortScroll V1.3.0
* Sorting without moving !
* The element being sorted will stay still while the rest of the page will scroll behind it.
* https://github.com/jadus/jquery-sortScroll
* Licensed under the MIT license
*/
;
(function ($, window, document, undefined) {
"use strict";
var defaults = {
animationDuration: 1000,// duration of the animation in ms
cssEasing: "ease-in-out",// easing type for the animation
keepStill: true,// if false the page doesn't scroll to follow the element
fixedElementsSelector: ""// a jQuery selector so that the plugin knows your fixed elements (like a fixed header)
};
function SortScroll(container, options) {
this.container = $(container);
this.settings = $.extend({}, defaults, options, this.container.data());
this._sorting = false;
this.elementClass = "sort-scroll-element";
this.sortingClass = "sort-scroll-sorting";
this.buttonUpClass = "sort-scroll-button-up";
this.buttonDownClass = "sort-scroll-button-down";
this.init();
}
$.extend(SortScroll.prototype, {
init: function () {
var self = this;
//auto initialization
self.container.on("click", "." + self.buttonUpClass + ", ." + self.buttonDownClass, function (event) {
event.preventDefault();
var button = $(this),
elementCollection = self.container.find("." + self.elementClass),
initialOrder = elementCollection.index(button.closest("." + self.elementClass)),
sortDirection;
sortDirection = button.hasClass(self.buttonUpClass) ? -1 : 1;
self.sortElement(initialOrder, sortDirection, self.settings.keepStill);
})
},
sortElement: function (initialOrder, sortDirection, keepStill) {
sortDirection = sortDirection === -1 ? sortDirection : 1;
keepStill = !keepStill ? false : this.settings.keepStill;
var self = this,
elementCollection = self.container.find("." + self.elementClass),
maxOrder = elementCollection.length - 1,
destinationOrder = initialOrder + sortDirection;
//don't move if element is at one end of the list
if (destinationOrder < 0 || destinationOrder > maxOrder) {
return false;
}
//if method is called again before animation end, we wait and we call it again on animation end
if (self._sorting) {
self.container.one("sortScroll.sortEnd", function (event, element, initialOrder, destinationOrder) {
self.sortElement(destinationOrder, sortDirection, keepStill);
});
return false;
}
elementCollection.each(function(){
$(this).css("top", 0);
});
var element = elementCollection.eq(initialOrder),
elementHeight = element.outerHeight(true),
initialElementY = element.offset().top,
otherElement = elementCollection.eq(destinationOrder),
otherElementHeight = otherElement.outerHeight(true),
initialOtherElementY = otherElement.offset().top,
finalElementY = otherElement.offset().top,
finalOtherElementY = initialOtherElementY + elementHeight,
modifyElementRelative = 0,
modifyOtherElementRelative = parseInt(otherElement.css("margin-top"),10);
if (sortDirection > 0) {
finalElementY = initialElementY + otherElementHeight;
finalOtherElementY = initialElementY;
modifyElementRelative = parseInt(element.css("margin-top"),10);
modifyOtherElementRelative = 0;
}
var relativeElementY = finalElementY - initialElementY - modifyElementRelative,
relativeOtherElementY = finalOtherElementY - initialOtherElementY - modifyOtherElementRelative,
duration = self.settings.animationDuration,
cssEasing = self.settings.cssEasing,
body = $("body"),
initialCssZIndex = element.css("z-index"),
elementDfd = $.Deferred(),
otherElementDfd = $.Deferred(),
bodyDfd = $.Deferred(),
transitionEndEvent = "transitionend webkitTransitionEnd msTransitionEnd oTransitionEnd",
styleString = "style",
sortingZIndex;
sortingZIndex = (initialCssZIndex === "auto") ? 2 : initialCssZIndex + 1;
//animating
self.container.trigger("sortScroll.sortStart", [element, initialOrder, destinationOrder]);
self._sorting = true;
element.data(styleString,element.attr(styleString));
otherElement.data(styleString,otherElement.attr(styleString));
element.add(otherElement).css({
"position" : "relative",
"transition" : duration+"ms "+cssEasing
})
element.addClass(self.sortingClass).css({
"z-index": sortingZIndex,
"top": relativeElementY+"px",
});
otherElement.css({
"top": relativeOtherElementY+"px",
});
if(keepStill) {
var initialScroll = $(window).scrollTop(),
finalScroll = initialScroll + relativeElementY,
maxScroll = Math.max(0, document.documentElement.scrollHeight - document.documentElement.clientHeight),
fixedElements = $(self.settings.fixedElementsSelector).filter(function(){
return $(this).css("position") === "fixed";
});
finalScroll = Math.min(finalScroll, maxScroll);
finalScroll = Math.max(finalScroll, 0);
body.data(styleString,body.attr(styleString)||"").css({
"transition" : duration+"ms "+cssEasing,
"margin-top": ( initialScroll - finalScroll ) + "px"
});
fixedElements.each(function(){
$(this).data(styleString,$(this).attr(styleString) || "")
}).css({
"transition" : duration+"ms "+cssEasing,
"margin-top": -( initialScroll - finalScroll ) + "px"
})
}
//after animation
element.one(transitionEndEvent, function () {
elementDfd.resolve();
})
otherElement.one(transitionEndEvent, function () {
otherElementDfd.resolve();
})
body.one(transitionEndEvent, function () {
bodyDfd.resolve();
})
$.when(elementDfd, otherElementDfd, bodyDfd).done(function () {
//back to initial style
element.attr(styleString,element.data(styleString));
otherElement.attr(styleString,otherElement.data(styleString));
body.attr(styleString,body.data(styleString));
if(keepStill) {
$("html, body").scrollTop(finalScroll + ($(window).scrollTop() - initialScroll));
fixedElements.each(function(){
$(this).attr(styleString,$(this).data(styleString));
});
}
element.removeClass(self.sortingClass);
//modifiyng dom
if(sortDirection > 0){
otherElement.after(element);
}else{
otherElement.before(element);
}
self._sorting = false;
self.container.trigger("sortScroll.sortEnd", [element, initialOrder, destinationOrder]);
return;
});
}
});
$.fn.sortScroll = function (opt) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function () {
var item = $(this), instance = item.data("sortScroll");
if (!instance) {
item.data("sortScroll", new SortScroll(this, opt));
} else {
if (typeof opt === "string") {
instance[opt].apply(instance, args);
}
}
});
};
$(".sort-scroll-container").each(function () {
$(this).sortScroll();
});
})(jQuery, window, document);