I have an element with style
position: relative; transition: all 2s ease 0s;
Then I want to change its position smoothly after clicking on it, but when I add the style change the transition doesn't take place, instead, the element moves instantly.
$$('.omre')[0].on('click',function(){ $$(this).style({top:'200px'}); });
However, if I change the color
property, for example, it changes smoothly.
$$('.omre')[0].on('click',function(){ $$(this).style({color:'red'}); });
What might be the cause of this? Are there properties that aren't 'transitional'?
EDIT: I guess I should have mentioned that this is not jQuery, it's another library. The code appears to work as intended, styles are being added, but transition only works in the second case?
If you have a transition not working, check that the starting value of the property is explicitly set. Sometimes, you'll want to animate height and width when the starting or finishing value is auto . (For example, to make a div collapse, when its height is auto and must stay that way.)
To trigger an element's transition, toggle a class name on that element that triggers it. To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. Then set those CSS properties of that element equal to those values you just got.
But transitions are not just limited to use with :hover . You can animate CSS properties, thus use CSS transitions without hover. This is done via transitions using some other CSS techniques, a number of which I've outlined below. I've also included a demo for each example.
Try setting a default value for top
in the CSS to let it know where you want it to start out before transitioning:
position: relative; transition: top 2s ease 0s; /* only transition top property */ top: 0; /* start transitioning from position '0' instead of 'auto' */
The reason this is needed is because you can't transition from a keyword, and the default value for top
is auto
.
It is also good practice to specify exactly what you want to transition (only top
instead of all
) both for performance reasons and so you don't transition something else (like color
) unintentionally.
Perhaps you need to specify a top value in your css rule set, so that it will know what value to animate from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With