Cant get Jquery to remove and add class on browser resize correctly -


can't seem jquery working way im trying to.

i'm using font-awesome icons.

basically, want "hamburger" menu change "x" symbol on browser resize people can hide there menu if @ larger screens.

the problem if leave jquery code way was, show hamburger menu @ larger screens, , when clicking hide menu, show x. want opposite on larger display. show x first on larger displays "hide menu" , bar car show again.

here code. (stripped , altered simple possible)

<div class="nav-bar">   <div class="drop-menu">     <ul>       <li><span class="menu-text">hide </span>menu</li>       <li><a name="ham-menu" id="ham-menu"><i class="fa fa-bars"></i></a></li>     </ul>   </div>   <nav>     <ul>       <li>link</li>       <li>link</li>       <li>link</li>       <li>link</li>     </ul>   </nav> 

css >

li {   list-style: none;   display: inline-block; }  .menu-text {   display: none; }  nav {   display: none; } @media , (min-width: 500px) {   .drop-menu span {             display: inline;         }   } } 

jquery >

jquery(document).ready(function($) {       /* drop down menu , toggle css class change 3 bar icon x icon on click */       var $window = $(window),             $hammenu = $('#ham-menu i'),           $nav = $('nav');        $('#ham-menu').on('click', function(event) {          event.preventdefault();          //$('nav').fadetoggle();         $hammenu.toggleclass('fa-bars fa-close');         $nav.slidetoggle();       });        /* code changes 3 bar menu icon "x" icon on browser resize past 500px */        function resize() {     if($window.width() >= 500) {         return $hammenu.removeclass('fa-bars');     }          $hammenu.addclass('fa-close');     }      $window         .resize(resize)         .trigger('resize');        }); /* end jquery */ 

my onclick event works fine when itself. drops nav menu down , changes 3 bar menu x.

the issue when add in 2nd part of code changes 3 bar icon x when browser greater width. want give user ability close menu if desire on larger display if have js enabled.

when added in resize function found in post here on so, if totally glitches menu , doesn't work right @ all.

here nav menu working fine 2nd part of jquery removed.

working navigation drop down

here menu not working @ when try add in code switch bar x icon on larger displays.

navigation not working anymore

the icon have x @ larger displays, there isn't 3 bar icon anymore @ mobile size. when click on 3 bar icon on mobile, removes , doesn't show x icon anymore. you'll see mean when @ fiddle. i'm missing easy.

how icon change 3 bar icon on mobile view. i'm lost.


update 1 >


here working code, simpler thought be.

  $('#ham-menu').on('click', function (event) {          event.preventdefault();          //$('nav').fadetoggle();         $hammenu.toggleclass('fa-bars fa-close');         $nav.slidetoggle();     });      /* change out 3 bar menu cross start on nav menu allow user close menu */     if ($window.width() > 750) {         return $hammenu.toggleclass('fa-close fa-bars');     } 

this works load browser @ different widths. let's wanted have classes "switched" taking browser , manually changing width of it, how that?

should resize event handler if / else, or want re-add fa-close class?


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -