Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transition doesn't work with top, bottom, left, right

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?

like image 425
php_nub_qq Avatar asked Dec 04 '13 18:12

php_nub_qq


People also ask

Why is transition CSS not working?

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.)

How do I enable transition CSS?

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.

Does transition only work with hover?

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.


2 Answers

Try setting a default value for top in the CSS to let it know where you want it to start out before transitioning:

CSS

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.

like image 171
xec Avatar answered Sep 28 '22 07:09

xec


Perhaps you need to specify a top value in your css rule set, so that it will know what value to animate from.

like image 45
sarahholden Avatar answered Sep 28 '22 06:09

sarahholden