简体   繁体   English

使用jQuery切换CSS动画?

[英]Toggle CSS animation with jQuery?

I'm making a simple slide down mobile menu, and I want it to move the whole site down 275px so it can have room for the mobile menu. 我正在制作一个简单的向下移动菜单,我希望它可以将整个网站向下移动275px,以便为移动菜单留出空间。 Make your browser width small, until you can see the list icon at the top right. 减小浏览器的宽度,直到您可以在右上角看到列表图标。 Then, click it. 然后,单击它。 You'll notice the whole site (everything in the globe class) is moved down 275 pixels via -webkit-animation (Im a jquery noob). 您会注意到,整个网站(globe类中的所有内容)都通过-webkit-animation(是jquery noob)向下移动了275个像素。 You should also notice the ease of the animation; 您还应该注意到动画的简便性; in contrast to the abrupt movement when you press it again. 与再次按下时的突然移动相反。 If you keep toggling the icon you'll see the difference. 如果您继续切换图标,将会看到不同之处。 I want the globe class to toggle that css animation some how; 我希望Globe类以某种方式切换该CSS动画; so when you toggle the icon, it will gingerly transition from 275px to 0px. 因此,当您切换图标时,它将小心地从275px过渡到0px。

$("#mobile").click(function () { // when icon is clicked, do the following:
    $("#m-nav").slideToggle(300); // side down the navigation
    $("#globe").toggleClass("translation"); // slide down the whole site 275px 
});

Here is the fiddle:: http://jsfiddle.net/7V5W5/3/ 这是小提琴:: http://jsfiddle.net/7V5W5/3/

You don't have reverse animation added in code: http://jsfiddle.net/7V5W5/13/ 您没有在代码中添加反向动画: http : //jsfiddle.net/7V5W5/13/

$("#mobile").click(function () {
    $("#m-nav").slideToggle(300);
    if ($('#globe').hasClass('translation')) {
        $("#globe").removeClass("translation").addClass("translation-reverse");
    } else {
        $("#globe").removeClass("translation-reverse").addClass("translation");
    }
});

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

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