Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Responsive Menu

Tags:

jquery

css

So i created a responsive menu but it has some weird behavior. Example of fiddle

$('ul.page-menu-nav').each(function () {
  var $ul = $(this);
  var $links = $(this).find('a');
  var $active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);

  $active.addClass('active');

  var $content = $($active[0].hash);

  $links.not($active).each(function () {
    $(this.hash).hide();
  });

  // Bind the click event handler
  $(this).on('click', 'a', function (e) {
    debugger;
    if($(this).parent().hasClass('icon')) {
      return;
    }

    $ul.removeClass('responsive');
    $active.removeClass('active');
    $content.hide();

    $active = $(this);
    $content = $(this.hash);

    $active.addClass('active');
    $content.show();


    e.preventDefault();
  });
});


function toggleMenu() {
  var $nav = $("#myTopnav");

  if ($nav.attr('class') == "page-menu-nav clearfix") {
    $nav.addClass('responsive');
  } else {
    $nav.removeClass();
    $nav.addClass('page-menu-nav clearfix');
  }
}
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */


ul.page-menu-nav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  color:#333;
  border-bottom: 3px solid #e6e6e6; 
  min-height: 48px;
}
ul.page-menu-nav li a.active {
  border-bottom: 3px solid #337ab7;
}

ul.page-menu-nav li {float: left;}

ul.page-menu-nav li a {
  display: inline-block;
  color:#333;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  margin-bottom:-3px;
  position:relative;
}

ul.page-menu-nav li a:hover {
  border-bottom: 3px solid #337ab7;
}

ul.page-menu-nav li.icon {display: none;}

@media screen and (max-width:680px) {
  ul.page-menu-nav li:not(:first-child) {display: none;}
  ul.page-menu-nav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.page-menu-nav.responsive {position: relative;}
  ul.page-menu-nav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.page-menu-nav.responsive li {
    float: none;
    display: inline;
  }
  ul.page-menu-nav.responsive li a {
    display: block;
    text-align: left;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="page-menu-nav clearfix" id="myTopnav">
  <li><a class="active" href="#home">Publish</a></li>
  <li><a href="#news">Post Activity</a></li>
  <li><a href="#contact">Content Library</a></li>
  <li><a href="#about">Post Calendar</a></li>
  <li><a href="#about">Profile Manager</a></li>
  <li class="icon">
    <a href="javascript:void(0);" style="font-size:15px;" onclick="toggleMenu()">☰</a>
  </li>
</ul>

When in responsive mode i need the active item to show, right now Publish always shows when you select another item

What would be the best way to accomplish this?

like image 740
R4nc1d Avatar asked Dec 06 '25 18:12

R4nc1d


1 Answers

Please check below snippet. It is working as your requirement.

$('ul.page-menu-nav').each(function () {
  var $ul = $(this);
  var $links = $(this).find('a');
  var $active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);

  $active.addClass('active');

  var $content = $($active[0].hash);

  $links.not($active).each(function () {
    $(this.hash).hide();
  });

  // Bind the click event handler
  $(this).on('click', 'a', function (e) {
    debugger;
    if($(this).parent().hasClass('icon')) {
      return;
    }

    $ul.removeClass('responsive');
    $active.removeClass('active');
    //$active.parent().hide();
    //$content.hide();

    $active = $(this);
    //$content = $(this.hash);

    $active.addClass('active');
    //$active.parent().show();

    $("#myTopnav li:eq(0)").before($(this).parent());
    e.preventDefault();
  });
});


function toggleMenu() {
  var $nav = $("#myTopnav");

  if ($nav.attr('class') == "page-menu-nav clearfix") {
    $nav.addClass('responsive');
  } else {
    $nav.removeClass();
    $nav.addClass('page-menu-nav clearfix');
  }
}
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */


ul.page-menu-nav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  color:#333;
  border-bottom: 3px solid #e6e6e6; 
  min-height: 48px;
}
ul.page-menu-nav li a.active {
  border-bottom: 3px solid #337ab7;
}

ul.page-menu-nav li {float: left;}

ul.page-menu-nav li a {
  display: inline-block;
  color:#333;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  margin-bottom:-3px;
  position:relative;
}

ul.page-menu-nav li a:hover {
  border-bottom: 3px solid #337ab7;
}

ul.page-menu-nav li.icon {display: none;}

@media screen and (max-width:680px) {
  ul.page-menu-nav li:not(:first-child) {display: none;}
  ul.page-menu-nav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.page-menu-nav.responsive {position: relative;}
  ul.page-menu-nav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.page-menu-nav.responsive li {
    float: none;
    display: inline;
  }
  ul.page-menu-nav.responsive li a {
    display: block;
    text-align: left;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="page-menu-nav clearfix" id="myTopnav">
  <li><a class="active" href="#home">Publish</a></li>
  <li><a href="#news">Post Activity</a></li>
  <li><a href="#contact">Content Library</a></li>
  <li><a href="#about">Post Calendar</a></li>
  <li><a href="#about">Profile Manager</a></li>
  <li class="icon">
    <a href="javascript:void(0);" style="font-size:15px;" onclick="toggleMenu()">☰</a>
  </li>
</ul>
like image 138
Rahul Patel Avatar answered Dec 08 '25 17:12

Rahul Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!