jquery – How to increase height in an animated way?

Question:

I'm on a project and I need an object on the side of the page that increases in height as I scroll .

The result I got so far is "breaking" a lot as I go down the scroll . How do you get something that's livelier or has a smoother transition?

HTML:

<header>
</header>
<div id="content">
    <div id="broca">
    </div>
</div>
<footer>    
</footer>

CSS:

header, footer{
    display:block;
    height:200px;
    background:red;
    width:100%;
}
#content{
    display:block;
    width:100%;
    height:1500px;
    background:blue;
}
#broca{
    width:50px;
    height:0;
    background:#000;
}

jQuery:

$(function(){
    $(window).scroll(function() {
        var $broca = $('#broca');
        var st = $(this).scrollTop();
        if (st > $broca.height()){
            $broca.height( st );
        }
        if( st == 0 ) {

        } else {
            $broca.show();
        }
    }).scroll();
})

JSFiddle

Answer:

You can use .animate , but as the scroll makes running several animates at the same time, it can still present some stops, the solution I found was to use .clearQueue seems that it became more linear:

$(function(){
    $(window).scroll(function() {
        var $broca = $('#broca');
        var st = $(this).scrollTop();
        if (st > $broca.height()){
            $broca.clearQueue().animate({
                height: st } , 1000);
        }
        if( st == 0 ) {

        } else {
            $broca.show();
        }
    }).scroll();
})

(just adjust the speed as desired, maybe do a calculation for st size)

JSFiddle

Scroll to Top