0

I am currently learning Vue.js for a project I am busy with. I have been learning through online resources, as well as a Udemy course by Maximilian Schwarzmüller, Which i'd highly recommend.

I am having an issue building a hamburger menu for practice purposes. I am adding/removing a class on a div which holds my menu content. The adding/removing of the class happens via a button click (my "hamburger").

As far as I can tell through my course and other online sources, including a good few StackOverflow questions, what I have done should work.

Please let me know if you can see any issues, as it isn't working in my codepen. Thanks in advance =)

relevant HTML:

<button v-on:click="isActive = !isActive" class="navigation-hamburger">
  <p>-<br>-<br>-</p>
</button>

<div class="menu-contain" v-bind:class="{ active: isActive}">
 <ul>
  <li><a href="#" class="links">Home</a></li>
  <li><a href="#" class="links">About</a></li>
  <li><a href="#" class="links">Contact</a></li>
 </ul>
</div>

CSS:

.menu-contain {
  width: 100%;
  height: 0px;
  background-color: #09333C;
  transition: all 0.5s linear;
}

.active {
  width: 100%;
  height: 300px;
  background-color: #8BAFB5;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.5s linear;
}

Vue js:

new Vue({
  el: '#wrap',
  data: {
    isActive: false
  }
});

Here is the codepen:

https://codepen.io/-Infamous/pen/KvXNJB

2
  • 1
    You have an erroneous closing </div> tag that is closing the #wrap div too early meaning your .menu-contain div is outside the scope of your Vue instance. Here's a working codepen: codepen.io/anon/pen/PKJKRM Commented Aug 14, 2017 at 13:37
  • Yup, That's it, Thanks very much =) Commented Aug 14, 2017 at 13:45

1 Answer 1

1

Check your markup, a couple of closing tags were out of order.

<div id="wrap">
  <header id="navigation-top">
    <nav class="main-navigation">
      <!-- this is the button that toggles the class "active" -->
      <button v-on:click="isActive = !isActive" class="navigation-hamburger">
        <p>-<br>-<br>-</p>
      </button>
    </nav>
  </header>

  <!-- this is the div that needs to get the class "active" when the button is clicked -->
  <div class="menu-contain" v-bind:class="{ active: isActive}">
    <ul>
      <li><a href="#" class="links">Home</a></li>
      <li><a href="#" class="links">About</a></li>
      <li><a href="#" class="links">Contact</a></li>
    </ul>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that worked, silly mistake on my behalf -_- Thank you very much! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.