Created
October 21, 2012 03:06
-
-
Save elclanrs/3925541 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 | |
}; | |
$.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