jQuery:如何将高度添加到元素顶部,从而将动画设置为更高的高度?

发布于 2024-08-25 05:44:06 字数 860 浏览 6 评论 0原文

我有一个简单的问题,但我不知道如何解决它。

基本上我有一些隐藏内容,一旦展开,需要 99px 的高度。折叠时,容纳它的元素 section#newsletter 的高度设置为 65px。

实例:http://dev.supply.net.nz/asap-finance- static/

单击 a#signup_form 时,section#newsletter 使用以下 jQuery 扩展到 99px:

$('#newsletter_form').hide(); // hide the initial form fields
$('#form_signup').click(function(event) {
    event.preventDefault(); // stop click
    // animate
    $('section#newsletter').animate({height: 99}, 400, function() {
        $('#newsletter_form').show(300);
    })
});

所有这些都很好用,除了以下事实:元素位于粘性页脚中,因此它的初始高度用于定位它。

动画这个元素的高度会导致浏览器上出现滚动条,因为添加的 34px 被添加到元素的底部,所以我的问题:

如何将这些 34px 添加到元素的顶部,以便高度向上扩展到主体而不是向下?

感谢您的阅读, 我期待您的帮助和建议。

詹尼斯

I have a simple problem but I am not sure how to solve it.

Basically I have some hidden content that, once expanded, requires a height of 99px. While collapsed the element holding it section#newsletter is set to be 65px in height.

LIVE EXAMPLE: http://dev.supply.net.nz/asap-finance-static/

On the click of a#signup_form the section#newsletter is expanded to 99px using the following jQuery:

$('#newsletter_form').hide(); // hide the initial form fields
$('#form_signup').click(function(event) {
    event.preventDefault(); // stop click
    // animate
    $('section#newsletter').animate({height: 99}, 400, function() {
        $('#newsletter_form').show(300);
    })
});

All this works great except for the fact that this element sits in a sticky footer so its initial height is used to position it.

Animating the height of this element causes scrollbars on the browser, because the 34px added are added to the bottom of the element, so my question:

How can I add these 34px to the top of the element so the height expands upwards into the body not downwards?

Thanks for reading,
I look forward to your help and suggestions.

Jannis

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

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

发布评论

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

评论(2

月亮是我掰弯的 2024-09-01 05:44:06

只是为了发布我的特定问题的完整解决方案,以防对其他人有所帮助,这里是代码:

JS:

$('your_trigger').click(function(event) { // click on the button to trigger animation
    event.preventDefault(); // stop click
    // animate
    function resizer () { 
        // run inside a function to execute all animations at the same time
        $('your_container').animate({marginTop: 0, height:99}, 400, function() {
            $('#newsletter_form').show(300); // this is just my content fading in
        });
        // this is the footer container (inside is your_container) 
        // & div.push (sticky footer component)
        $('footer,.push').animate({marginTop:0}, 400);
    };
    resizer(); // run
});

CSS: (这是我正在使用的粘性页脚

/* ================= */
/* = STICKY FOOTER = */
/* ================= */
html, body {
    height: 100%;
}
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -135px; /* -full expanded height of the footer */
position: relative;
}
footer, .push {
height: 101px; /* height of this is equal to the collapsed elements height */
clear: both;
}
section#newsletter,footer {
    margin-top: 34px; 
    /* this is the difference between the expanded and the collapsed height */
}

所以基本上我正在定位我的页脚,就像我通常使用 TOTAL 的完整高度一样(在动画高度之后),然后我通过 margin-top 下推初始内容,以补偿 your_container 折叠时高度的较小值。

然后在动画上调整 your_container 的高度以及 your_containerfooter 上的边距顶部。 .push 被删除。

希望这对其他人偶然发现这一点时有所帮助。

J。

just to post the complete solution to my specific problem in case this may help others, here is the code:

JS:

$('your_trigger').click(function(event) { // click on the button to trigger animation
    event.preventDefault(); // stop click
    // animate
    function resizer () { 
        // run inside a function to execute all animations at the same time
        $('your_container').animate({marginTop: 0, height:99}, 400, function() {
            $('#newsletter_form').show(300); // this is just my content fading in
        });
        // this is the footer container (inside is your_container) 
        // & div.push (sticky footer component)
        $('footer,.push').animate({marginTop:0}, 400);
    };
    resizer(); // run
});

CSS: (this is the sticky footer i am using)

/* ================= */
/* = STICKY FOOTER = */
/* ================= */
html, body {
    height: 100%;
}
#wrap {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -135px; /* -full expanded height of the footer */
position: relative;
}
footer, .push {
height: 101px; /* height of this is equal to the collapsed elements height */
clear: both;
}
section#newsletter,footer {
    margin-top: 34px; 
    /* this is the difference between the expanded and the collapsed height */
}

So basically I am positioning my footer as I would normally with the full height of the TOTAL (after animation height) and then I push down the initial content via margin-top to compensate for the lesser value in height when the your_container is collapsed.

Then on animate both the height of your_container is adjusted and the margin-top on your_container and footer & .push are removed.

Hope this helps someone else when they stumble upon this.

J.

笙痞 2024-09-01 05:44:06

你可以这样做:

$('.box').animate({
    height: '+=10px'
});

You can do something like this:

$('.box').animate({
    height: '+=10px'
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文