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.

503 thoughts on “jQuery Before/After Plugin”

  1. This is Great work!!!

    If I had two suggestions to make:

    it would be nice to make the green button in the middle slide up and down along the ridge, thus preventing it from obstructing the part of the image you are looking at (you could also have it, the mouse, and the ridge become transparent when you keep the mouse button pressed),…

    otherwise, you could have 4 images instead of 2 (for example, to display a scene during 4 seasons), and have both vertical and horizontal sliding (i don’t know if that makes sense)

  2. I’m having problems getting the handle and left/right png images to show up. I didn’t change anything in the code, and I didn’t move them. I see someone mentioned deleting ” ‘+o.imagePath+’ ” but that doesn’t make any difference either. It has to be something to do with the path of the image, can anyone help? I’ve even tried to put a direct link in there….

  3. Oh, this tool is great. I’m only having one trouble: the tiny images for handle.gif, lt-small and rt-small.png are not displaying properly. Thanks!!!

    1. Hi Carlos,
      Did you ever solve the handle.gif, lt-small and rt-small.png not displaying properly? Am having the same issue. Thanks

      1. Hi. Just change “/js/beforeAfter/” to “./js/” (line 22 of jquery.beforeafter.js) to solve this issue, assuming you have the “js” folder in the same folder of the html file. Attention to the dot: “./js/” instead of “/js/”

      2. Great plugin, exactly what I’m looking for. I’m still having issues with the handle displaying, though I’ve tried every suggestion offered here. As of now I have everything in one folder my js, HTML and images. Other than that it works perfectly just no handle image.

  4. Would it be possible to make the animation completely automatic, even without the drag component? I’d just like that it would perdiodically move from “before” to “after”, wait a bit, move back to before, rinse and repeat. Any suggestions please how to do it? I guess similar to thew animateIntro part it could be done hopefully without too much work.

  5. hi.
    thanx for the awesome tutorial!really :) . . . but :D ( there’s always a but i guess :) )
    i’m using drupal and added the before.after.js in my theme and everything seems to work ok except the the dragable effect the links ‘show only the before’ or ‘show only the after’ works fine the images are loaded to i used this code in order to load correct ( based to my relevant
    path ofcourse )

    $(function(){
    $('#beforeafter').beforeAfter({
    imagePath:'ba/'
    });
    });

    but when i go to firefox’s error console i get this error

    Error: $("#dragwrapper" + randID).draggable is not a function
    Code: http://localhost/skia/sites/default/files/js/js_a871d7a3c2a74e3c88406cc38285ea6c.js
    Line: 3308

    As far as i know i’ve not made any other customizations expect the image path , do you know what this could be ?
    thanx!

  6. Great. Does this work on an iPad? I’m working remote and don’t have one in front of me today. It seems that is should. If not, how hard would it be to make it work?

    Thanks!

      1. hi folks,
        does any of you have any tips on how to integrate this plugin in a wordpress theme? It looks really nice, especially the effect of being able to drag it and I would love to integrate it on my site.
        thanks for any tips!
        -fabian

      2. Really worked on iPad!
        Just some links missing like images and stuff… but anyone can correct this… Lol

        Thanks for the code and hints man!! That`s just what i`m looking for!

  7. Hi, I love this thing. I built one in Flash awhile back but now I’m being forced to migrate to something non-Flash and this is exactly what I need.

    However, I noticed that when one clicks and drags rapidly sometimes the button/divider bar doesn’t line up with the image devision. Can you tell me why this is happening? Thanks again for this!

    -jonathan

  8. I would like to have the before and after animate back and forth prefeably with a pause in between. Can you help?

  9. Hey – this is *awesome*. I was wondering if you would be interested in modifying the code to allow clicking in the container to follow a “story” link – so, I would wrap the images in an a href tag, and then, if a user clicks on the images I could load a page describing the before/after image in detail. Thank you for the post in any event!

  10. It doesn’t seem that I’m able to use a relative path for the handle image. I’m trying:
    imagePath : '../images/',
    but the two dots are being printed as they are while page render:
    src="../images/handle.gif"

    Am I missing something?

    1. To solve the missing images problem:

      Go to this file: beforeafter/js/jquery.beforeafter-1.3.js (or the minified version).

      Under the var defaults (at the beginning of the file) where you see imagePath :…the path that should be there MUST be a *relative path* to the image files as if you were on the *HTML page* where you’re applying the plugin… it should NOT be relative to the beforeafter/js/jquery.beforeafter-1.3.js, as I was thinking.

      In other words, how you linked this file: “beforeafter-1.3.js” in the head of the HTML page should be the path you should use for imagePath :

      For example:
      If this is my HTML document,
      <head>
      <script type=”text/javascript” src=”../../js/beforeafter/js/jquery.beforeafter-1.3.js”></script>
      </head>

      the beferafter-1.3.js should read as follows:
      var defaults =
      {
      animateIntro : false,
      introDelay : 1000,
      introDuration : 1000,
      introPosition : .5,
      showFullLinks : true,
      beforeLinkText: ‘Show only before’,
      afterLinkText: ‘Show only after’,
      imagePath : ‘../../js/beforeafter/js/’,
      cursor: ‘pointer’,
      clickSpeed: 600,
      linkDisplaySpeed: 200,
      dividerColor: ‘#888′,
      onReady: function(){}
      };

      Hope this helped!

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>