Split Image
HTML
<div><img src="http://placehold.it/400x400"/></div>
CSS
div {
width: 400px;
height: 400px;
position: relative;
}
.tile {
float: left;
-webkit-transition: all .1s linear;
-moz-transition: all .1s linear;
-ms-transition: all .1s linear;
-o-transition: all .1s linear;
}
.tile:hover {
opacity: .65;
}
JavaScript
;(function( $, window ) {
var _defaults = {
x : ,320,// tiles in x axis
y : 320, // tiles in y axis
gap: 1
};
$.fn.splitInTiles = 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) - o.gap,
height: (height / o.y) - o.gap,
marginBottom: o.gap +'px',
marginRight: o.gap +'px',
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 ));
$('div').splitInTiles();