-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathjquery.doubleScroll.js
83 lines (74 loc) · 3.16 KB
/
jquery.doubleScroll.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
/*
* @name DoubleScroll
* @desc displays scroll bar on top and on the bottom of the div
* @requires jQuery, jQueryUI
*
* @author Pawel Suwala - http://suwala.eu/
* @version 0.3 (12-03-2014)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($){
$.widget("suwala.doubleScroll", {
options: {
contentElement: undefined, // Widest element, if not specified first child element will be used
topScrollBarMarkup: '<div class="suwala-doubleScroll-scroll-wrapper" style="height: 20px;"><div class="suwala-doubleScroll-scroll" style="height: 20px;"></div></div>',
topScrollBarInnerSelector: '.suwala-doubleScroll-scroll',
scrollCss: {
'overflow-x': 'scroll',
'overflow-y':'hidden'
},
contentCss: {
'overflow-x': 'scroll',
'overflow-y':'hidden'
}
},
_create : function() {
var self = this;
var contentElement;
// add div that will act as an upper scroll
var topScrollBar = $($(self.options.topScrollBarMarkup));
self.element.before(topScrollBar);
// find the content element (should be the widest one)
if (self.options.contentElement !== undefined && self.element.find(self.options.contentElement).length !== 0) {
contentElement = self.element.find(self.options.contentElement);
}
else {
contentElement = self.element.find('>:first-child');
}
// bind upper scroll to bottom scroll
topScrollBar.scroll(function(){
self.element.scrollLeft(topScrollBar.scrollLeft());
});
// bind bottom scroll to upper scroll
self.element.scroll(function(){
topScrollBar.scrollLeft(self.element.scrollLeft());
});
// apply css
topScrollBar.css(self.options.scrollCss);
self.element.css(self.options.contentCss);
// set the width of the wrappers
$(self.options.topScrollBarInnerSelector, topScrollBar).width(contentElement[0].scrollWidth);
topScrollBar.width(self.element[0].clientWidth);
},
refresh: function(){
// this should be called if the content of the inner element changed.
// i.e. After AJAX data load
var self = this;
var contentElement;
var topScrollBar = self.element.parent().find('.suwala-doubleScroll-scroll-wrapper');
// find the content element (should be the widest one)
if (self.options.contentElement !== undefined && self.element.find(self.options.contentElement).length !== 0) {
contentElement = self.element.find(self.options.contentElement);
}
else {
contentElement = self.element.find('>:first-child');
}
// set the width of the wrappers
$(self.options.topScrollBarInnerSelector, topScrollBar).width(contentElement[0].scrollWidth);
topScrollBar.width(self.element[0].clientWidth);
}
});
})(jQuery);