forked from jsor/jcarousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jcarousel-scrollintoview.js
63 lines (50 loc) · 1.69 KB
/
jquery.jcarousel-scrollintoview.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
/*! jCarousel - v0.3.0 - 2014-01-09
* http://sorgalla.com/jcarousel
* Copyright (c) 2014 Jan Sorgalla; Licensed MIT */
(function($) {
'use strict';
$.jcarousel.fn.scrollIntoView = function(target, animate, callback) {
var parsed = $.jCarousel.parseTarget(target),
first = this.index(this._fullyvisible.first()),
last = this.index(this._fullyvisible.last()),
index;
if (parsed.relative) {
index = parsed.target < 0 ? Math.max(0, first + parsed.target) : last + parsed.target;
} else {
index = typeof parsed.target !== 'object' ? parsed.target : this.index(parsed.target);
}
if (index < first) {
return this.scroll(index, animate, callback);
}
if (index >= first && index <= last) {
if ($.isFunction(callback)) {
callback.call(this, false);
}
return this;
}
var items = this.items(),
clip = this.clipping(),
lrb = this.vertical ? 'bottom' : (this.rtl ? 'left' : 'right'),
wh = 0,
curr;
while (true) {
curr = items.eq(index);
if (curr.length === 0) {
break;
}
wh += this.dimension(curr);
if (wh >= clip) {
var margin = parseFloat(curr.css('margin-' + lrb)) || 0;
if ((wh - margin) !== clip) {
index++;
}
break;
}
if (index <= 0) {
break;
}
index--;
}
return this.scroll(index, animate, callback);
};
}(jQuery));