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.
Genius!!! Perfect for what I was looking for.
Hi,
Great plugin!
I need to keep the size of the jQuery-UI to a minimum. Would you be able to tell me which components are necessary so that I can build a custom file?
Thanks!
MSabene
Just draggable.
Great! Much smaller now…
Thanks for your prompt response.
Hi, I am having a hard time getting options to work. When I have the following code, I get nothing on screen. All my images are blank, so I am guessing I have bad code somewhere.
$(function(){
$(‘#container’).beforeAfter({
animateIntro : true,
introDelay : 2000,
introDuration : 500,
showFullLinks : false,
});
};
Oh, and I had another thing too. For some reason the .gif files aren’t loading for the slider bar and left & right arrows. I just have the vertical bar …. Any thoughts there, too?
Thanks
I was able to figure this out.
The images needed a ./js/beforeAfter in the script var section, that way I can put the js folder in a subdirectory.
Also, the syntax had too many semi colons.
where do i extract and upload jquery.beforeafter.zip on my site?
and can this be used in joomla?
Hi y’all. With admin’s approval, I’ve added a few features to the code and have wrapped it up as a wordpress plugin. I just posted a long explanation of the changes and a download link at my blog.
The alterations allow the use of an arbitrary number of beforeAfter containers per page, with user settings snarfed from html5 custom attributes in the container. I’ve also added “wipeto”, “beforetext”, and “aftertext” variables so one can set the start point of the control widget (where 0 is the image’s left edge and 100 is its right), and so one can specify the text used for the links below the container. These settings default to their original behavior. More information and an example container can be found in my blog post. I’d crosspost the info here, but it’s very long.
Honestly after looking at the code, things like this takes minutes to do in Flash.
Minutes? Yup, and it takes minutes for flash to load also…………
Sometimes the plugin loads without the before/after images
does anybody know how I can fix it?
admin, any suggestions of any other JQuery plugins I could integrate this with, to use the BeforeAfter with a gallery..? Thanks.
@David,
I haven’t tested this with other plugins yet but that’s an idea for the next version if the current version can’t do it.
Thanks for FAST reply!
Can I use ../1.4.2/jquery.min.js or are there changes from ../1.3.2/jquery.min.js
so that the new version would produce errors?
1.4.2 works fine with it.
@admin
OK playing around with Sudo Slider plugin, as an option to have multiple BeforeAfter images on the same page.
Example:
It is working for the most part…looks like there a few JS conflicts when changing slides…I will keep playing with it…
Question is: How might I move the “show before” and “show after” buttons above the actual images..?
Been rearranging the function [o.showFullLinks] around in the jquery.beforeafter.js file, but I haven’t had success moving the buttons to the top of the page. Any suggs? Thanks :-)
*This combo looks great BTW………
Hi,
With the latest update of jQuery (1.4.3) the slider in Before and After can’t be dragged anymore in Safari 5.0.2. Works fine in Firefox, though…
Any suggestions?
Thanks!
MSabene
Seems to happen in Chrome too. I think I have a fix for this but I want to also see if I can streamline this a bit more while I’m looking at it. Thanks.
Thanks! I appreciate your quick response.
OK, the zip file has been updated and it should fine now in webkit. I’ve also updated the demos to run with jQuery 1.4.3 and the latest version of jQueryUI. I’ll add a new post a bit later.
Do you have any detailed instructions on how to install and create pages with this plug-in? I have uploaded the .zip to my site ftp, but that’s as far as I got. I’m still new to this wordpress plugin stuff so any help is appreciated.
Great, thanks! I’ll update my files…
Which file can I edit to reposition the “show only before/after” links. Thanks.
Is there a way to turn this into a gallery that slides from one before/after set to the next?
i would also be interested if i can use this plugin for more than one pairs of before/after pictures?
thanks!
I thought someone earlier might have posted a way to do this but by default, no, there’s no option to have a gallery of before/afters. Could be a feature in a future version.
@admin
When centering your jQuery Before/After Plugin your slider position code does not compensate. It does not slide correctly in IE, FireFox, Chrome (all PC).
I’ve tried adding some code to jquery.beforeafter “var PositionConflict = 211 + 256; /* 211 : can’t explain / 256 : column 1 width */” & ” var clickX = e.pageX – this.offsetLeft – PositionConflict;”, but it does not help.
Can you fix this or can we not have your plugin centered..??
@David
You new to CSS? Works fine if you center the container with margin: 0 auto. BTW you should try things before blaming others for simple problems. CMF’s plugins rock!
Cool plungin! Thanks for sharing. I hope to see more from you.
HI, thanks for the plugin.
How would I add to default to the middle when mouseout?
Very Nice . I am going to use in my “trouwringen” site
Wow this one is amazing.
nice!
only one thing ..i want to change style of ‘Show only before’ and Show only after’. ….. how to change or wanna add my own css ( for fonts style). please help
when using the latest version of jQuery, the click part does not work because of line ~123 in the file.
it says
var clickX = e.pageX - this.offsetLeft;which uses the deprecated offsetLeft.change it to
var clickX = e.pageX - $(this).offset().left;using the offset() functionto make it work.
Seems to work fine in all major browsers (including IE8). Are you using the latest version on the plugin?
Using the last version of the plugin on Google Chrome.
When alerting the offsetLeft, the value always turned up as a negative value.
It’s a great plugin, with some lovely little touches. However it’s a shame that it doesn’t seem to be actively maintained and people in this thread have identified various issues which need to be addressed.
Admin, please consider hosting this plugin on GitHub so that we can all collaborate on improving it by contributing fixes, raising issues, etc. Thanks in advance!
Please elaborate on what you believe needs to be fixed. It’s quite actively maintained, however I don’t have the time to respond to each and every request. Thanks.
i am fresher in js ..I download jquery.beforeafter.zip.. my problem i dont know how to integrate.
pls help me
This is amazing. great!
Very good . I am going to use in my http://www.almekhlafi.com site