How to increase the height in a lively way?

Asked

Viewed 648 times

3

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

The result I have achieved so far is "breaking" as I go down the scroll. How to get something that is more lively 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

2 answers

1


You can use the .animate, but as scroll does run multiple animates at the same time, it can still present some stops, the solution I found was to use the .clearQueue looks like it’s gotten 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 make a calculation as the size of st)

Jsfiddle

  • Jader, it worked perfectly. Thank you very much.

0

I noticed that in your example when climbing the scroll bar the size of the div does not follow as it happens when going down the scrollbar, so I rang, I do not know if this is what I wanted, but I figured, if you do not want to let me know in the comments.

I applied the property animate() that creates change css with a transition;

So any questions let me know I’ll do my best to help you.

Jsfiddle

  • Thanks for the answer, Samir. But actually the idea of the project, is to NOT go up, and get the size that was during the descent. What I need now, is that when I make the size increase, there is a coarse effect. I need it to be softer, maybe using one . However, I could not implement.

  • The example that you saw Jsfiddle, if it is not smooth enough you can increase or decrease the milliseconds.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.