修复 jQuery SlideDown 动画末尾的垂直跳跃

发布于 2024-10-06 02:38:08 字数 229 浏览 7 评论 0原文

我是 Jquery 的新手,但写了一个简单的垂直手风琴。这似乎符合我所要求的工作,但在滑落结束时有明显的抖动。

如果有人能看一下它并提出一个解决方案,这将阻止我再拔头发!

这是我的测试页面的链接(为了方便起见,我的所有代码 [css、js 等] 都在一个文件中): 垂直手风琴

I am new to Jquery but have written a simple vertical accordion. It seems to to the job I require, but at the end of the slide down there is a visible jerk.

If anyone could look at it and suggest a solution, it will stop me pulling any more of my hair out!

Here is a a link to my test page (all my code [css, js etc.] is in one file for ease) : Vertical Accordion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜中书 2024-10-13 02:38:08

在您的自定义代码中,我通过对 CSS 进行小更改并指定手风琴中 p 标签的高度来摆脱“跳转”。

由于您通过脚本将它们全部隐藏,因此在执行之前:

$(".accordion p:not(:first)").hide(); 

也许您可以遍历并获取每件的计算高度并将其添加到每个项目样式中,从而消除最后遇到的“混蛋”。

沿着这些思路的事情:

$('.accordion p').each(function(index) {
   $(this).css('height', $(this).height());
});

编辑

我继续下载了您的页面的副本并对其进行了测试,它似乎在一些快速浏览器测试中工作正常,所以这是您修改后的 vaccordian.js:

$(document).ready(function(){   
    $('.accordion p').each(function(index) {
       $(this).css('height', $(this).height());
    });


    $(".accordion h3:first").addClass("active");
    $(".accordion p:not(:first)").hide();


    $(".accordion h3").click(function(){
        $(this).next("p").slideToggle("slow")
        .siblings("p:visible").slideUp("slow");
        $(this).toggleClass("active");
        $(this).siblings("h3").removeClass("active");
    });
});

TL;DR - 通过设置明确的高度在手风琴的每个“打开”部分,它消除了抖动动画。所以我们通过脚本设置这些高度。

In your custom code, I got rid of the 'jump' by making a small change in the CSS and specifying the height of the p tags within the accordion.

Since you hide them all via script, before you do:

$(".accordion p:not(:first)").hide(); 

maybe you could walk through and get the calculated heights of each piece and add that to each items style, thereby eliminating that "jerk" you get at the end.

Something along these lines:

$('.accordion p').each(function(index) {
   $(this).css('height', $(this).height());
});

Edit

I went ahead and downloaded a copy of your page and tested this, and it seems to work fine in a few quick browser tests, so here's your revised vaccordian.js:

$(document).ready(function(){   
    $('.accordion p').each(function(index) {
       $(this).css('height', $(this).height());
    });


    $(".accordion h3:first").addClass("active");
    $(".accordion p:not(:first)").hide();


    $(".accordion h3").click(function(){
        $(this).next("p").slideToggle("slow")
        .siblings("p:visible").slideUp("slow");
        $(this).toggleClass("active");
        $(this).siblings("h3").removeClass("active");
    });
});

TL;DR - by setting an explicit height on each 'opening' part of the accordion, it removes the jerky animation. so we set those heights via script.

述情 2024-10-13 02:38:08

作为参考,以防其他人遇到此问题,以下内容对我有用:

.ui-accordion .ui-accordion-content {
    overflow: auto;
    box-sizing: content-box;
    -moz-box-sizing: content-box;
}

我真的没有时间调查此修复程序为何有效的细节,但我想无论如何我都会分享。

For reference in case somebody else comes across this problem, the following worked for me:

.ui-accordion .ui-accordion-content {
    overflow: auto;
    box-sizing: content-box;
    -moz-box-sizing: content-box;
}

I don't really have time to investigate the details of why this fix works, but thought I'd share anyway.

清风疏影 2024-10-13 02:38:08

我只需使用 overflow: autooverflow: hide 就可以解决我的问题。我认为这是有效的,因为它忽略了元素的高度并会显示它可以显示的任何内容。只要高度不小,它就应该能够显示所有内容,但添加溢出属性可以使其过渡更平滑,因为它不计算高度。

I was able to fix my problem just by using overflow: auto or overflow: hidden. I think this works because it ignores the height of the element and will show whatever it can. As long as there isnt a small height it should be able to show everything but adding the overflow property allows it transition more smoothly because it is not calculating the height.

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