vpn.email
Version:
vpn.email client
327 lines (251 loc) • 8.65 kB
JavaScript
$.widget("metro.carousel", {
version: "3.0.0",
options: {
auto: true,
period: 5000,
duration: 1000,
effect: 'slide', // slide, fade, switch, slowdown
effectFunc: 'linear',
direction: 'left',
controls: true,
controlNext: false,
controlPrev: false,
markers: true,
stop: true,
width: '100%',
height: false,
_slides: {},
_currentIndex: 0,
_interval: 0,
_outPosition: 0,
_animating: false
},
_create: function(){
var that = this, o = this.options,
element = this.element;
$.each(element.data(), function(key, value){
if (key in o) {
try {
o[key] = $.parseJSON(value);
} catch (e) {
o[key] = value;
}
}
});
o._slides = element.find('.slide');
var max_height = 0; //element.find('.slide:nth-child(1)').outerHeight();
$.each(o._slides, function(){
var oh, slide = $(this);
oh = slide.outerHeight();
if (oh > max_height) {max_height = oh;}
});
element.css({
'width': o.width,
'height': o.height ? o.height : max_height
});
if (o._slides.length <= 1) {return;}
if (o.markers) {
this._markers();
}
if (o.controls) {
this._controls();
}
if (o.stop) {
element
.on('mouseenter', function(){
clearInterval(o._interval);
})
.on('mouseleave', function(){
if (that.options.auto) {that._autoStart();}// that.options.period;
});
}
element.find('.slide').hide();
element.find('.slide:nth-child(1)').show();
//this._slideToSlide(0);
if (o.auto) {
this._autoStart();
}
element.data('carousel', this);
},
_autoStart: function(){
var that = this, o = this.options;
o._interval = setInterval(function(){
if (o.direction === 'left') {
that._slideTo('next');
} else {
that._slideTo('prior');
}
}, o.period);
},
_slideTo: function(direction){
var carousel = this.element, that = this, o = this.options;
var currentSlide = o._slides[o._currentIndex], nextSlide;
if (direction === undefined) {direction = 'next';}
if (direction === 'prior') {
o._currentIndex -= 1;
if (o._currentIndex < 0) {o._currentIndex = o._slides.length - 1;}
o._outPosition = this.element.width();
} else if (direction === 'next') {
o._currentIndex += 1;
if (o._currentIndex >= o._slides.length) {o._currentIndex = 0;}
o._outPosition = -this.element.width();
}
nextSlide = o._slides[o._currentIndex];
switch (this.options.effect) {
case 'switch': this._effectSwitch(currentSlide, nextSlide); break;
case 'slowdown': this._effectSlowdown(currentSlide, nextSlide, this.options.duration); break;
case 'fade': this._effectFade(currentSlide, nextSlide, this.options.duration); break;
default: this._effectSlide(currentSlide, nextSlide, this.options.duration);
}
carousel.find('.carousel-bullets a').each(function(){
var index = $(this).data('num');
if (index === o._currentIndex) {
$(this).addClass('bullet-on');
} else {
$(this).removeClass('bullet-on');
}
});
},
_slideToSlide: function(slideIndex){
var o = this.options,
currentSlide = o._slides[o._currentIndex],
nextSlide = o._slides[slideIndex];
if (o._currentIndex === slideIndex) {
return false;
}
if (slideIndex > o._currentIndex) {
o._outPosition = -this.element.width();
} else {
o._outPosition = this.element.width();
}
switch (this.options.effect) {
case 'switch' : this._effectSwitch(currentSlide, nextSlide); break;
case 'slowdown': this._effectSlowdown(currentSlide, nextSlide); break;
case 'fade': this._effectFade(currentSlide, nextSlide); break;
default : this._effectSlide(currentSlide, nextSlide);
}
o._currentIndex = slideIndex;
},
_controls: function(){
var next, prev, that = this, element = this.element, o = this.options;
next = $('<span/>').addClass('carousel-switch-next').html(">");
prev = $('<span/>').addClass('carousel-switch-prev').html("<");
if (o.controlNext) {
next.html(o.controlNext);
}
if (o.controlPrev) {
prev.html(o.controlPrev);
}
next.appendTo(element);
prev.appendTo(element);
if (o._slides.length > 1) {
prev.on('click', function(){
if (o._animating === false) {
that._slideTo('prior');
o._animating = true;
setTimeout(function(){o._animating = false;}, o.duration);
}
});
next.on('click', function(){
if (o._animating === false) {
that._slideTo('next');
o._animating = true;
setTimeout(function(){o._animating = false;}, o.duration);
}
});
} else {
next.hide();
prev.hide();
}
},
_markers: function () {
var div, a, i, that = this, o = this.options;
div = $('<div class="carousel-bullets" />');
for (i = 0; i < o._slides.length; i++) {
a = $('<a class="carousel-bullet" href="javascript:void(0)" data-num="' + i + '"></a>');
if (i === 0) {
a.addClass('bullet-on');
}
a.appendTo(div);
}
div.find('a').on('click', function (e) {
var _this = $(this),
index = _this.data('num');
div.find('a').removeClass('bullet-on');
_this.addClass('bullet-on');
if (index === o._currentIndex) {
return false;
}
that._slideToSlide(index);
e.preventDefault();
e.stopPropagation();
});
div.appendTo(this.element);
},
_effectSwitch: function(currentSlide, nextSlide){
$(currentSlide)
.hide();
$(nextSlide)
.css({left: 0})
.show();
this.element.css({
height: $(nextSlide).outerHeight()
});
},
_effectSlide: function(currentSlide, nextSlide){
var o = this.options;
$(currentSlide)
.animate({left: o._outPosition}, o.duration, o.effectFunc);
$(nextSlide)
.css('left', o._outPosition * -1)
.show();
this.element.css({
height: $(nextSlide).outerHeight()
});
$(nextSlide).animate({left: 0}, o.duration, o.effectFunc);
},
_effectSlowdown: function(currentSlide, nextSlide){
var o = this.options;
var options = {
'duration': o.duration,
'easing': 'doubleSqrt'
};
$.easing.doubleSqrt = function(t) {
return Math.sqrt(Math.sqrt(t));
};
$(currentSlide)
.animate({left: o._outPosition}, options);
$(nextSlide)
.css('left', o._outPosition * -1)
.show();
this.element.css({
height: $(nextSlide).outerHeight()
});
$(nextSlide).animate({left: 0}, options);
},
_effectFade: function(currentSlide, nextSlide){
var o = this.options;
$(currentSlide)
.fadeOut(o.duration);
$(nextSlide)
.css({left: 0})
.fadeIn(o.duration);
this.element.css({
height: $(nextSlide).outerHeight()
});
},
slideTo: function(index){
this._slideToSlide(index);
},
nextSlide: function(){
this._slideTo('next');
},
priorSlide: function(){
this._slideTo('prior');
},
_destroy: function(){
},
_setOption: function(key, value){
this._super('_setOption', key, value);
}
});