javascript - Add cookies to toggle sidebar -


i have toggling sidebar on side, , want use cookies make remember state it's at. has been brought lot before, haven't been able find solution works code. (or maybe does, i'm new @ this, have used wrong.)

var main = function() {     $('.icon-menu').click(function() {         $('.menu').animate({             left: "0px"         }, 200);          $('body').animate({             left: "240px"         }, 200);     });      $('.icon-close').click(function() {         $('.menu').animate({             left: "-240px"         }, 200);          $('body').animate({             left: "0px"         }, 200);      }); }; 

i looked this ask, seems i'm looking for, codes different didn't work. same enter link description here viktor's helpful setup here - easier redo code more "standard"? or can set if command both menu , body?

grateful tips. cheers!

download , include js-cookie, , use follows:

$(document).ready(function() {     $('.icon-menu').click(function() {         $('.menu').animate({             left: "0px"         }, 200);          $('body').animate({             left: "240px"         }, 200);          cookies.set('menu-state', 'open');     });      $('.icon-close').click(function() {         $('.menu').animate({             left: "-240px"         }, 200);          $('body').animate({             left: "0px"         }, 200);          cookies.set('menu-state', 'closed');     });      // open menu (without animation) if open last time     if (cookies.get('menu-state') === 'open') {         $('.menu').css({             left: "0px"         });          $('body').css({             left: "240px"         });     } else {         $('.menu').css({             left: "-240px"         });          $('body').css({             left: "0px"         });     }; }); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -