Skip to content

Instantly share code, notes, and snippets.

@setola
Created May 24, 2016 16:24
Show Gist options
  • Save setola/db907297cfda8c9d7dc4793e7ad4afde to your computer and use it in GitHub Desktop.
Save setola/db907297cfda8c9d7dc4793e7ad4afde to your computer and use it in GitHub Desktop.
A simple way to integrate external controls to Bootstrap carousel
/**
* Just an object used for name spacing
*/
var agitaLab = {};
/**
* Two ways binding between an external "carousel-control like"
* element and the carousel slides.
* Remember to add data-item-no to your carousel :)
*/
agitaLab.sliderExternalControl = function () {
var elements = jQuery(this);
var activeClass = 'active';
var controls = jQuery('[href="#' + elements.attr('id') + '"], [data-target="' + elements.attr('id') + '"]');
elements.each(
function () {
var element = jQuery(this);
element.on(
'slide.bs.carousel', function (event) {
var currentItemTo = jQuery(event.relatedTarget).attr('data-item-no');
console.log(event, event.relatedTarget, currentItemTo);
controls.each(
function () {
var control = jQuery(this);
if (control.data('slide-to') == currentItemTo) {
control.addClass(activeClass);
} else {
control.removeClass(activeClass);
}
}
);
}
);
}
);
};
agitaLab.onReady = function () {
agitaLab.sliderExternalControl.call(jQuery('#carousel-home-1'));
};
jQuery(document).ready(agitaLab.onReady);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment