Created
March 8, 2013 19:37
-
-
Save jshaw/5119184 to your computer and use it in GitHub Desktop.
Optimized image swapping.
jQuery required
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.button { | |
color: blue; | |
text-decoration: underline; | |
cursor: pointer; | |
} | |
#img1, | |
#img2 { | |
float: left; | |
margin: 5px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p class="button swapimage_ele">swap image element</p> | |
<p class="button swapimage_src">swap image src</p> | |
<br /> | |
<div id="img1"> | |
<img src="http://farm8.staticflickr.com/7244/7339763896_6b3a7487fe_s.jpg" /> | |
</div> | |
<div id="img2"> | |
<img src="http://farm9.staticflickr.com/8016/7339765048_d83ffbb15d_s.jpg" /> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.swapimage_ele').on("click", function(evt){ | |
var img1 = $("#img1").html(); | |
var img2 = $("#img2").html(); | |
$("#img1").html(img2); | |
$("#img2").html(img1); | |
}); | |
$('.swapimage_src').on("click", function(evt){ | |
var img1 = $("#img1 img").attr("src"); | |
var img2 = $("#img2 img").attr("src"); | |
$("#img1 img").attr("src", img2); | |
$("#img2 img").attr("src", img1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment