Created
October 21, 2012 04:33
-
-
Save elclanrs/3925759 to your computer and use it in GitHub Desktop.
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
.sliced { | |
position: relative; | |
width: 640px; | |
height: 400px; | |
} | |
.tile { float: left; } |
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
;(function( $, window ) { | |
var _defaults = { | |
x : 2, // number of tiles in x axis | |
y : 2, // number of tiles in y axis | |
random : true, // animate tiles in random order | |
speed : 2000 // time to clear all times | |
}; | |
/** | |
* range Get an array of numbers within a range | |
* @param min {number} Lowest number in array | |
* @param max {number} Highest number in array | |
* @param rand {bool} Shuffle array | |
* @return {array} | |
*/ | |
function range( min, max, rand ) { | |
var arr = ( new Array( ++max - min ) ) | |
.join('.').split('.') | |
.map(function( v,i ){ return min + i }); | |
return rand | |
? arr.map(function( v ) { return [ Math.random(), v ] }) | |
.sort().map(function( v ) { return v[ 1 ] }) | |
: arr; | |
} | |
$.fn.sliced = function( options ) { | |
var o = $.extend( {}, _defaults, options ); | |
return this.each(function() { | |
var $container = $(this); | |
/*--------------------------------- | |
* Make the tiles: | |
---------------------------------*/ | |
var width = $container.width(), | |
height = $container.height(), | |
$img = $container.find('img'), | |
n_tiles = o.x * o.y, | |
tiles = [], $tiles; | |
for ( var i = 0; i < n_tiles; i++ ) { | |
tiles.push('<div class="tile"/>'); | |
} | |
$tiles = $( tiles.join('') ); | |
// Hide original image and insert tiles in DOM | |
$img.hide().after( $tiles ); | |
// Set background | |
$tiles.css({ | |
width: width / o.x, | |
height: height / o.y, | |
backgroundImage: 'url('+ $img.attr('src') +')' | |
}); | |
// Adjust position | |
$tiles.each(function() { | |
var pos = $(this).position(); | |
$(this).css( 'backgroundPosition', -pos.left +'px '+ -pos.top +'px' ); | |
}); | |
/*--------------------------------- | |
* Animate the tiles: | |
---------------------------------*/ | |
var tilesArr = range( 0, n_tiles, o.random ), | |
tileSpeed = o.speed / n_tiles; // time to clear a single tile | |
tilesArr.forEach(function( tile, i ) { | |
setTimeout(function(){ | |
$tiles.eq( tile ).fadeTo( 'fast', 0 ); | |
}, i * tileSpeed ); | |
}); | |
}); | |
}; | |
}( jQuery, window )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment