我的 div 通过 show() 正确淡入,但随后在动画完成时跳起
我正在使用 jQuery 为
$(document).ready(function() {
...
function calculate() {
...
if ($("#results").css('display') === 'none') {
console.log("fadein");
$("#results").show("normal", function() {
console.log("animation complete");
});
}
...
}
}
这是
<div id="results">
<p>Input equation: <span id="eq" class="result"></span></p>
<p>Roots: <span id="root1" class="result"></span>, <span id="root2" class="result"></span></p>
</div>
淡入效果完美。然而,动画一完成,元素就会突然在页面上跳跃约 20 像素。 (我还可以说动画将元素放置在其预期位置下方约 20 像素处,然后在完成后将元素向上跳跃 20 像素。)如何解决此问题?
(我应该提到这个跳跃错误在 Chrome 和 Firefox 中都会发生)
编辑:
我实际上没有太多样式表,但这里是:
<style type="text/css">
.result {
color: blue;
}
#results {
display: none;
}
input[type="text"] {
width:50px;
}
</style>
如果有任何用处,这里是我的文档的整个正文,因为它足够短了。
<body>
<p><input type="text" maxlength="10" id="input_a" class="variable"/>x^2 +
<input type="text" maxlength="10" id="input_b" class="variable"/>x +
<input type="text" maxlength="10" id="input_c" class="variable"/> = 0</p>
<p><button type="submit" disabled="disabled">Calculate</button>
<button type="reset">Clear</button></p>
<div id="results">
<p>Input equation: <span id="eq" class="result"></span></p>
<p>Roots: <span id="root1" class="result"></span>, <span id="root2" class="result"></span></p>
</div>
</body>
编辑2:这是完整的代码,因为它不太长: http://pastebin.com/kfjpkSBr
I'm using jQuery to animate a fade-in transition for a <div> element that I have. This is the code:
$(document).ready(function() {
...
function calculate() {
...
if ($("#results").css('display') === 'none') {
console.log("fadein");
$("#results").show("normal", function() {
console.log("animation complete");
});
}
...
}
}
Here is the <div> element I'm fading in:
<div id="results">
<p>Input equation: <span id="eq" class="result"></span></p>
<p>Roots: <span id="root1" class="result"></span>, <span id="root2" class="result"></span></p>
</div>
The fading in effect works perfectly. However, as soon as the animation completes, the element suddenly jumps about 20 pixels up the page. (I could also say that the animation places the element about 20 pixels below its intended location, and then jumps the element up 20 pixels after completion.) How can I fix this?
(I should mention that this jumping bug happens in both Chrome and Firefox)
Edit:
I don't really have much of a style sheet, but here it is:
<style type="text/css">
.result {
color: blue;
}
#results {
display: none;
}
input[type="text"] {
width:50px;
}
</style>
If it's any use at all, here's the entire body of my document, since it's short enough.
<body>
<p><input type="text" maxlength="10" id="input_a" class="variable"/>x^2 +
<input type="text" maxlength="10" id="input_b" class="variable"/>x +
<input type="text" maxlength="10" id="input_c" class="variable"/> = 0</p>
<p><button type="submit" disabled="disabled">Calculate</button>
<button type="reset">Clear</button></p>
<div id="results">
<p>Input equation: <span id="eq" class="result"></span></p>
<p>Roots: <span id="root1" class="result"></span>, <span id="root2" class="result"></span></p>
</div>
</body>
Edit 2: Here's the entire code, since it's not too long: http://pastebin.com/kfjpkSBr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据原始帖子之后的讨论,
p
元素上的默认边距和填充被确定为导致动画跳转。只需从位于动画
results
元素内的p
元素中删除边距和填充即可解决该问题。这可能是由于 jQuery 中的动画逻辑中的错误(计算动画元素的最终所需尺寸)造成的。编辑:此外,请注意,许多基于 JS 的动画问题可以通过通过
width
和height
CSS 属性为动画元素定义尺寸来解决。根据所使用的特定 (jQuery) 动画及其所应用的元素,边距和/或填充值可能需要转移到间隔元素以保持一致的过渡。Per the discussion following the original post, the default margin and padding on the
p
elements was determined to cause the jump in animation.Simply removing the margin and padding from the
p
elements located within the animatedresults
element solved the problem. This is likely due to a bug in the animation logic within jQuery that calculates the final desired dimensions of elements that are being animated.EDIT: Also, just to make note of it, many JS-based animation issues can be solved by giving the animated elements defined dimensions via
width
andheight
CSS properties. Depending on the specific (jQuery) animation being used, and the element it is being applied to, margin and/or padding values may need to be shifted to spacer elements to maintain consistent transitions.