Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically scroll when div expands at bottom of page?

Lately I have come into a dead end. I'm trying to expand the footer (#footernotes) and that works, it's basically a div behind the body (done with z-index) and when you click on a button it moves down. The scroll bar goes with it too, meaning that the page is now longer.

But what I'm trying to do is to make the viewport move with the expanded div. What happens now is that when I press the button (.secret) the div (#footernotes) comes in but it is still out of the viewport UNLESS you manually scroll to view the longer page.

So to some it up, how do you make the viewport automatically scroll down after you expanded the page? In other words, how do you make the viewport stay at the bottom of the page.

Here is my code:

<script>
$(document).ready(function(){
    $('.secret').click(function(){
            $("#footernotes").animate({top: "100px"}, 1000);
        return false;
    });
});
</script>

<div id="footer">
    <a href="" class="secret" ><div id="buttontest" style="width:50px; height:50px; background-color:green;"></div></a>
    <div id="footernotes">
    </div>
</div> <!-- end #footer -->

And the CSS for #footernotes

#footernotes {
    background-color: red;
    width: 100%;
    position: relative;
    top: -80px;
    height: 150px;
    z-index: -400;
}

EDIT: While typing up the question I figure out the answer, you have to use the scrollTop. I have added the line code in the example below:

<script>
    $(document).ready(function(){
        $('.secret').click(function(){
                $("#footernotes").animate({top: "100px"}, 1000);
                $('body,html').animate({scrollTop: "210px"},1000);
            return false;
        });
    });
    </script>

You can still answer this if you think there is a better way, I just thought I'll leave this question posted in case other people have the same question.

like image 994
Banzboy Avatar asked Dec 21 '22 08:12

Banzboy


1 Answers

document.getElementById('divID').scrollIntoView();

try and see if that would do the job.

like image 85
mavili Avatar answered Apr 06 '23 00:04

mavili