-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarker.js
150 lines (133 loc) · 4.89 KB
/
barker.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
module.exports = function ($) {
"use strict";
$.fn.barkerCarousel = function (option) {
var $carousel = this;
$.extend($.easing, {
def: 'easeOutQuad',
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
}});
this.show_carousel_pager = function() {
var $barker = $('.barker');
var pager = document.createElement('div');
var page_left = document.createElement('a');
$(page_left).html('œ');
var page_right = document.createElement('a');
$(page_right).html('š');
//for style
$(pager).addClass('barker-carousel-pager');
$(page_left)
.addClass('page-left')
.appendTo(pager)
.addClass('disabled');
$(page_right)
.addClass('page-right')
.appendTo(pager);
$barker.append(pager);
};
function hide_carousel_pager() {
var $pager = $('.barker-carousel-pager');
if($pager.length > 0) {
$pager.remove();
}
}
function carousel_scroll_position(direction) {
// Which numbers do we need for maths?
var content_width = $carousel[0].scrollWidth;
var window_width = $carousel.width();
var new_position = 0;
var cur_position = $carousel.scrollLeft();
var $items = $carousel.find('td');
// do MATHS!!!
if(direction == 'right') {
new_position = $carousel.scrollLeft() + window_width;
if(new_position + window_width > content_width)
return content_width - window_width;
else
// Make sure new_position isn't in the middle of an image
$items.each(function() {
if(new_position > this.offsetLeft
&& new_position < this.offsetLeft + $(this).outerWidth()) {
new_position = this.offsetLeft;
if(new_position == cur_position)
new_position = this.offsetLeft + $(this).outerWidth();
}
});
return new_position;
} else if(direction == 'left') {
new_position = $carousel.scrollLeft() - window_width;
if(new_position < 0)
return 0;
else
// Make sure new_position isn't in the middle of an image
$items.each(function() {
if(new_position > this.offsetLeft
&& new_position < this.offsetLeft + $(this).outerWidth())
new_position = this.offsetLeft + $(this).outerWidth();
if(new_position == cur_position)
new_position = this.offsetLeft;
});
return new_position;
}
}
var that = this;
return this.each(function () {
//Disable touch support to match the rest of NGUX
//var touchSupport = 'ontouchstart' in document.documentElement;
var touchSupport = false;
// are the contents of the carousel larger than the space available?
var carousel_is_scrolly = ($carousel[0].scrollWidth != $carousel.width());
if(carousel_is_scrolly) {
// If we're on a touch device, enable overflow scroll so you can
// use your fingers instead of the buttons!
if(touchSupport) {
$carousel
.css('overflow-x', 'scroll')
.css('-webkit-overflow-scrolling', 'touch');
}
// Show the carousel arrows
that.show_carousel_pager();
//for js
var $pager = $('.barker-carousel-pager');
var $pageLeft = $('.page-left');
var $pageRight = $('.page-right');
// TODO if page loads with items scrolled to left and class disabled
// it's unusable: $('.barker-carousel-pager .page-left').addClass('disabled');
// TODO: throttle this call
$carousel.scroll(function scroll_carousel() {
if($carousel.scrollLeft() === 0) {
// disable left arrow
$pageLeft.addClass('disabled');
} else if($carousel[0].scrollWidth - $carousel.scrollLeft() == $carousel.width()) {
// disable right arrow
$pageRight.addClass('disabled');
} else {
$pager.find('a').removeClass('disabled');
// enable both arrows
}
});
// Setup click handlers for the prev/next buttons
$pageLeft.click(
function page_left_carousel(e){
if(!$(this).hasClass('disabled'))
$carousel.animate({
scrollLeft: carousel_scroll_position('left')
}, 500, "easeOutQuad");
e.preventDefault();
});
$pageRight.click(
function page_right_carousel(e){
if(!$(this).hasClass('disabled'))
$carousel.animate({
scrollLeft: carousel_scroll_position('right')
}, 500, "easeOutQuad");
e.preventDefault();
});
} else {
hide_carousel_pager();
}
});
};
$('.barker-carousel').barkerCarousel();
return $.fn.barkerCarousel;
}(require('jquery'));