0

So, I want to create a navbar and rather than re-invent the wheel, I am using some public code to speed up my MVP dev.

Essentially, I am using this nav-bar code - https://codepen.io/PaulVanO/pen/GgGeyE.

But I am not sure of how I can implement jquery part within my Vue code (I have made a component, copied over html and css, now just need to integrate the jquery functionality within it.)

Here is the Jquery code I need to integrate.

$('#toggle').click(function() {
  $(this).toggleClass('active');
  $('#overlay').toggleClass('open');
});

It would be really thankful if anyone could help me accomplish with this.

3
  • Do you want to put this script inside <script> tag of the component? Commented Feb 13, 2019 at 16:39
  • Can you provide us with the code of the component you created? Than I can help you to toggle the navbar. Commented Feb 13, 2019 at 16:40
  • 1
    Possible duplicate of How to use a jQuery plugin inside Vue Commented Feb 13, 2019 at 16:43

1 Answer 1

3

Assuming you have your markup (html and css) as part of one component, getting the toggle to add/remove a class would be really simple, you just need to have a method toggle the active state and a data property to keep the data. An example would be better, so here it goes.
In your component object:

{
    data() {
        return {
            isActive: false
        }
    },

    methods: {
        toggleMenu(){
            this.isActive = !this.isActive
        }
    }
}

In your markup you need this

<div class="button_container" id="toggle" :class="{'active': isActive}" @click="toggleMenu">
    <span class="top"></span>
    <span class="middle"></span>
    <span class="bottom"></span>
</div>
------------------------------------
<div class="overlay" id="overlay" :class="{'open': isActive}">
<nav class="overlay-menu">
  <ul>
    <li ><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Work</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

That should get you going, just note i used the shorthand form for v-on and for v-bind

EDIT: Here's also a link to an updated pen with the whole example

Sign up to request clarification or add additional context in comments.

1 Comment

Man, you are the best!! Worked like a charm

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.