-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnavscroll.plugin.js
More file actions
67 lines (51 loc) · 1.61 KB
/
navscroll.plugin.js
File metadata and controls
67 lines (51 loc) · 1.61 KB
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
/**
* Mat Mannion <M.Mannion@warwick.ac.uk>
*/
(function($) {
var isScrollable = function(el, offset) {
offset = offset || 0;
var last = $(el).find('li:last-child');
var isScrollable = (last.position().left + last.width()) > ($(el).width() + offset);
return isScrollable;
};
$.fn.navscroll = function(options) {
var defaults = {};
if (options) {
$.extend(defaults, options);
}
// This is for multiple calls. Look for existing buttons and remove them
this.find('.navscroll').remove();
return this.filter(function() { return isScrollable(this); }).each(function() {
var list = this;
var $list = $(this);
var width = $list.width();
// move offset to a variable so we can track its value easily
var marginLeft = 0;
var prev = $('<a/>').attr({ href: '#' }).addClass('navscroll').addClass('prev').html('«').hide();
var next = $('<a/>').attr({ href: '#' }).addClass('navscroll').addClass('next').html('»');
next.click(function() {
prev.show();
if (!isScrollable(list, width)) {
next.hide();
}
marginLeft -= width;
$list.animate({
marginLeft: marginLeft
});
return false;
});
prev.click(function() {
next.show();
if (parseInt($list.css('margin-left')) + width >= 0) {
prev.hide();
}
marginLeft += width;
$list.animate({
marginLeft: marginLeft
});
return false;
});
$(this).before(prev).after(next);
});
};
})(jQuery);