css - Toggle div on click or touch and hide on click/touch/scroll outside -


i need hide particular div press of button or toggle, basically: press once hide, press again show, press area of screen hide, , scroll hide. want hide/show rules done in css if possible. thanks.

html:

<input type="checkbox" id="toggle1" /> <div> <label for="toggle1" class="toggle1" data-open="open" data-close="close" onclick></label> <ul class="catmenu">   <li><a href="/">aaa</a></li>   <li><a href="/">bbb</a></li>   <li><a href="/">ccc</a></li> </ul> </div>  <input type="checkbox" id="toggle2" /> <div> <label for="toggle2" class="toggle2" data-open="open" data-close="close" onclick></label> <ul class="catmenu">   <li><a href="/">aaa</a></li>   <li><a href="/">bbb</a></li>   <li><a href="/">ccc</a></li> </ul> </div> 

css:

.header { position: relative; } #toggle1, .toggle1 { display: none; } .catmenu > li { display: none;  }  #toggle2, .toggle2 { display: none; }  .clearfix:before, .clearfix:after { display: table; content: ""; } .clearfix:after { clear: both; }  @media screen , (max-width: 767px){ .catmenu { display: none; opacity: 0; width: 100%; position: absolute; } .catmenu > li { display: block; width: 100%; margin: 0; } #toggle1:checked + div .catmenu { display: block; opacity: 1;}  #toggle2:checked + div .catmenu { display: block; opacity: 1;} }  .header{ min-height: 100px; height: 100%; padding: 0 20px; background: #ffffff; }  .header > h1 { float: left; padding: 30px 0 0;       font-style: italic; font-size: 28px; color: #dfdfdf; }  .nav{  display: block;  float: right;  }  .toggle1{  z-index: 2;  display: inline-block; width: 50px; float: left; }  .toggle2{  z-index: 2;  display: inline-block; width: 50px; float: left; }  @media screen , (max-width: 767px){  .catmenu{     border-top: 1px solid #51c1f1;     margin: 35px 0 0 0;     text-align: left;     background: none repeat scroll 0 0 #eeeeee;     padding: 14px 0 5px 2px;     font-family: "microsoft jhenghei", arial narrow, serif;     font-size: 20px;     line-height: 28px; }  .catmenu > li { display: inline-block; text-align: left; width: 47px; margin: 0px -1px 0 0; }  .catmenu > li > a{ display: block; padding: 0 0; color: #333333; }  .catmenu > li > a:hover, .catmenu > li > a:focus{ }  .toggle1:after {     content: attr(data-open);     display: inline-block;     width: 50px;     margin: 0 0 0 0;     padding: 0 0 0 0;     background: none;     -webkit-border-radius: 2px;     border-radius: 2px;     text-align: center;     font-size: 12px;     line-height: 35px;     color: #333333;     -webkit-transition: 0.5s linear;     -moz-transition: 0.5s linear;     -o-transition: 0.5s linear;     transition: 0.5s linear;     -webkit-box-sizing: border-box;     -moz-box-sizing: border-box;     box-sizing: border-box;  }  .toggle2:after {     content: attr(data-open);     display: inline-block;     width: 50px;     margin: 0 0 0 0;     padding: 0 0 0 0;     background: none;     -webkit-border-radius: 2px;     border-radius: 2px;     text-align: center;     font-size: 12px;     line-height: 35px;     color: #333333;     -webkit-transition: 0.5s linear;     -moz-transition: 0.5s linear;     -o-transition: 0.5s linear;     transition: 0.5s linear;     -webkit-box-sizing: border-box;     -moz-box-sizing: border-box;     box-sizing: border-box;  }  .toggle1:hover:after{     background: none;     text-align: center;     color: #333333; }  #toggle1:checked + div .toggle:after{     content: attr(data-close);     background: #45abd6;     text-align: center;     color: #ffffff; }   .toggle2:hover:after{     background: none;     text-align: center;     color: #333333; }  #toggle2:checked + div .toggle2:after{     content: attr(data-close);     background: #45abd6;     text-align: center;     color: #ffffff; } } 

hiding , showing divs based on user actions requires watching actions (events), not possible css alone.

you can following jquery show/hide div id div1 based on clicking element id toggle:

$('#toggle1').on('change', function () {     var div1 = $('#div1');     if (div1.is(":visible")) {         div1.show();     }     else {         div1.hide();     } }); 

if toggle1 checkbox , want show/hide coincide checked state, can this:

$('#toggle1').on('change', function () {     var div1 = $('#div1');     if ($(this).is(":checked")) {         div1.show();     }     else {         div1.hide();     } }); 

to hide on scroll:

$(window).on('scroll', function () {     $('#div1').hide(); }); 

to hide on clicking outside div:

$(document).on('mouseup', function (e) {     var div1 = $('#div1');     // if click not on div or descendant of div, hide div     if (!div1.is(e.target) && div1.has(e.target).length === 0)     {         div1.hide();     } }); 

update: here working jsfiddle: http://jsfiddle.net/yy2vm/


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -