Skip to content

Instantly share code, notes, and snippets.

@lagden
Created November 21, 2012 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lagden/4123268 to your computer and use it in GitHub Desktop.
Save lagden/4123268 to your computer and use it in GitHub Desktop.
A CodePen by Thiago Lagden. Infinite Carousel Responsive - jQuery - Oh yeahhh!! Another jQuery Carousel plugin!!
<div class="wrapper">
<a href="#nav" class="arrow prev">Prev</a>
<a href="#nav" class="arrow next">Next</a>
<ul class="slider">
<li>
<div class="caption">
<h1><a href="#lnk">1 - Sample</a></h1>
</div>
</li>
<li>
<div class="caption">
<h1><a href="#lnk">2 - Sample<br>Nice!!</a></h1>
</div>
</li>
<li>
<div class="caption">
<h1><a href="#lnk">3 - Sample</a></h1>
</div>
</li>
</ul>
</div>
// lagdenSlider - Carousel infinito
!function($){
"use strict";
var LagdenSliderInfinito = function(el, opts)
{
this.init('lagdenSlider', el, opts);
}
LagdenSliderInfinito.prototype = {
constructor: LagdenSliderInfinito,
init: function(type, el, opts){
var self = this;
this.type = type;
this.$el = $(el);
this.opts = this.getOptions(opts);
this.pos = 0;
this.itens = this.$el.find(this.opts.base);
// grab the width and calculate left value
this.itensWidth = (100 / this.itens.length);
this.leftValue = -100;
this.$el.css({'width': (this.itens.length * 100) + '%' });
this.itens.css({'width': this.itensWidth + '%' });
// move the last item before first item, just in case user click prev
this.$el.find(this.opts.base + ':first').before(this.$el.find(this.opts.base + ':last'));
// set the default item to the correct position
this.$el.css({'left': this.leftValue + '%'});
if(this.opts.prev != null)
{
$(this.opts.prev).on('click.' + this.type, function(e){
if(!self.moving) self.move('prev');
e.preventDefault();
});
}
if(this.opts.next != null)
{
$(this.opts.next).on('click.' + this.type, function(e){
if(!self.moving) self.move('next');
e.preventDefault();
});
}
// autoslide
if(this.opts.autoSlide) this.autoSlide();
}
,getOptions: function(opts)
{
opts = $.extend({}, $.fn[this.type].defaults, opts);
return opts;
}
,move: function(direction)
{
this.moving = true;
var that = this;
var left_indent = ((direction == 'prev') ? this.leftValue + 100 : this.leftValue - 100 ) + '%';
//slide the item
this.$el.animate({'left' : left_indent}, this.opts.speedTransition, 'swing', function(){
(direction == 'prev')
? that.$el.find(that.opts.base + ':first').before(that.$el.find(that.opts.base + ':last'))
: that.$el.find(that.opts.base + ':last').after(that.$el.find(that.opts.base + ':first'));
// restart time
if(that.opts.autoSlide) that.autoSlide();
// set the default item to correct position
that.$el.css({'left': that.leftValue + '%'});
// free for next animation
that.moving = false;
});
}
,autoSlide: function()
{
var that = this;
if(this.run) clearInterval(this.run);
this.run = setInterval(function(){ that.move('next'); }, this.opts.speed);
}
}
$.fn.lagdenSlider = function(opts)
{
return this.each(function () {
new LagdenSliderInfinito(this, opts)
});
}
$.fn.lagdenSlider.Constructor = LagdenSliderInfinito;
$.fn.lagdenSlider.defaults = {
'base': '> li'
,'prev': null
,'next': null
,'speed': 3000
,'speedTransition': 1000
,'autoSlide': true
}
}(jQuery);
(function($){
$('ul.slider').lagdenSlider({
prev: 'a.arrow.prev'
,next: 'a.arrow.next'
});
})(jQuery);
@import "compass";
.wrapper{
width: 100%;
overflow: hidden;
ul.slider{
font-size: 0;
list-style: none;
margin: 0;
padding: 0;
position: relative;
> li{
background:{
image: url('http://placekitten.com/g/700/300');
size: cover;
position: center;
}
display: inline-block;
.caption{
height: 300px;
&:before{
content: '\0020';
display: inline-block;
height: 100%;
vertical-align: middle;
}
h1{
font-size: 1rem;
width: 100%;
background-color: rgba(black, .5);
display: inline-block;
vertical-align: middle;
text-align: right;
a{
display: block;
color: white;
padding: 1rem;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment