Skip to content

Commit 783e422

Browse files
committed
New default behavior: Group handles when multiple slides are visible
1 parent 6acebda commit 783e422

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

jquery.carousel.js

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
circular: false, // go to first slide after last one
1515
autoplay: 0, // auto-advance interval (0: no autoplay)
1616
pauseAutoplayOnHover: true,
17-
keyboardNav: true // enable arrow and [p][n] keys for prev / next actions
17+
keyboardNav: true, // enable arrow and [p][n] keys for prev / next actions
18+
groupedHandles: true // combine handles to group if visibleSlides > 1 (e.g. "1-3", "4-6", "7")
1819
},
1920
elements: { // which navigational elements to show
2021
prevNext: true, // buttons for previous / next slide
@@ -327,7 +328,7 @@
327328
if (this.settings.elements.handles) {
328329
this.$dom.handles.on(events, function(event){
329330
var $this = $(this),
330-
handleIndex = $this.index(),
331+
handleIndex = $this.data(namespace +'-handle-index') || $this.index(),
331332
slideIndex = self._getCurrentSlideIndex(handleIndex),
332333
callback = function(){
333334
self.goTo(slideIndex);
@@ -586,20 +587,48 @@
586587

587588
// Return a group of handles (one for each slide)
588589
_getHandles: function(){
589-
var fragment = document.createDocumentFragment();
590+
var fragment = document.createDocumentFragment(),
591+
slideGroups;
590592

591-
for (var i = 0; i < this.props.total; i++) {
592-
// var handle = document.createElement('span');
593-
//
594-
// handle.className = namespace + '-handle';
595-
// handle.innerHTML = this.settings.text.handle.replace('%index%', (i+1));
596-
// handle.setAttribute('role', 'button');
597-
// handle.setAttribute('tabindex', 0);
593+
if (!this.settings.behavior.groupedHandles) {
594+
for (var i = 0; i < this.props.total; i++) {
595+
// var handle = document.createElement('span');
598596

599-
var text = this.settings.text.handle.replace('%index%', (i+1)),
600-
handle = this.$dom.handle.clone().text(text).get(0);
597+
// handle.className = namespace + '-handle';
598+
// handle.innerHTML = this.settings.text.handle.replace('%index%', (i+1));
599+
// handle.setAttribute('role', 'button');
600+
// handle.setAttribute('tabindex', 0);
601601

602-
fragment.appendChild(handle);
602+
var text = this.settings.text.handle.replace('%index%', (i+1)),
603+
handle = this.$dom.handle.clone().text(text).get(0);
604+
605+
fragment.appendChild(handle);
606+
}
607+
} else {
608+
slideGroups = this.props.total / this.props.visible;
609+
610+
for (var i = 0; i < slideGroups; i++) {
611+
var minIndex = i * this.props.visible + 1,
612+
maxIndex = (i+1) * this.props.visible,
613+
$handle = this.$dom.handle.clone(),
614+
handle, text;
615+
616+
if (maxIndex > this.props.total) {
617+
maxIndex = this.props.total;
618+
}
619+
620+
if (minIndex < maxIndex) {
621+
text = minIndex +' - '+ maxIndex;
622+
} else {
623+
text = maxIndex;
624+
}
625+
626+
text = this.settings.text.handle.replace('%index%', text);
627+
628+
handle = $handle.text(text).data(namespace + '-handle-index', minIndex-1).get(0);
629+
630+
fragment.appendChild(handle);
631+
}
603632
}
604633

605634
return $(fragment);
@@ -908,6 +937,10 @@
908937
var currentIndex = this._getOriginalSlideIndex(this.props.currentDomIndex),
909938
currentHandles = (currentIndex > 0) ? ':gt(' + (currentIndex - 1) + '):lt(' + this.props.visible + ')' : ':lt(' + (currentIndex + this.props.visible) + ')';
910939

940+
if (this.settings.behavior.groupedHandles) {
941+
currentHandles = ':eq(' + Math.ceil(currentIndex/this.props.visible) + ')';
942+
}
943+
911944
utils.enableButton(this.$dom.handles);
912945

913946
this.$dom.handles.removeClass('state-current');

0 commit comments

Comments
 (0)