<!-- Author: Aaron Silber -->
<!-- Insert your foldup images and give them the 'foldup' class -->
<img src="http://placekitten.com/200/200" class="foldup" />
<img src="http://placekitten.com/150/200" class="foldup" />
function FoldImg(islices) {
var foldupsrc = $('.foldup');
var folduph = foldupsrc.height();
var foldupw = foldupsrc.width();
function initSegments(i) {
var thisimg = foldupsrc[i];
var folduph = $(thisimg).height();
var foldupw = $(thisimg).width();
/* Create wrapper for this img's Segments */
$(thisimg).before('<div class="foldwrap"></div>');
var slicewrap = $(foldupsrc[i]).prev('.foldwrap');
/* Given the foldwarp height and width based on src image h/w */
$(slicewrap).css({"height":folduph,"width":foldupw});
var foldupbg = $(foldupsrc[i]).attr('src');
/* Create the basic slices for this image */
for(i=0; i < islices; i++){
e = i+1;
var slicemarkup = $('<div style="background-image:url('+ foldupbg +')"></div>');
$(slicewrap).append(slicemarkup.addClass('slice'+e));
}
foldupsrc.hide(); /* Remove src image */
}
function styleSegments() {
$('.foldwrap').each( function() {
var isegments = $(this).children().size(); /* Find the number of segments */
var folduph = $(this).height();
var foldupw = $(this).width();
$(this).children().each( function(i) {
$(this).css("height",(1/isegments*folduph));
$(this).css("backgroundPositionY",(-i/isegments*folduph));
});
});
}
var imgs = foldupsrc.size();
/* If browser supports transitions and such.. if modernizer used */
if($('.csstransitions').length) {
for(i=0; i < imgs; i++){
initSegments(i);
}
styleSegments();
}
}
$(document).ready(function(){
FoldImg(5); /* Pass the number of slices */
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.