简体   繁体   English

在 Jquery 中动画 addClass 和 removeClass

[英]Animate addClass and removeClass in Jquery

I Have a simple code that removes a class when a div is clicked.我有一个简单的代码,可以在单击 div 时删除一个类。 What i am looking for is that rather than just showing when the div is clicked, can I make it slide in from left to right?我正在寻找的是,我可以让它从左到右滑入,而不仅仅是在单击 div 时显示吗?

$(document).scroll(function() {
  var y = $(this).scrollTop();
  if (y > 140) {
    $('#primary-menu-container').addClass("hide-nav-fixed");
  } else {
    $('#primary-menu-container').removeClass("hide-nav-fixed");
  }
});

If you use jQueryUI, simply add arguments to your add/removeClass functions like this:如果您使用 jQueryUI,只需向您的 add/removeClass 函数添加参数,如下所示:

.addClass('class-name', time-in-milliseconds, 'effect');

Results with a Swing effect for 1 second:具有 1 秒摇摆效果的结果:

$(document).scroll(function() {
    var y = $(this).scrollTop();
    if (y > 140) {
        $('#primary-menu-container').addClass('hide-nav-fixed', 1000, 'swing');
    } 
    else {
        $('#primary-menu-container').removeClass('hide-nav-fixed', 1000, 'swing');
    }
});

Here is the documentation for (addClass/removeClass):这是 (addClass/removeClass) 的文档:

Just make another class called 'slide-left'只需创建另一个名为“向左滑动”的类

.slide-left{
    margin-left:-500px;
    transition:margin-left 500ms;
}

And add it like you are doing with the other classes.并像添加其他类一样添加它。 Then, after 500ms you can hide it.然后,在 500 毫秒后,您可以隐藏它。

Edit: you can move the div any way you want following the same strategy.编辑:您可以按照相同的策略以任何方式移动 div。 And depending on the current css attributes of the div, other attributes would be more appropiate to make the animation并且根据当前 div 的 css 属性,其他属性会更适合制作动画

Would this be what you are looking for:这会是你要找的吗:

Animate.css动画.css

Collection of simple CSS animations you can attach to html elements.可以附加到 html 元素的简单 CSS 动画的集合。 See how the author linked the animations in the demo when you press a button and do the same before you remove the item (set long enough timer before you actually remove item).当您按下按钮并在删除项目之前执行相同操作时(在实际删除项目之前设置足够长的计时器),看看作者如何链接演示中的动画。 When adding you simply add an animation class as well.添加时,您只需添加一个动画类。

Plenty of animations seem suitable for entry removal from the list许多动画似乎适合从列表中删除条目

You can always make animations yourself but why waste time, these are battle tested and work.你总是可以自己制作动画,但为什么要浪费时间,这些都是经过实战测试和工作的。 If you don't wanna the whole pack make your own by removing everything you don't need?如果您不想通过删除您不需要的所有内容来制作自己的整个包?

如果没有 CSS3(与非常旧的浏览器版本不兼容),您可以在添加类后使用 jquery 动画功能:

$('#primary-menu-container').animate({"left":"0px"}, "slow");

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

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