2

I am using the to implement a popup window using zIndex... things work well but I want to implement a function, that is when user click any region that is outside of the popup div, the div will be closed, how to do that?

Bin

Bin Chen
  • 58,165
  • 53
  • 139
  • 181
  • 1
    I'm interested in this question somewhat. I just trigger a click in the document, then check the mouse position to see if it is inside the box. Interested to see other solutions. – Jason Oct 28 '10 at 05:55
  • how can you get the actual size of the div? can you share with me? – Bin Chen Oct 28 '10 at 05:57

3 Answers3

2
(function($){
   $.fn.outside = function(ename, cb){
      return this.each(function(){
         var $this = $(this),
              self = this;

         $(document).bind(ename, function tempo(e){
             if(e.target !== self && !$.contains(self, e.target)){
                cb.apply(self, [e]);
                if(!self.parentNode) $(document).unbind(ename, tempo);
             }
         });
      });
   };
}(jQuery));

That is a copy&paste code from

mouse click somewhere else on page (not on a specific div)

Community
  • 1
  • 1
jAndy
  • 223,102
  • 54
  • 301
  • 354
1

An easy way to do this is to cover your page with a transparent (could also be completely transparent) mask using a div.

Attach a click handler on this mask div to close out the popup.

See example here: http://www.sohtanaka.com/web-design/examples/modal-window/

Moin Zaman
  • 24,919
  • 6
  • 67
  • 73
1

Here is an example, using the Event.target (cross browser).

http://jsbin.com/eqeto3/edit

Strelok
  • 48,156
  • 8
  • 98
  • 113