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?
This question is related to
javascript
css
css-transitions
Are there properties that aren't 'transitional'?
Answer: Yes.
If the property is not listed here it is not 'transitional'.
Reference: Animatable CSS Properties
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;
I ran into this issue today. Here is my hacky solution.
I needed a fixed position element to transition up by 100 pixels as it loaded.
var delay = (ms) => new Promise(res => setTimeout(res, ms));
async function animateView(startPosition,elm){
for(var i=0; i<101; i++){
elm.style.top = `${(startPosition-i)}px`;
await delay(1);
}
}
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 {_x000D_
border: 1px solid black;_x000D_
height: 240px;_x000D_
width: 260px;_x000D_
margin: 50px;_x000D_
position: relative;_x000D_
}_x000D_
.jump {_x000D_
position: absolute;_x000D_
width: 200px;_x000D_
height: 50px;_x000D_
color: white;_x000D_
padding: 5px;_x000D_
}_x000D_
#jump1 {_x000D_
background-color: maroon;_x000D_
top: 0px;_x000D_
right: 0px;_x000D_
transition: right 1s ease 0s;_x000D_
}_x000D_
#jump2 {_x000D_
background-color: green;_x000D_
top: 60px;_x000D_
right: 0;_x000D_
transition: right 1s ease 0s;_x000D_
}_x000D_
#jump3 {_x000D_
background-color: blue;_x000D_
top: 120px;_x000D_
right: 0px;_x000D_
transition: right 1s ease 0;_x000D_
}_x000D_
#jump4 {_x000D_
background-color: gray;_x000D_
top: 180px;_x000D_
right: 0;_x000D_
transition: right 1s ease 0;_x000D_
}_x000D_
#box:hover .jump {_x000D_
right: 50px;_x000D_
}
_x000D_
<div id="box">_x000D_
<div class="jump" id="jump1">right: 0px<br>transition: right 1s ease 0s</div>_x000D_
<div class="jump" id="jump2">right: 0<br>transition: right 1s ease 0s</div>_x000D_
<div class="jump" id="jump3">right: 0px<br>transition: right 1s ease 0</div>_x000D_
<div class="jump" id="jump4">right: 0<br>transition: right 1s ease 0</div>_x000D_
</div>
_x000D_
Perhaps you need to specify a top value in your css rule set, so that it will know what value to animate from.
Source: Stackoverflow.com