0

I have used a simple css and js for showing an image as popup when an image is clicked used and close button on top to close the image. Both openimage and closeimage are js functions i have implemented as
open image function as

function imgv(x,y,img)
{
     document.getElementById('imgv_i2').src = img;
     document.getElementById('imgv_i2').style.width = x;
     document.getElementById('imgv_i2').style.height = y;
     document.getElementById('imgv_i2').width = x;
     document.getElementById('imgv_i2').height = y;
    imgToMiddle('imgv_i2');
    document.getElementById('imgv_i2').style.display = 'block';
}

close image as

function closeimgv()
{
    document.getElementById('imgv_i2').style.display = 'none';
    document.getElementById('imgv_close').style.display = 'none';
}

event as

<a href="javascript:void(0);"  onclick="imgv(101,101,'image url');"><img src="image src"></a>

now for closing image when clicked on outside i have used these like

$('#imgv_i2').click(function(event){
    event.stopPropagation();
});
$('html').click(function() {
if(document.getElementById('imgv_i2').style.display == 'block') {
            closeimgv();
    }
});

now what happens is when click for opening image triggered also the close image function also triggered how can i make the $('html').click(); bind after the open image function triggered and unbind after close image function. Thanks.

Community
  • 1
  • 1
Sathya Raj
  • 1,079
  • 2
  • 12
  • 30

2 Answers2

0

Try

$(document).click(function(ev) {
    //avoid clicks on image itself
    if($(ev.originalTarget).is("whatever triggers image opening")) {return;} 

    if(document.getElementById('imgv_i2').style.display == 'block') {
            closeimgv();
    }
});
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
0

I would recommend you to use jquery plugins for thick box or popup as they are very simple and easy to implement and customize. There are lots of open source plugins available try them out and they do provide the type of customization you are asking for !

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
  • s i know but some of them what i have tried were having more features than i needed and also it would be too much file to include for a simple image popup ... if you have tell me any simple popup plugins ???.. i too load those images as ajax so i have bind their plugin event every time i load that ll also be overkill – Sathya Raj Oct 28 '13 at 08:40
  • @SathyaRaj You have to just include only one js file for the plugin and all of them supports the image loaded via ajax and most of them are so light weighted to be used on mobile devices – Rahul Gupta Oct 28 '13 at 08:51