Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

jquery - Why my mobile burger won't work? How to tackle the issue?

When I click on the burger it won't transform into cross. Although I can see in DOM the class .active is being added. Also menu doesn't respond on click at all. .img and .nav are grid items. Maybe I confused something in grid container. Don't hesitate to ask me about more details of the project. I hope I added enough details to figure out the situation. =) Thank you!

$(document).ready(function() {
  $('.header-burger').click(function(event) {
    $('.header-burger, .nav').toggleClass('.active');
  });
});
@media only screen and (max-width: 370px) {
  .nav {
    grid-row: 2;
    grid-column: 1/ span 2;
    position: relative;
    top: -112%;
    width: 100%;
    height: 100%;
    background-color: black;
    padding: 0 0 0 10px;
    z-index: 3;
  }
  .nav .active {
    top: 100%;
  }
  .header-burger {
    display: block;
    position: relative;
    width: 30px;
    height: 20px;
  }
  .header-burger:before,
  .header-burger:after {
    content: '';
    background-color: white;
    position: absolute;
    width: 100%;
    height: 2px;
    left: 0;
  }
  .header-burger:before {
    top: 0;
  }
  .header-burger .active:before {
    transform: rotate(45deg);
    top: 9px;
  }
  .header-burger .active:after {
    transform: rotate(-45deg);
    bottom: 9px;
  }
  .header-burger:after {
    bottom: 0;
  }
  .header-burger span {
    position: absolute;
    background-color: white;
    left: 0;
    width: 100%;
    height: 2px;
    top: 9px;
    transition: all 0.3 ease 0.3;
  }
  .header-burger.active span {
    transform: scale(0);
  }
}
<div class="img">
  <img src="./logo.svg">
  <div class="header-burger"><span></span></div>
</div>

<div class="nav">

  <ul class="burger-ul">
    <li class="burger-li"><a>About</a></li>
    <li class="burger-li"><a>Careers</a></li>
    <li class="burger-li"><a>Events</a></li>
    <li class="burger-li"><a>Products</a></li>
    <li class="burger-li"><a>Support</a></li>
  </ul>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
question from:https://stackoverflow.com/questions/65923290/why-my-mobile-burger-wont-work-how-to-tackle-the-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are adding the class .active including the dot. Remove the dot from your script

$(document).ready(function() {
  $('.header-burger').click(function(event) {
    $('.header-burger, .nav').toggleClass('active');
  });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...