Skip to content

Instantly share code, notes, and snippets.

@tjtaylo
Last active February 11, 2016 20:58
Show Gist options
  • Save tjtaylo/05a8f8e418718b56bd83 to your computer and use it in GitHub Desktop.
Save tjtaylo/05a8f8e418718b56bd83 to your computer and use it in GitHub Desktop.
(function ($){
$.fn.testimonials = function(options) {
var list_wrapper = this;
var children = this.children();
var list = [];
var display_count = options.display;
var current_index = 0;
var timer = options.timer;
var animate = options.animate;
init();
function init() {
// setup css
var animation = 'opacity ' + animate + 'ms, max-height ' + animate + 'ms, margin-bottom ' + animate + 'ms';
$(children).css({
WebkitTransition : animation,
MozTransition : animation,
MsTransition : animation,
OTransition : animation,
transition : animation
});
// setup list
for (var i = 0; i < children.length; i++) {
list.push(children[i]);
if (i % 2 === 0) {
$(list[i]).addClass('even');
} else {
$(list[i]).addClass('odd');
}
}
$(list_wrapper)
// set height of wrapper
.height(getMaxHeight())
// remove contents of wrapper
.empty()
// set class of loaded
.addClass('loaded');
// display initial list items
for (var i = 0; i < display_count; i++) {
list_wrapper.append(list[i]);
setTimeout(function(){
$(list_wrapper).children().addClass('visible')
}, 1);
}
// activate loop
setInterval(loop, options.timer);
}
// the loop
function loop() {
var prev = getPrev();
var next = getNext();
// remove hidden item
$(list[prev]).removeClass('hidden').remove();
// append next item and animate it
list_wrapper.append(list[next]);
setTimeout(function(){
$(list_wrapper).children().last().addClass('visible')
}, options.animate / 2);
// hide the first item
$(list[current_index]).addClass('hidden').removeClass('visible');
// iterate the current_index
iterate();
}
// returns array with current visible elements ex. [0, 1]
function getVisible() {
var output = [];
for (var i = 0; i < display_count; i++) {
var index = current_index + i;
if (index >= list.length) {
index -= list.length;
}
output.push(list[index]);
}
return output;
};
// returns element before current index
function getPrev() {
var output = current_index - 1;
if (output < 0) {
output = list.length - 1;
}
return output;
};
// returns next element to be appended
function getNext() {
var output = current_index + display_count;
if (output >= list.length) {
output -= list.length;
}
return output;
}
// iterates the current index
function iterate() {
current_index++;
if (current_index >= list.length) {
current_index -= list.length;
}
};
// returns the maximum possible height based on list items height and the display_count
function getMaxHeight() {
var output = [];
var max = 0;
for (var i = 0; i < list.length; i++) {
var height = 0;
for (var e = 0; e < display_count; e++) {
var index = i + e;
if (index >= list.length) {
index -= list.length;
}
height += $(list[index]).outerHeight(true);
}
if (height > max) {
max = height;
}
}
return max;
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment