[英]Inline JavaScript transition from negative position overridden by CSS style
I have a fiddle that has two small boxes inside a larger box . 我的小提琴在一个大盒子里有两个小盒子 。
When either of the buttons "Run B" or "Run C" is clicked, the corresponding box is run through a CSS transition, effected via JavaScript. 单击“运行B”或“运行C”按钮中的任意一个时,将通过CSS转换(通过JavaScript进行转换)运行相应的框。 The code is reproduced here with comments:
该代码在此处带有注释:
// Get the specified box
var element = document.getElementById(id);
// Save initial style for resetting
var initial = element.style.cssText;
// Initialise transition
element.style.position = 'relative';
element.style.transition = 'top 800ms ease';
// Set start position
element.style.top = '-50px';
// Effect the transition in the next cycle
// by setting the end position
setTimeout(function(){
element.style.top = '15px';
}, 25);
// Reset the style on completion
setTimeout(function(){
element.style.cssText = initial;
}, 800);
You should be able to observe that each transition is quite different. 您应该能够观察到每个过渡都完全不同。 However, the only difference between the two boxes is the CSS property
top: 0
set in the stylesheet. 但是,这两个框之间的唯一区别是CSS属性
top: 0
在样式表中设置为top: 0
。
Can someone please explain why the transitions are different? 有人可以解释一下为什么过渡不同吗?
Since the style for the element is set inline, I would have expected that to override the CSS style, but that is not happening. 由于该元素的样式是内联设置的,所以我希望可以覆盖CSS样式,但是这没有发生。 Why does the
top: 0
have any impact at all? 为什么
top: 0
根本没有影响?
I'll do my best to explain: You are setting the top to -50px
and then you have a setTimeout
which sets it to 15px
; 我将尽力解释:您将顶部设置为
-50px
,然后有一个setTimeout
其设置为15px
;
Since #b
have a starting position of 0px
, it will start animating to -50px
but after 25 mllis it will get a new position of 15px
and it will animate there instead (you can see that if you give the first setTimeout a longer time like 100). 由于
#b
的起始位置为0px
,它将开始设置动画为-50px
但是在25 mllis之后,它将获得15px
的新位置,并且将在此处进行动画处理(您可以看到,如果给第一个setTimeout更长的时间,例如100)。 Since 25 millis is very short, it looks like it immediately starts to go down. 由于25毫秒非常短,因此看起来立即开始下降。
Since #c
does not have a starting position, when you set it to -50
you actually give it a starting position and then it animates from -50 to 15px; 由于
#c
没有起始位置,因此当您将其设置为-50
,实际上是给它一个起始位置,然后将其动画设置为-50至15px;
I hope I've managed to clarify it :) 我希望我已经设法弄清楚了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.