jQuery Before/After Plugin

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.

506 thoughts on “jQuery Before/After Plugin”

  1. 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

      1. 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

  2. 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.

  3. Hi,

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

    1. 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

  4. 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?

      1. 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

  5. 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

  6. 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

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

  8. 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!

  9. 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

  10. I like the effect and wanted to experiment with it on a testbed I keep. I have had several difficulties some of which I was able to overcome (I think) but it is still not working. See the test page at:
    http://www.keptlight.com/testblog/?p=1

    Here are the steps I followed:
    1. I downloaded the ZIP file, extracted the folder beforeafter
    2. Uploaded the folder to the plugins folder on my WordPress site
    3. It did not show up as a plugin per se, but I continued on
    4. I added the header information I copied from this site
    5. I saw two images stacked on top of each other
    6. I looked into the folder and noticed that the JS file name was not what I copied and pasted in the code. So I changed it to conform to one of the names, jquery.beforeafter-1.4.js
    7. I ended up the effect producing a similar but a strange effect, instead of hide-reveal of full images it seems to stretch one image as you see
    8. I also do not see the slider handle, probably a CSS issue pointing to the right image file is needed but I do not know where the CSS declaration may be.

    Any help you could offer will be greatly appreciated. Thank you

    1. Update: I have found the hard-coded path in the JS file and modified it to reflect where it is. Now the image of the handle is there but is very slim. The arrows show up too. The main problem still remains that the image is stretched-squeezed rather than covered-revealed.

      1. I have figured out the problem finally! There was a declaration in the theme CSS which specified max-width for the images in the content area, 98%. I disabled that and the B&A is working now. Thank you for making this available for us to use. If it were a fully developed WordPress plugin by the original author, that will be a welcome addition to my collection.

      2. A different kind of problem now. I cannot implement a second instance. I tried the following:
        1. Used “container” ID for both, only the first one works
        2. Used “container” and “container1″ IDs, only the DIV with “container” worked regardless the ordering of the divs.
        You can see the two sets of images where one works and the other does not:
        http://www.keptlight.com/testblog/?p=1

        I feel so close, yet not there yet ;-)

      3. I peeked into your example 2 code and realized that you are declaring the container# several times. I replicated the idea on my example and they are both working now. The multiple images per page requires a function like below, thanks again:

        $(function(){
        $(‘#container1′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container2′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container3′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container4′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container5′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container6′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        $(‘#container7′).beforeAfter({imagePath:’wp-content/plugins/beforeafter/js/’});
        });

      1. You will find the steps I followed in this comment thread, start with my first one and follow my steps in the subsequent comments, essentially replies to myself so that others reading them will learn how I threaded the needle. Maybe I should one day write a post about this. I wish someone could develop this into a WordPress plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *

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