0

So I'm in need of a bit of help. I have an issue that requires javascript/css combination.

jsfiddle

I have the menu setup. As you can see, the arrows animate on hover, which is not what I want in this case.

I want the arrows/css to animate on click. So I'm not sure on how to do this, if I should use .hasClass in js and switch it on click. Also, how would I be able to define which menu item is opened (defined by class, instead of defined in js).

After clicking, I want the arrow to remain how it should after animating (i.e. open menu has arrow up, closed menu has arrow down).

Would I have to change the js alot or would it just be minimal edits.

$(document).ready(function() {
    // Collapse everything but the first menu:
    $("#VerColMenu > li > a").not(":first").find("+ ul").slideUp(1);
    // Expand or collapse:
    $("#VerColMenu > li > a").click(function() {
        $(this).find("+ ul").slideToggle("fast");
    });
});

Any help would be great.

3
  • Please start using the "Tidy Up" button on jsfiddle. It makes your code beautiful. Commented Nov 16, 2013 at 21:42
  • Fixed, sorry. Will remember for future fiddles Commented Nov 16, 2013 at 21:48
  • That's alright. BTW, I have provided you the code to keep the arrow the way it should remain after animating. Do you also want to retain the current on-hover animation? Commented Nov 16, 2013 at 21:54

2 Answers 2

1

Here is the solution to the second part of your question: http://jsfiddle.net/R7f2K/3/

Your HTML has been updated to change:

<i class="fa fa-angle-down vnavright"></i>

to

<i class="vnavright fa fa-angle-down"></i>

I have added this line to your JS code:

$(this).find("[class^='vnavright']").toggleClass('fa-angle-down fa-angle-up');

The CSS rules have been modified to use #VerColMenu li a [class^="vnavright"].

While things are working as you had desired, I find the animation to be confusing.

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

7 Comments

Thanks. Would it still be possible to include the css animation in that? Or would that be something more dificult to implement?
Totally possible. We just have to use a different selector.
So something like creating a seperate class for angle up and angle down both with the css animation
See the updated answer. You should accept this by clicking on the checkmark next to the answer.
I've accepted this, thanks for your help. It all seems how I'd like it to be aside from the animations. Is it possible to trigger the animation onClick (i.e. only when you click the menu item)?
|
0

In the original HTML, you'll also probably want to change that first arrow's class to fa-angle-up :

    <ul id="VerColMenu">
        <li><a class="top-vnav">General <i class="fa fa-angle-up vnavright"></i></a>
...

1 Comment

Thanks, I figured as 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.