This is a questions once asked by my class mate while studying the boring Physics class. We were sitting at the last bench while teacher were busy with Newton's Ring Experiment, we were discussing how to handle a popup menu. Finally I came out with two solutions.
Method #1
This solution is easy to use and good for popups. Actually this one works with a simple methodology of layer before popup. This layer will cover the whole screen. Once the user will click the layer behind the popup, the layer and popup will fade out.
How it works?
Take a website with a button that triggers popup menu.
<div class='popup'>This is the popup</div>Then CSS would be:
<div class='overlay'></div>
<div class='content'>This is the main content ....<span class='clickme'>Click Me</span></div>
.popup{
display:none;
position:fixed;
top:20px;
z-index:3;
}
.overlay{
display:none;
position:fixed;
width:100%;
height:100%;
left:0;
top:0;
background:rgba(0,0,0,0.6);
z-index:2;
}
So now we have a layout, once the button is clicked the popup and layer both will appear:
$('.clickme').click(function(){
$('.overlay,.popup').fadeIn();
});
Now behind popup a semi transparent layer is the closing agent for popup. If you just add an event on that layer to close the popup then user can easily close the popup by clicking on that layer. Works good specially in small screen sizes where do not have to find small close buttons to close popups.
Method #2
Second method is also quite easy and works well with every condition. This one works with the current position mouse. Have a look at demo first:
$(document).mouseup(function(e){Now this what I was taking about. This one works with every kind of situations.
var foo=$('.notification');
if (!foo.is(e.target) // Check if this is the container
&& foo.has(e.target).length === 0) // ... nor its child
{
$('.notification').removeClass('active');
}
});
Post A Comment:
0 comments: