Scroll HTML page before it 'reaches' the bottom

frankgreco

enter image description here

I have this chat thread

What it does currently is when it gets to the bottom of the page it scrolls up so that it is always at the bottom of the page.

The issue is that as you can see in the image, it hides behind the form before it reaches the bottom of the page. Hence, I need to redefine what the bottom of my page is:

I have tried:

function scroll() {
    window.scrollTo(0, document.body.scrollHeight + document.getElementById('form_id').offsetHeight);
};

But it seems as though it has no effect from the function listen before this one.

Here is the rest of my code:

<html>
    <head>
        <title>Chat Demo</title>

        <style>
            .top-bar {
                background: #009E60;
                position: relative;
                overflow: hidden; 
            }

            .top-bar::before {
                content: "";
                position: absolute;
                top: -100%;
                left: 0;
                right: 0;
                bottom: -100%;
                opacity: 0.25;
                background: radial-gradient(white, #009E60);
            }

            .top-bar > * {
                position: relative;
            }

            .top-bar::before {
              animation: pulse 1s ease alternate infinite;
            }

            @keyframes pulse {
              from { opacity: 0; }
              to { opacity: 0.5; }
            }


            * {
                margin: 0;
                padding: 0;
            }

            h1{
                padding: 5px;
            }

            body {
                font: 15px Helvetica, Arial;
            }

            form {
                background: #009E60;
                padding: 10px;
                bottom: 0;
                position: fixed;
                width: 60%;
                box-sizing: border-box;
            }

            form input {
                padding: 5px;
                width: 80%;
                margin-right: 0.5%;
                vertical-align: bottom;
                font: 15px Helvetica, Arial;

            }

            form button {   
                width: 18.5%;
                padding: 9px;
                vertical-align: bottom;

            }

            ol {
                padding-bottom: 100px;
            }

            #messages {
                list-style-type: none;
                margin: 0;
                padding: 0;
            }

            #messages li {
                padding: 5px 10px;
            }

            .left {
                padding: 5px 10px;
                background: -webkit-linear-gradient(left,rgba(215,229,228,1),rgba(255,255,255,0)); /*Safari 5.1-6*/
                background: -o-linear-gradient(right,rgba(255,255,255,0),rgba(215,229,228,1)); /*Opera 11.1-12*/
                background: -moz-linear-gradient(right,rgba(255,255,255,0),rgba(215,229,228,1)); /*Fx 3.6-15*/
                background: linear-gradient(to right, rgba(215,229,228,1), rgba(255,255,255,0)); /*Standard*/
                text-align: left;
            }

            .right {
                padding: 5px 10px;
                background: -webkit-linear-gradient(left,rgba(255,255,255,0),rgba(228,215,229,1)); /*Safari 5.1-6*/
                background: -o-linear-gradient(right,rgba(228,215,229,1),rgba(255,255,255,0)); /*Opera 11.1-12*/
                background: -moz-linear-gradient(right,rgba(228,215,229,1),rgba(255,255,255,0)); /*Fx 3.6-15*/
                background: linear-gradient(to right,rgba(255,255,255,0),rgba(228,215,229,1)); /*Standard*/
                text-align: right;
            }

            .module {
              width: 60%;
              margin: 20px auto;
            }
        </style>

    </head>

    <body>

        <section class="module">

            <div id="wrapper">
                <div id="header">
                    <header class="top-bar">
                        <h1>Chat Demo</h1>
                    </header>
                </div>
                <div id="content">
                    <ol id="messages"></ol>
                </div>
                <div id="footer">
                    <form id="form_id" action = "#">
                        <input id = "user_input"/>
                        <button id="btn_id">send</button>
                    </form>
                </div>
            </div>
        </section>

        <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
        <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
        <script>
            var socket = io();

            $("#form_id").submit(function(){
                var value = $("#user_input").val();
                if(value.length > 0 && value != "Default text"){
                    socket.emit('message from me', value);
                    $('#messages').append($('<li class="right">').text(value));
                    $('#user_input').val('');
                    scroll();
                }
                return false;
            });

            socket.on('message', function(msg){
                $('#messages').append($('<li class="left">').text(msg));
            });

            function scroll() {
                window.scrollTo(0, document.body.scrollHeight);
            };

        </script>

    </body>
