Created
December 14, 2012 17:39
-
-
Save uhtred/4287216 to your computer and use it in GitHub Desktop.
Javascript: Carrossel Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Carrossel = (function(){ | |
var instance, itemHeight, itemWidth, allItensWidth = 0; | |
function init( instanceId, options ) { | |
instance = $('#'+instanceId); | |
$( instance ) | |
.delegate('.carrossel-widget-nav-prev', 'click', prev) | |
.delegate('.carrossel-widget-nav-next', 'click', next); | |
configSizes(); | |
} | |
function configSizes(){ | |
var sample = instance.find('.carrossel-widget-images li:first'); | |
itemHeight = sample.height(); | |
itemWidth = sample.width(); | |
allItensWidth = itemWidth * instance.find('.carrossel-widget-images li').length; | |
$('.carrossel-widget-images').width(allItensWidth); | |
} | |
function getInstance(){ | |
return instance; | |
} | |
function next(){ | |
var next = getActiveItem().next(); | |
if( !next.length ) { | |
next = getFirstItem(); | |
} | |
goToItem( next ); | |
} | |
function prev(){ | |
var prev = getActiveItem().prev(); | |
if( !prev.length ) { | |
prev = getLastItem(); | |
} | |
goToItem( prev ); | |
} | |
function goToItem( item ){ | |
setActiveItem( item ); | |
var prevPosition = ( itemWidth * (item.prevAll().length) ) * -1; | |
goToPosition(prevPosition); | |
} | |
function setActiveItem( item ){ | |
$('.carrossel-widget-item-active').removeClass('carrossel-widget-item-active'); | |
item.addClass('carrossel-widget-item-active'); | |
} | |
function getActiveItem(){ | |
return instance.find('.carrossel-widget-item-active'); | |
} | |
function getFirstItem(){ | |
return instance.find('.carrossel-widget-images li:first'); | |
} | |
function getLastItem(){ | |
return instance.find('.carrossel-widget-images li:last'); | |
} | |
function goToPosition( position ) { | |
console.log(position+'px') | |
instance.find('.carrossel-widget-images').css('margin-left', position+'px'); | |
} | |
return { | |
getInstance: getInstance, | |
init: init, | |
next: next, | |
prev: prev | |
}; | |
})(); | |
$(function(){ | |
Carrossel.init('carrossel-1'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment