Created
December 28, 2017 18:51
-
-
Save anwerashif/ba7cbbb0feed3e5b7c46c3f507f11f7c to your computer and use it in GitHub Desktop.
jQuery Modal Image
This file contains hidden or 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
jQuery( document ).ready(function(){ | |
// Add Modal HTML to body | |
jQuery('body').append( "<div id=\"myModal\" class=\"modal\"><span class=\"close\">×</span><img class=\"modal-content\" id=\"img01\"><div id=\"caption\"></div></div>"); | |
// Get the image and insert it inside the modal - use its "alt" text as a caption | |
jQuery(".img-frm img").on("click", function(){ // Change the class name ".img-frm img" with yours | |
var image = jQuery(this).attr("src"); | |
var captext = jQuery(this).attr("alt"); | |
jQuery(".modal").css("display","block"); | |
jQuery("#img01").attr("src", image); | |
jQuery("#caption").html( captext ); | |
// When the user clicks on <span> (x), close the modal | |
jQuery("#myModal .close").on("click", function(){ | |
jQuery(".modal").css("display","none"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment