Reputation: 341
This works fine. But now I want to add the functionality so that if the user clicks anywhere outside of the div, the div will go back to being hidden. One more question, can i use the same code IPade also...
How do I hide or close the div box by click outside?
<script type='text/javascript'>//<![CDATA[
$(document).ready(function () {
$("#focus").click(function () {
$('#hidden-item').toggleClass('focused');
});
});//]]>
</script>
<style type="text/css">
#focus {float: left; background: #333; color: #fff; padding: 5px;}
#wrap {
width: 100%;
position: fixed;
}
#hidden-item {
background: red; color: #fff;
position: absolute;
top: 50px;
left: 100%;
width: 100%;
margin-left:15px;
-webkit-transition: all 0.5s ease-in;
-moz-transition: all 0.5s ease-in;
-ms-transition: all 0.5s ease-in;
-o-transition: all 0.5s ease-in;
transition: all 0.5s ease-in;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
-ms-backface-visibility: hidden;
backface-visibility: hidden;
}
#hidden-item.focused {
left: 0;
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
</style>
</head>
<body>
<div id="wrap">
<p id="focus">Click to Show/Hide</p>
<div id="hidden-item">
Hidden div...
<br />
<br /><br /><br /><br /><br />
Line2
</div>
</div>
</body>
</html>
Upvotes: 1
Views: 3150
Reputation: 1681
You have to change left:0
to left:0px
in your toggle class.
Result using your code : http://jsfiddle.net/G5Yh4/1
For your issue change JS to :
var hover_focus = false ;
$(document).ready(function () {
$("#focus").click(function () {
$('#hidden-item').toggleClass('focused');
})
.mouseover(function () {
hover_focus = true ;
})
.mouseout(function () {
hover_focus = false ;
});
});
$(document).click(function() {
if(!hover_focus) {
$('#hidden-item').removeClass('focused');
}
}) ;
I think jQuery have a selector for "not hover", something like.not(':hover')
. you can check there : http://api.jquery.com/not/
Upvotes: 0
Reputation: 2281
try this selector $(":not('#wrap')")
$(":not('#wrap')").click(function(){
// hide your div here
});
Upvotes: 1
Reputation: 16764
See my question and the marked answer:
Close a customized box at ESC keypress with JQuery
I posted similar answer from Joffrey (with little modifications). Instead #modal-dialog
can be any selector of div which you want to close it. And instead of live
which is deprecated in new version of jquery, use on
.
$("html").on("keyup", function(e) {
if(e.keyCode === 27 && !($('#modal-dialog').hasClass("no-display")))
escape_check();
}
}
function escape_check() {
// here, reset any fields at closing box (if you have any)
$("#modal-dialog").removeClass("no-display");
$("#feedback-modal-dialog input").each(function() {
$('#modal-dialog').attr("value","");
});
$("#feedback-modal-dialog textarea").each(function() {
$('#modal-dialog').val("");
});
$("#modal-dialog").addClass("no-display"); //or .hide()
}
Upvotes: 0