jQuery: How can I animate to a taller height with the height being added to the top of the element?

橙三吉。 提交于 2019-12-25 04:56:10

问题


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


回答1:


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.




回答2:


You can do something like this:

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


来源:https://stackoverflow.com/questions/2460039/jquery-how-can-i-animate-to-a-taller-height-with-the-height-being-added-to-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!