Skip to content
This repository has been archived by the owner on Sep 13, 2018. It is now read-only.

Commit

Permalink
The script now waits for all images to be loaded before executing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian committed Aug 29, 2011
1 parent a28e574 commit d065c06
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples.html
Expand Up @@ -156,8 +156,8 @@ <h1>Images are larger than the viewport </h1>
<script type="text/javascript">
$(function(){
var carousel1 = Carousel($("#carousel1"),{speed:100, debug:false});
var carousel2 = Carousel($("#carousel2"),{speed:500, debug:true});
var carousel3 = Carousel($("#carousel3"),{speed:300, debug:false});
var carousel2 = Carousel($("#carousel2"),{speed:500, debug:false});
var carousel3 = Carousel($("#carousel3"),{speed:300, debug:true});
var carousel4 = Carousel($("#carousel4"),{speed:300, debug:false});
});
</script>
Expand Down
22 changes: 16 additions & 6 deletions js/libs/jquery.carousel.js
@@ -1,6 +1,7 @@
/********************************************************************************************
* Author: Sebastian Porto
* August 2011
* v.0.3
* ******************************************************************************************/

var Carousel = function(element, args){
Expand Down Expand Up @@ -33,8 +34,18 @@ var Carousel = function(element, args){
if(args.debug) _debug = args.debug;

init();

function init(){
//all image must be loaded
$("figure", _$element).hide();

$(window).load(
function(){
initWithImages();
});
}

function initWithImages(){
log("init");

//store the width of the container
Expand Down Expand Up @@ -90,7 +101,8 @@ var Carousel = function(element, args){
//make an array with the size of each image
//and store the total width of the images
$("figure img",_$element).each(function(){
var w = $(this).width()
var w = $(this).width();
log("w = " + w);
_originalItemsWidthsArray.push(w);
_allItemsWithArray.push(w);
_originalItemsTotalWidth += w;
Expand All @@ -106,7 +118,7 @@ var Carousel = function(element, args){
// var nextToClone = _originalItems - _pre
var clonedIndex = _originalItemsCount-1;
log("clonedIndex " + clonedIndex);
var clonedWidth = _originalItemsWidthsArray[clonedIndex];
var clonedWidth = _originalItemsWidthsArray[clonedIndex];
log("clonedWidth " + clonedWidth);
var clone = $(_originalItems[clonedIndex]).clone();

Expand All @@ -131,18 +143,16 @@ var Carousel = function(element, args){
log("Too many!!!");
break;
}

if(nextIndex >= _originalItemsCount) nextIndex = 0;

clonePostItem(nextIndex);

nextIndex++;
}
}

function clonePostItem(clonedIndex){
log("clonePostItem " + clonedIndex);
var clonedWidth = _originalItemsWidthsArray[clonedIndex];
log("clonedWidth " + clonedWidth);
var clone = $(_originalItems[clonedIndex]).clone();

_allItemsWithArray.push(clonedWidth);
Expand Down

0 comments on commit d065c06

Please sign in to comment.