/*!
* jquery wmuslider v2.1
*
* copyright (c) 2011 brice lechatellier
* http://brice.lechatellier.com/
*
* licensed under the mit license: http://www.opensource.org/licenses/mit-license.php
*/
;(function($) {
$.fn.wmuslider = function(options) {
/* default options
================================================== */
var defaults = {
animation: 'fade',
animationduration: 600,
slideshow: true,
slideshowspeed: 7000,
slidetostart: 0,
navigationcontrol: true,
paginationcontrol: true,
previoustext: 'previous',
nexttext: 'next',
touch: false,
slide: 'article',
items: 1
};
var options = $.extend(defaults, options);
return this.each(function() {
/* variables
================================================== */
var $this = $(this);
var currentindex = options.slidetostart;
var wrapper = $this.find('.wmusliderwrapper');
var slides = $this.find(options.slide);
var slidescount = slides.length;
var slideshowtimeout;
var paginationcontrol;
var isanimating;
/* load slide
================================================== */
var loadslide = function(index, infinite, touch) {
if (isanimating) {
return false;
}
isanimating = true;
currentindex = index;
var slide = $(slides[index]);
$this.animate({ height: slide.innerheight() });
if (options.animation == 'fade') {
slides.css({
position: 'absolute',
opacity: 0
});
slide.css('position', 'relative');
slide.animate({ opacity:1 }, options.animationduration, function() {
isanimating = false;
});
} else if (options.animation == 'slide') {
if (!infinite) {
wrapper.animate({ marginleft: -$this.width() / options.items * index }, options.animationduration, function() {
isanimating = false;
});
} else {
if (index == 0) {
wrapper.animate({ marginleft: -$this.width() / options.items * slidescount }, options.animationduration, function() {
wrapper.css('marginleft', 0);
isanimating = false;
});
} else {
if (!touch) {
wrapper.css('marginleft', -$this.width() / options.items * slidescount);
}
wrapper.animate({ marginleft: -$this.width() / options.items * index }, options.animationduration, function() {
isanimating = false;
});
}
}
}
if (paginationcontrol) {
paginationcontrol.find('a').each(function(i) {
if(i == index) {
$(this).addclass('wmuactive');
} else {
$(this).removeclass('wmuactive');
}
});
}
// trigger event
$this.trigger('slideloaded', index);
};
/* navigation control
================================================== */
if (options.navigationcontrol) {
var prev = $('' + options.previoustext + '');
prev.click(function(e) {
e.preventdefault();
cleartimeout(slideshowtimeout);
if (currentindex == 0) {
loadslide(slidescount - 1, true);
} else {
loadslide(currentindex - 1);
}
});
$this.append(prev);
var next = $('' + options.nexttext + '');
next.click(function(e) {
e.preventdefault();
cleartimeout(slideshowtimeout);
if (currentindex + 1 == slidescount) {
loadslide(0, true);
} else {
loadslide(currentindex + 1);
}
});
$this.append(next);
}
/* pagination control
================================================== */
/*-- if (options.paginationcontrol) {
paginationcontrol = $('
');
$.each(slides, function(i) {
paginationcontrol.append('' + i + '');
paginationcontrol.find('a:eq(' + i + ')').click(function(e) {
e.preventdefault();
cleartimeout(slideshowtimeout);
loadslide(i);
});
});
$this.append(paginationcontrol);
}
/* slideshow
================================================== */
if (options.slideshow) {
var slideshow = function() {
if (currentindex + 1 < slidescount) {
loadslide(currentindex + 1);
} else {
loadslide(0, true);
}
slideshowtimeout = settimeout(slideshow, options.slideshowspeed);
}
slideshowtimeout = settimeout(slideshow, options.slideshowspeed);
}
/* resize slider
================================================== */
var resize = function() {
var slide = $(slides[currentindex]);
$this.animate({ height: slide.innerheight() });
if (options.animation == 'slide') {
slides.css({
width: $this.width() / options.items
});
wrapper.css({
marginleft: -$this.width() / options.items * currentindex,
width: $this.width() * slides.length
});
}
};
/* touch
================================================== */
var touchswipe = function(event, phase, direction, distance) {
cleartimeout(slideshowtimeout);
if(phase == 'move' && (direction == 'left' || direction == 'right')) {
if (direction == 'right') {
if (currentindex == 0) {
wrapper.css('marginleft', (-slidescount * $this.width() / options.items) + distance);
} else {
wrapper.css('marginleft', (-currentindex * $this.width() / options.items) + distance);
}
} else if (direction == 'left') {
wrapper.css('marginleft', (-currentindex * $this.width() / options.items) - distance);
}
} else if (phase == 'cancel' ) {
if (direction == 'right' && currentindex == 0) {
wrapper.animate({ marginleft: -slidescount * $this.width() / options.items }, options.animationduration);
} else {
wrapper.animate({ marginleft: -currentindex * $this.width() / options.items }, options.animationduration);
}
} else if (phase == 'end' ) {
if (direction == 'right') {
if (currentindex == 0) {
loadslide(slidescount - 1, true, true);
} else {
loadslide(currentindex - 1);
}
} else if (direction == 'left') {
if (currentindex + 1 == slidescount) {
loadslide(0, true);
} else {
loadslide(currentindex + 1);
}
} else {
wrapper.animate({ marginleft: -currentindex * $this.width() / options.items }, options.animationduration);
}
}
};
if (options.touch && options.animation == 'slide') {
if (!$.isfunction($.fn.swipe)) {
$.ajax({
url: 'jquery.touchswipe.min.js',
async: false
});
}
if ($.isfunction($.fn.swipe)) {
$this.swipe({ triggerontouchend:false, swipestatus:touchswipe, allowpagescroll:'vertical' });
}
}
/* init slider
================================================== */
var init = function() {
var slide = $(slides[currentindex]);
var img = slide.find('img');
img.load(function() {
wrapper.show();
$this.animate({ height: slide.innerheight() });
});
if (options.animation == 'fade') {
slides.css({
position: 'absolute',
width: '100%',
opacity: 0
});
$(slides[currentindex]).css('position', 'relative');
} else if (options.animation == 'slide') {
if (options.items > slidescount) {
options.items = slidescount;
}
slides.css('float', 'left');
slides.each(function(i){
var slide = $(this);
slide.attr('data-index', i);
});
for(var i = 0; i < options.items; i++) {
wrapper.append($(slides[i]).clone());
}
slides = $this.find(options.slide);
}
resize();
$this.trigger('hasloaded');
loadslide(currentindex);
}
init();
/* bind events
================================================== */
// resize
$(window).resize(resize);
// load slide
$this.bind('loadslide', function(e, i) {
cleartimeout(slideshowtimeout);
loadslide(i);
});
});
}
})(jquery);