Button animation does not happen

Asked

Viewed 72 times

0

When I clicked on the button it was supposed to go left for 400ms but when I click it goes straight without the animation, it "jumps" straight to the final place without going through the rest.

CSS:

#icone-menu.icone-menu-animacao{
    left: -10px;
    position: relative;

}

JS with Jquery:

$("#btn-menu").on("click", function(){
        $('#icone-menu').toggleClass('icone-menu-animacao', 400);
    }); 

HTML:

<a href="#" id="btn-menu">
<img src="img/menu55.png" id="icone-menu">
</a>

New CSS:

#icone-menu{
    position: block;
     left:0;
     transition: 0.2s;   
}
#icone-menu.icone-menu-animacao{
    left: -10px;
    position: relative;
    transition: 0.2s;
}
  • if the answer is right, please mark it as right :)

2 answers

2

Your CSS has to have transition and a "default state", here is an example in jsFiddle.net;
Plus, the ,400 on the line $('#icone-menu').toggleClass('icone-menu-animacao', 400); is not necessary since the toggleClass only takes one argument

  • but tell me, how to make him come back with the animation too? Because he’s coming right back..

  • 1

    @Andreyhartung to return with animation too, you change the css of the "default state" to have relative position and Transition thus

  • 2 things, 1o why can’t I reference you at the beginning in the message? I put @MoshMage and add 2o I edited there with the CSS that ta now and I took the 400 but the animation back still n is happening...

  • @Andreyhartung as for the first, I don’t know x) :)

  • I managed to do everything I wanted, I rewrote 96% of the animations' functioning kkkkkkkkkkk

  • Boa @Andreyhartung ! : ) I was just a little sad that mine is not the answer accepted as 'right' when both answers are (essentially) equal :P

  • I marked his response as certain, because although the two are very similar, his is a little better treated. So much so that his on the back menu bugged and he in the comments helped me treat it and posted even some article links. But I really appreciate your help, I started cleaning up my mess here with your help!

Show 2 more comments

1


CSS transitions happen due to property transition, without it the changes of properties of the elements will happen "abruptly". To make any exchange (as long as Transition affects these) of elements you can use this property. It controls transition time, type of transition and many other things.

That way, to make an element have an animation, you can simply use jQuery to put (or take) a class that will change the properties of that element, from now on, to transition will animate this exchange. Example:

$('#block').click(function() {
    $(this).toggleClass('mover');
});
#block {
    width:50px;
    height:50px;
    background-color:blue;
    position:relative;
    left:0;
    transition:left .5s linear;
}

#block.mover {
    left:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="block"></div>

And a detail, you used a second parameter in the command toggleClass in your question. This is not wrong, but this second parameter only works if you are using jQuery UI.

  • Rafael tell me something, I have an id like this #id{display:none;opacity:0;transition: all 1s linear;} and call the toggle #id.toggle{display:block;opacity:1;}, but it gets no excitement at all. I think it’s because of the display. There’s something I can do?

  • 1

    This is because Transition does not affect the property display, the ideal would be to use only the property opacity: http://jsfiddle.net/th6w20us/ There are some articles that address this issue: http://www.impressivewebs.com/animate-display-block-none/

  • 1

    A quick and easy solution is to use the function fadeToggle, she already takes care of it all by making your element appear and disappear the way you want it. Example: http://jsfiddle.net/th6w20us/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.