</html>
Pradeep Andrewson

Hi just add this css in your html page you will get output div#footer { margin-top: 10px; }

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Scroll Automatically to the Bottom of the Page

From Dev

Infragistics UltraGrid - how to add rows when scroll reaches bottom?

From Dev

Go to URL if draggable div reaches top or bottom of page

From Dev

Infinitely scroll to the bottom of the page

From Dev

JS for smooth scroll to the bottom of the page

From Dev

Laravel - redirect to page and scroll to the bottom of the page

From Dev

Change opacity of DIV when page scroll reaches an anchor

From Dev

Automatically scroll to bottom as the page loads

From Dev

Angularjs: How to trigger event when scroll reaches to the bottom of the scroll bar in div?

From Dev

Hide div when user reaches the bottom of page

From Dev

HTML: How to position button at bottom of page before and after page expand

From Dev

scroll to bottom on page load but will allow to scroll on top

From Dev

Firing event when scroll bar reaches the bottom of panel

From Dev

Scroll to near the bottom of page

From Dev

$mdtoast position bottom of page on scroll

From Dev

scroll the a growing page to the bottom

From Dev

Infragistics UltraGrid - how to add rows when scroll reaches bottom?

From Dev

JS for smooth scroll to the bottom of the page

From Dev

Laravel - redirect to page and scroll to the bottom of the page

From Dev

Hide div when user reaches the bottom of page

From Dev

scroll to bottom on page load but will allow to scroll on top

From Dev

Scroll bar in page bottom

From Dev

Stop div from following scroll before it reaches the footer

From Dev

Scroll to near the bottom of page

From Dev

$mdtoast position bottom of page on scroll

From Dev

CSS- stick footer to bottom of page (so only visible after scroll) WITHOUT html selector?

From Dev

Scroll to the Bottom of the Page after postBack

From Dev

how to scroll the page to bottom whenever the page load?

From Dev

How to exit loop when scrollBy reaches the bottom of the web page?

Related Related

  1. 1

    Scroll Automatically to the Bottom of the Page

  2. 2

    Infragistics UltraGrid - how to add rows when scroll reaches bottom?

  3. 3

    Go to URL if draggable div reaches top or bottom of page

  4. 4

    Infinitely scroll to the bottom of the page

  5. 5

    JS for smooth scroll to the bottom of the page

  6. 6

    Laravel - redirect to page and scroll to the bottom of the page

  7. 7

    Change opacity of DIV when page scroll reaches an anchor

  8. 8

    Automatically scroll to bottom as the page loads

  9. 9

    Angularjs: How to trigger event when scroll reaches to the bottom of the scroll bar in div?

  10. 10

    Hide div when user reaches the bottom of page

  11. 11

    HTML: How to position button at bottom of page before and after page expand

  12. 12

    scroll to bottom on page load but will allow to scroll on top

  13. 13

    Firing event when scroll bar reaches the bottom of panel

  14. 14

    Scroll to near the bottom of page

  15. 15

    $mdtoast position bottom of page on scroll

  16. 16

    scroll the a growing page to the bottom

  17. 17

    Infragistics UltraGrid - how to add rows when scroll reaches bottom?

  18. 18

    JS for smooth scroll to the bottom of the page

  19. 19

    Laravel - redirect to page and scroll to the bottom of the page

  20. 20

    Hide div when user reaches the bottom of page

  21. 21

    scroll to bottom on page load but will allow to scroll on top

  22. 22

    Scroll bar in page bottom

  23. 23

    Stop div from following scroll before it reaches the footer

  24. 24

    Scroll to near the bottom of page

  25. 25

    $mdtoast position bottom of page on scroll

  26. 26

    CSS- stick footer to bottom of page (so only visible after scroll) WITHOUT html selector?

  27. 27

    Scroll to the Bottom of the Page after postBack

  28. 28

    how to scroll the page to bottom whenever the page load?

  29. 29

    How to exit loop when scrollBy reaches the bottom of the web page?

HotTag

Archive