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?
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?
Share Improve this question edited Jan 5, 2021 at 11:40 leonheess 21.2k19 gold badges92 silver badges136 bronze badges asked Dec 4, 2013 at 18:46 php_nub_qqphp_nub_qq 16k22 gold badges80 silver badges148 bronze badges 8 | Show 3 more comments6 Answers
Reset to default 272Try 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.
Perhaps you need to specify a top value in your css rule set, so that it will know what value to animate from.
Something that is not relevant for the OP, but maybe for someone else in the future:
For pixels (px
), if the value is "0", the unit can be omitted: right: 0
and right: 0px
both work.
However I noticed that in Firefox and Chrome this is not the case for the seconds unit (s
). While transition: right 1s ease 0s
works, transition: right 1s ease 0
(missing unit s
for last value transition-delay
) does not (it does work in Edge however).
In the following example, you'll see that right
works for both 0px
and 0
, but transition
only works for 0s
and it doesn't work with 0
.
#box {
border: 1px solid black;
height: 240px;
width: 260px;
margin: 50px;
position: relative;
}
.jump {
position: absolute;
width: 200px;
height: 50px;
color: white;
padding: 5px;
}
#jump1 {
background-color: maroon;
top: 0px;
right: 0px;
transition: right 1s ease 0s;
}
#jump2 {
background-color: green;
top: 60px;
right: 0;
transition: right 1s ease 0s;
}
#jump3 {
background-color: blue;
top: 120px;
right: 0px;
transition: right 1s ease 0;
}
#jump4 {
background-color: gray;
top: 180px;
right: 0;
transition: right 1s ease 0;
}
#box:hover .jump {
right: 50px;
}
<div id="box">
<div class="jump" id="jump1">right: 0px<br>transition: right 1s ease 0s</div>
<div class="jump" id="jump2">right: 0<br>transition: right 1s ease 0s</div>
<div class="jump" id="jump3">right: 0px<br>transition: right 1s ease 0</div>
<div class="jump" id="jump4">right: 0<br>transition: right 1s ease 0</div>
</div>
In my case div position was fixed , adding left position was not enough it started working only after adding display block
left:0;
display:block;
Are there properties that aren't 'transitional'?
Answer: Yes.
If the property is not listed here it is not 'transitional'.
Reference: Animatable CSS Properties
Under the hood while animation
Modern web browsers heavily optimize animations and transitions by using hardware acceleration. This means that animations and transitions are often delegated to the GPU (Graphics Processing Unit) for smoother rendering.
Concern
You should avoid using transitions with the left
/top
/right
/bottom
properties. it can result in flickering/slow or janky animations.
Those don’t create a fluid animation because they have the browser creating layouts each time, which will affect all of their children. it often triggers a "reflow" and a "repaint" of the entire page. Also, they aren't always hardware-accelerated, leading to slower and less smooth animations.
Solution
Use CSS transitions and animations with properties like transform
and opacity
when you animate as they have no impact on the flow and are much smoother. They are hardware-accelerated in most cases.
Pro-tip : will-change
The will-change
CSS property is used to provide a hint to the browser about which properties are likely to change and be animated in the future. This hint allows the browser to optimize the rendering of these properties, resulting in better animation performance.
In many cases, when you apply will-change
to an element, the browser will promote that element to its own GPU-accelerated layer. This means that the element and the specified property are rendered separately from the rest of the page, which can improve rendering performance.
Warning: will-change
is intended to be used as a last resort, in order to try to deal with existing performance problems. It should not be used to anticipate performance problems.
.on()
method. If not - what is $$? – xec Commented Dec 4, 2013 at 18:48top
appears to be in the list, which eliminates my doubt abouttop
not being available for transitions. – php_nub_qq Commented Dec 4, 2013 at 18:53top
value set (to 0 for instance) before you change it (to 200 or whatever) – xec Commented Dec 4, 2013 at 18:54