Skip to content

Instantly share code, notes, and snippets.

@csessig86
Created February 22, 2013 23:10
Show Gist options
  • Save csessig86/5017312 to your computer and use it in GitHub Desktop.
Save csessig86/5017312 to your computer and use it in GitHub Desktop.
Before, after photos: Full demo code
<!DOCTYPE html>
<html>
<head>
<title>Before/after demo</title>
<style>
.trackMe img.beforeafterphotos {
top:0 !important;
left:0 !important;
position:absolute;
margin:0 0 15px 0 !important;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" language="JavaScript">
$(document).ready(function() {
$('.trackMe').each(function(){
$(this).children("img:last").mousemove(function(e) {
var offset = $(this).offset();
var xpos = (e.pageX - offset.left);
var ypos = (e.pageY - offset.top);
//now to get the first child image width..
var thisImage = $(this);
var thisWidth = thisImage.width();
var pct = Math.round((xpos/thisWidth)*100)/100;
var ipct = Math.abs(Math.round(((xpos-thisWidth)/thisWidth)*100)/100);
thisImage.css({ 'opacity' : ipct });
});
});
});
</script>
</head>
<body>
<div style="position:relative; width:600px; height:460px;" class="trackMe">
<img src="http://wcfcourier.com/app/special/beforeafter/beforeafter05b.jpg" class="beforeafterphotos" />
<img src="http://wcfcourier.com/app/special/beforeafter/beforeafter05a.jpg" class="beforeafterphotos" />
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment