Skip to content

Instantly share code, notes, and snippets.

@elclanrs
Created October 21, 2012 03:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elclanrs/3925541 to your computer and use it in GitHub Desktop.
Save elclanrs/3925541 to your computer and use it in GitHub Desktop.
.sliced {
position: relative;
width: 640px;
height: 400px;
}
.tile { float: left; }
;(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
};
$.fn.sliced = function( options ) {
var o = $.extend( {}, _defaults, options );
return this.each(function() {
var $container = $(this),
width = $container.width(),
height = $container.height(),
$img = $container.find('img'),
n_tiles = o.x * o.y,
wraps = [], $wraps;
for ( var i = 0; i < n_tiles; i++ ) {
wraps.push('<div class="tile"/>');
}
$wraps = $( wraps.join('') );
// Hide original image and insert tiles in DOM
$img.hide().after( $wraps );
// Set background
$wraps.css({
width: width / o.x,
height: height / o.y,
backgroundImage: 'url('+ $img.attr('src') +')'
});
// Adjust position
$wraps.each(function() {
var pos = $(this).position();
$(this).css( 'backgroundPosition', -pos.left +'px '+ -pos.top +'px' );
});
});
};
}( jQuery, window ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment