Feed on
Posts
Comments

About a month ago I was reading the New York Times online and they had an article which showed a road in Brooklyn that had been reconstructed to make it safer and more pleasing to the eye. To show the difference  in the reconstruction project, they showed a before and after picture using Flash that let the visitor drag a slider over the images, which were sandwiched with one on top of the other, so that you could easily see how dramatic the changes were. I immediately thought that this could be done in JavaScript using jQuery, so I set out to do it. Here’s the result:

Pretty slick no? The possibilities for this plugin are endless. Doctors can have before and after images of patients, Photoshop users can show the before and after differences between images, remodelers can show the before and after images of projects and on and on. This plugin weighs in at only 7K and can be used multiple times on a page.

Download

Go to the Before/After download page

What’s So Great About this Plugin?

  • Slick effect, no Flash needed
  • It’s just 7K (4K compressed)
  • Reusable on multiple containers
  • Degradable. If the visitor doesn’t have JavaScript they will still see both images.

How to Use

First, your before and after images must be the same size.  Each image must be inside its own div, and both of those within a containing div which must have an ID.  See this example.

<div id="container">
 <div><img alt="before" src="before.jpg" width="600" height="366" /></div>
 <div><img alt="after" src="after.jpg" width="600" height="366" /></div>
</div>

All images *MUST* have the width and height declared otherwise the plugin won’t work in Safari, Chrome, and any other webkit-based browsers.

To use the plugin you’ll need to have a copy of  jQuery and the jQuery UI, or point to jquery on Google and jqueryui on Google, and the plugin. Place the files on your site and link to them:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.beforeafter.js"></script>
<script type="text/javascript">
$(function(){
	$('#container').beforeAfter();
});
</script>

That’s it! You can apply the before/after plugin to any number of elements on a page.

Options

The following options are configurable:

  • animateIntro  – whether or not to have the drag bar start completely to the right of the container and gradually move by itself to the midpoint (default  false)
  • introDelay – if animateIntro is true, the number of milliseconds to wait before beginning the automatic drag bar move to the center of the image (default  1000)
  • introDuration – if animateIntro is true, the number of milliseconds it will take for the drag bar to go from the right side of the image to the middle of the image (default 1000)
  • showFullLinks – whether or not to display links below the image that a visitor can click on that will automatically show the full before or full after image (default true)
  • imagePath – the path (absolute or relative) to where you store the navigation images (default ‘/js/beforeAfter/’)

Options are added when calling the script:

$('#container').beforeAfter({
	animateIntro : true,
        introDelay : 2000,
        introDuration : 500,
        showFullLinks : false
});

Enjoy!

Demos

Note that the plugin uses several images which are kept in the same folder as the plugin. If you store the plugin in another folder on your server, update the path to these images. Please be sure to read the license before using.

394 Responses to “jQuery Before/After Plugin”

  1. Ashlee says:

    Hi,
    How would I go about making the scroll move vertically instead of horizontally? My skills are very limited, so I would GREATLY appreciate any help with this!
    Thank you!

  2. Charles Wood says:

    Having trouble with before after javascript (not your fault though). Im trying to use it with cargo collective, and they already reference there own jquery 1.4.1 library. If I add a reference to the 1.6 library you’ve bundled, it breaks other aspects of the site.

    Is it possible for this to work referencing only jquery 1.4 elements?

    page in question:
    http://www.bigchaz.com/Mizuno-USA-Pitch

    thanks!

  3. David says:

    Is there a video tutorial online for using the jQuery Before/After Plugin? (this is all new to me) :)

  4. ademilter says:

    iPhone & iPad does not work :( Is there a solution?

    • admin says:

      That’s actually a jQuery UI issue. Their draggable function doesn’t work on touch devices. However, by adding the jQuery Touch/Punch plugin (https://github.com/furf/jquery-ui-touch-punch) after the Before/After plugin, it should work fine without any changes.

      I’ve been meaning to add this footnote to the docs for Before/After since I get a fair number of questions about this so hopefully this will remind me to do so.

  5. Makarand says:

    Thanks for the plug in, I was searching for this before and after effects !! got it !

  6. It doesn’t show on my site in Chrome and I have the latest jQuery. It’s odd because sometimes it seems to half load, but others it completely doesn’t.

  7. olivier says:

    Hi,
    I’m looking for the same effect but without having to click, just by moving the mouse on the image. Anyone can help ? Can’t find what I’m looking for…

  8. Irfan Ashraf says:

    I am recently working on your plugin. I am having problems with .png files. I made the background transparent and this script do not show the transparent background of the images. Do you have any suggestion please let me know.

  9. Ianemv says:

    1st Web Designer led me here. Nice plugin! I remember pictures of tsunami aftermath last year

  10. Paulius says:

    This plugin could be improved by making entire image area dragable. Now 50% of the time when I try to drag slider I drag by mistake image. Please take a look at this website
    http://www.guardian.co.uk/uk/interactive/2011/aug/09/london-riots-before-after-photographs

    so you can see how you can improve your plugin.

    Select anywhere on the image and try to drag and you will see that there is no mistake to be made.

Trackbacks/Pingbacks

  1. [...] jQuery Before/After – Plugin étonnant qui plaira à tous ceux qui veulent proposer la comparaison de deux états d’une photo. [...]

  2. [...] Source + Demo #dd_ajax_float{ background:none repeat scroll 0 0 #FFFFFF; border:1px solid #DDDDDD; float:left; margin-left:-120px; margin-right:10px; margin-top:-10px; top:10px; position:absolute; z-index:9999; }jQuery(document).ready(function($){ //put content div class, when scroll beyond this y, float it var $postShare = $('#dd_ajax_float'); if($('.dd_content_wrap').length > 0){ var descripY = parseInt($('.dd_content_wrap').offset().top) – 400; var pullX = $postShare.css('margin-left'); $(window).scroll(function () { var scrollY = $(window).scrollTop(); var fixedShare = $postShare.css('position') == 'fixed'; //make sure .post_share exists if($('#dd_ajax_float').length > 0){ if ( scrollY > descripY && !fixedShare ) { $postShare.stop().css({ position: 'fixed', top: 16 }); } else if ( scrollY < descripY && fixedShare ) { $postShare.css({ position: 'absolute', top: descripY, marginLeft: pullX }); } } }); } }); jQuery(document).ready(function($) { if($(window).width()> 790){ $('#dd_ajax_float').show() }else{ $('#dd_ajax_float').hide() } $(window).resize(function() { if($(window).width()> 790){ $('#dd_ajax_float').show() }else{ $('#dd_ajax_float').hide() } }); }); ; [...]

  3. [...] with jQuery jQuery Touchwipe jQuery Zoomooz Music player using jQuery jQuery Before/After Plugin ZURB Annotation plugin Crazy Dots – jQuery Plugin jQuery CSS Clip Animation Plugin Dynamic FAQ [...]

  4. [...] with jQuery jQuery Touchwipe jQuery Zoomooz Music player using jQuery jQuery Before/After Plugin ZURB Annotation plugin Crazy Dots – jQuery Plugin jQuery CSS Clip Animation Plugin Dynamic FAQ [...]

  5. [...] I decided to try this out myself.  The easiest solution I found (since I use WordPress) is to download the “Before/After Pictures” plugin.  It’s easy to use as it adds a new icon to the WordPress toolbar when posting.  There seems to be both Flash and Javascript-based tools out there for this effect, this one is based on Javascript.  The original jQuery code – for those that don’t use WordPress – can be found here. [...]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>