简体   繁体   English

javascript单击更改类,保持CSS动画

[英]javascript change class on click, keep css animations

So I'm in need of a bit of help. 因此,我需要一些帮助。 I have an issue that requires javascript/css combination. 我有一个需要javascript / css组合的问题。

jsfiddle 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. 我希望箭头/ css在单击时进行动画处理。 So I'm not sure on how to do this, if I should use .hasClass in js and switch it on click. 所以我不确定如何执行此操作,如果我应该在js中使用.hasClass并在单击时将其切换。 Also, how would I be able to define which menu item is opened (defined by class, instead of defined in js). 另外,我将如何定义打开哪个菜单项(由类定义,而不是在js中定义)。

After clicking, I want the arrow to remain how it should after animating (ie 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. 我需要大量更改js还是进行最少的修改。

$(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. 任何帮助都会很棒。

Here is the solution to the second part of your question: http://jsfiddle.net/R7f2K/3/ 这是问题第二部分的解决方案: http : //jsfiddle.net/R7f2K/3/

Your HTML has been updated to change: 您的HTML已更新,可以更改:

<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: 我已经将这一行添加到您的JS代码中:

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

The CSS rules have been modified to use #VerColMenu li a [class^="vnavright"] . CSS规则已修改为使用#VerColMenu li a [class^="vnavright"]

While things are working as you had desired, I find the animation to be confusing. 当事情如您所愿时,我发现动画令人困惑。

In the original HTML, you'll also probably want to change that first arrow's class to fa-angle-up : 在原始HTML中,您可能还希望将第一个箭头的类更改为fa-angle-up:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM