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.

471 Responses to “jQuery Before/After Plugin”

  1. Paul says:

    Is it possible to re-size the handle in the middle. I tried making the image bigger but it is being constrained by a size somewhere that I cannot find.

    Any help would be great…

    thanks,

  2. Tom Cavill says:

    This is brilliant, I’d been searching for a while but wasn’t sure what the plugin (if one existed) would be called. Before and After makes a lot of sense.

    How would one go about having the slider slide based on mouse position, without requiring a click / drag? So the slider would slide on mouseover of the image(s)? I’m thinking this would be a great feature for a header section of a homepage, and cause some delight to the user when they notice the transition between old and new on mouseover.

    Thanks.

    • admin says:

      Thanks. One early version I tested had this functionality but people really disliked it because they felt they didn’t have as much control as they did when dragging. You can hack the plugin to do this however, basically replacing the drag function with mouseovers.

  3. Dan says:

    Hi, thanks for making this plugin, it is just what I need. I have a page setup that has two instances of your plugin one for mobile and one for desktop. I have a problem, when I load it up on mobile, a weird green streak appears on the images. This looks like an error on the handle.gif. I was wondering if you had any ideas to fix it?
    Link: danielcreese.co.uk/project_amethyst.html

    Thanks,
    Dan.

  4. Zeeshan says:

    Dear Admin,

    I have made picture frame and it is working fine…..Thank you so much for this outstanding plugins. I would like to know, how i can fit picture on my page side by side (Right and Left) its only showing top to bottom & second to change the handle color…..pls reply

    Reg
    Zeeshan

  5. J Hart says:

    Not sure if this has been asked before, but it is possible to have the option of slider moving up and down instead of left and right? Thanks!

  6. 5280Newbie says:

    Great plugin, as a newbie I am struggling. Any help would be great.

    What am I missing to ge this to work? http://www.5280av.com/beforeafter.html

  7. I’m trying to use this great plugin in my cargocollective website, but I’m not able to make that this work properly. Have anybody used it in this website and can anybody gives the way to use it.
    I think this is the best plugin to show my photo retouching work.
    Best Regards

    • admin says:

      Cargo collective?

      • Yeah… Cargocollective.com is a website that gives accesible tools and networked context to talented individuals… It’s used to photographers, designers, architecs and artist in general… It’s only accesible by invitation.
        It gives the opportunity to artist to show his works. Their support use templates but it’s highly configurable. I have tried to add your script inside the customization tools (CSS and HTML) But I’m not able to to make it run. I know other people use your script and I’m asking to the cargocolective support, but u have answer faster :)
        I guessed u could give me some indications to implement your plugin, but if you have never heard about it I think must be difficult…
        I will share the solution with you when I’ll find it to give support other user os this website
        Best regards

  8. Greg says:

    Fantastic plug-in. I’m running into an issue I don’t know how to resolve though… rather than progressively hiding the foreground image, when i move the back and forth, my before image is compressed and expanded. any suggestions? see page for example: https://greg-mills-s7vn.squarespace.com/before-after
    Thanks!

  9. Dan says:

    Anyone know how to get this to work with Google Sites? I tried making a new page as a file-container named js and uploaded all the files there, then create a HTML box on another page but the boxes only allow (for example):
    http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
    http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js

    It won’t load the before-after plugin because of security reasons apparently after doing some reading. The only way to get it to work is by making a gadget, but I don’t quite understand what to do.

    Any suggestions would be appreciated.

  10. Taylor Young says:

    Hello! We saw your plugin in action on this page (http://tmagazine.blogs.nytimes.com/category/model-morphosis/) and would like to use it on an e-commerce site. I see your CC license forbids commercial use. What do we need to do to get a license for the software that we can use?

    We have a really cool idea involving black and white to color transitions.

    Thanks,
    -Taylor

  11. Wyck says:

    How can we use this, up to down ?? Not right-left.

  12. Stefan says:

    Hi,

    this is a very good script! For my usecase I have implemented the options MousemoveAnimation and ShowDragHandle.
    Thanks!

  13. Doug says:

    Is it possible for the images to be centred, if so, how?

    Thanks :)

  14. Doug says:

    Can the images resize automatically (on a responsive design website)?

    • Pierre says:

      Hi Doug,
      Did you finaly found a way to make this Jquery responsive? I’m trying to insert it in a bootstrap framework and it’s not working properly.

      Thanks,
      Pierre

  15. Ryan says:

    I have successfully gotten everything working with the before and after slider, however, after setting the animate intro setting to true, it does not animate. I did make a change regarding picture size which is set at 800px X 535px. I thought perhaps the larger image size affected it but not settings I change make a difference. Why would the animate intro not work?

    • admin says:

      Without seeing an example I have no idea. Are you getting any errors?

      • Ryan says:

        No errors that I see. It just does not slide over from the right side and stop at center. I am using google chrome.

        Also, when trying to put this in let’s say a single page promo site, I cant get the images to show up. They are there, but not visible. I am going to try smaller images and see if that helps, but any information you can provide in making sure it works in a page would be greatly appreciated.

        https://www.dropbox.com/sh/znnnrx911e8q2qz/N5xeRclCWb

      • admin says:

        Sorry, I’ll need to see actual code, not pictures, to troubleshoot.

  16. Chris says:

    Can someone give me a step by step as to how to incorporate Touch Punch to a BeforeAfter page? I am not exactly sure where to put the code and snippets.

    Thanks!
    Chris

  17. chris says:

    hey,
    really nice plugin. How can i use this, up to down ?? Not right-left. Is it possible? what i have to change in the code?

    thx

  18. Nick W. says:

    fantastic slider! Question: can the slider be setup with a class instead of an ID and still function? I would like to display multiple sliders on the same page. Is there a performance reason for setting up an ID instead of a class perhaps? Thank you

  19. Landry says:

    Can you, or someone, post a link to an example of this plugin in action using before and after photos that don’t line up perfectly? I’d like to see if the transition effect still looks good when used with more amateur photos. Thanks!

  20. devang says:

    i am into image retouching and image manipulation. i donr know scripting or html. can somebody help work this sliders on my images?

  21. sarah says:

    Hi,

    I would like to use a event after before for my site site on the page design.
    Do you have advice ? I need help

    Thanks

    Sarah

  22. Michele says:

    Is there any way to change the color of the green part of the slider? Dumb question I know .. jw

  23. craig says:

    I was wondering if this would work with videos? thanks!

  24. Miguel says:

    Hi!. We are using your slider for our project and it works perfect! We really appreciate that you share your knowledge and free this gadget We had a project to use aerial pictures to describe the housing bubble in spain and its impact on the territory and your script it´s the perfect tool for it!

    Best Regards, Miguel

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. [...]

  6. [...] b4/after suggestions the original one with useful caption fancy advanced one (consider the possibilities!)Jake & Dan on June 20th, [...]

  7. [...] you can switch between the 2 images. It’s a pretty cool plugin developed by a guy behind CatchmyFame. If you want to integrate this jQuery slider plugin you must know that it uses fix width images, so [...]

  8. [...] After plugin integrates modified version jQuery Before/After Plugin in the wordpress. It can be used to show the difference between edited and original photo, before [...]

  9. [...] After Pictures plugin integrates jQuery Before/After Plugin in the wordpress. It can be used to show the difference between edited and original photo, before [...]

  10. [...] interaction was built with a Creative Commons jquery plugin by Catch My Fame. I just looked at the demo, and it seems like it would actually be really easy to [...]

  11. [...] 156. jQuery Before/After Plugin This works like a sliding door in our homes. When you drag the navigation button, you expose the background of the image. [...]

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

  13. [...] 访问该插件 26. Simple I tune Slider 访问该插件 27. Jquery Before And After Plugin 访问该插件 28. Jquery Slider Kit 访问该插件 29. Parallax Slider Kit 访问该插件 30. Cloud Zoom [...]

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>