简体   繁体   English

在jQuery slideDown动画结束时修复垂直跳转

[英]Fixing vertical jump at end of jQuery slideDown animation

I am new to Jquery but have written a simple vertical accordion. 我是Jquery的新手,但写过一部简单的垂直手风琴。 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 aa link to my test page (all my code [css, js etc.] is in one file for ease) : Vertical Accordion 这是我的测试页面的链接(我的所有代码[css,js等]都在一个文件中以方便): Vertical Accordion

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. 在你的自定义代码中,我通过对CSS进行一些小改动并指定手风琴中p标签的高度来摆脱“跳跃”。

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: 我继续下载了你的页面副本并对其进行了测试,它似乎在一些快速的浏览器测试中运行良好,所以这里是你修改过的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. TL; DR - 通过在手风琴的每个“开口”部分设置一个明确的高度,它会移除生涩的动画。 so we set those heights via script. 所以我们通过脚本设置这些高度。

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. 我没有时间调查这个修复工作原理的细节,但我想我还是要分享。

I was able to fix my problem just by using overflow: auto or overflow: hidden . 我只能通过使用overflow: autooverflow: 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. 只要不是一个很小的高度它应该能够显示一切,但添加overflow属性允许它更平滑地过渡,因为它不计算高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM