我的 div 通过 show() 正确淡入,但随后在动画完成时跳起

发布于 2024-12-03 17:17:26 字数 2025 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-12-10 17:17:26

根据原始帖子之后的讨论,p 元素上的默认边距和填充被确定为导致动画跳转。

只需从位于动画 results 元素内的 p 元素中删除边距和填充即可解决该问题。这可能是由于 jQuery 中的动画逻辑中的错误(计算动画元素的最终所需尺寸)造成的。

编辑:此外,请注意,许多基于 JS 的动画问题可以通过通过 widthheight 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 animated results 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 and height 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文