javascript - Toggle menu on click of two elements -


var main = function() {     $('#menu-open').click(function() {         $('.menu').animate({ left: '0px' }, 200);         $('body').animate({ left: '285px' }, 200);     });     $('#menu-close').click(function() {         $('.menu').animate({ left: '-285px' }, 200);         $('body').animate({ left: '0px' }, 200);     }); };  $(document).ready(main); 

i want able close menu click of #menu-close or #menu-open when menu open. how can this?

you can bind single function both open/close button. inside function, check menu status. example:

$('.menu-open, .menu-close').click(function() {     var left = $('.menu').offset().left;      if (left < 0) {         $('.menu').animate({ left: 0 }, 200);         $('body').animate({ left: 285 }, 200);     } else {         $('.menu').animate({ left: -285 }, 200);         $('body').animate({ left: 0 }, 200);     } }); 

edit

this updated fiddle:

https://jsfiddle.net/mahdaen/jsf0oc99/3/


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -