答案 0 :(得分:2)
该插件有点旧,我建议您使用此插件,改编自this other plugin:
演示: http://jsfiddle.net/elclanrs/HmpGx/
;(function( $, window ) {
var _defaults = { x: 3, y: 3, gap: 2 };
$.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('') );
$img.hide().after( $wraps );
$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') +')'
});
$wraps.each(function() {
var pos = $(this).position();
$(this).css( 'backgroundPosition', -pos.left +'px '+ -pos.top +'px' );
});
});
};
}( jQuery, window ));