Home > Mobile >  How to scroll down inside of a chatbox using css and javascript
How to scroll down inside of a chatbox using css and javascript

Time:04-09

So i want the website to scroll down exactly like messenger, but with two input boxes which are username and message always at the bottom, and just above i want the messages, and for the messages if it can't show all the messages i want it to automatically scroll down to the newest message (scroll all the way down).

Here is my CSS and HTML code :

*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Raleway', sans-serif;
    outline: none;
    border: none;
}

body
{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #1a1a1a;
}

.container
{
    display: flex;
    justify-content: center;
    align-items: center;
}

/* chat box section */
.chat-box
{
    width: 400px;
    height: 450px;
    background-color: #fff;
    border-radius: 10px;
    overflow-y: scroll;
    scroll-behavior: auto;
    position: relative;
}

/* about client */

.client
{
    display: flex;
    justify-content: start;
    align-items: center;
    height: 80px;
    background-color: #77b3d4;
    padding: 15px;
}

.client img
{
    width: 50px;
    height: 50px;
    border-radius: 50%;
}

.client-info
{
    color: #fff;
    padding: 15px;
}

.client-info p
{
    color: #008000;
    font-weight: bold;
    font-size: 0.8em;
}


/* chat section */

.chats
{
    width: 100%;
    padding: 0 15px;
    color: #fff;
    position: relative;
    font-size: 0.9em;
}

.client-chat
{
    width: 75%;
    word-wrap: break-word;
    background-color: #4f5d73c7;
    padding: 7px 10px;
    border-radius: 10px 10px 10px 0;
    margin: 10px 0;
}

.my-chat
{
    width: 75%;
    word-wrap: break-word;
    background-color: #77b3d4c7;
    padding: 7px 10px;
    border-radius: 10px 10px 0 10px;
    margin: 5px 0 5px auto;
}

/* input section */
.chat-input
{
    display: flex;
    align-items: center;
    width: 100%;
    height: 65px;
    background-color: #fff;
    padding: 15px 1px 15px 1px;
    overflow: hidden;
    position: absolute;
    bottom: 0px;
}

.chat-input input
{
    width: calc(100% - 40px);
    height: 100%;
    background-color: #4f5d7321;
    border-radius: 50px;
    color: #000;
    font-size: 1.2em;
    padding: 0 15px;
}

.send-btn
{
    width: 65px;
    height: 40px;
    background-color: transparent;
    overflow: hidden;
    position: relative;
    margin-left: 5px;
    cursor: pointer;
    transition: 0.4s ease-in-out;
}

.send-btn:active
{
    transform: scale(0.85);
}

.send-btn img
{
    width: 100%;
    height: 100%;
}

/* chat box button section */

.chat-btn
{
    width: 70px;
    height: 70px;
    border-radius: 50%;
    background-color: #fff;
    cursor: pointer;
    overflow: hidden;
    position: fixed;
    bottom: 20px;
}

.chat-btn img
{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    position: absolute;
    top: 0;
    left: 0;
    padding: 5px;
    transition: 0.4s ease-in-out;
}

.chat-btn:hover img
{
    transform: rotate(30deg);
}
<html lang="en">
   <head>
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>ChatBox</title>
       <link rel="stylesheet" href="chatbox-style.css"/>
   </head>
    
   <body>
        <div >
            <div >
                <div >
                    <img src="../images/Favicon.svg" alt="logo"/>
                    <div >
                        <h2>Message</h2>
                        <p>online</p>
                    </div>
                </div>

                <div  id="message">
                    <div >Hi There!</div>
                    <div >Please type your username then your message</div>
                    <div >Test 1</div>
                    <div >Test 2</div>
                    <div >Test 3</div>
                    <div >Test 4</div>
                    <div >Test 5</div>
                    <div >Test 6</div>
                </div>

                <div  id="chatbox">
                    <form action="../htbin/chatsend.py" method="post" id="searchForm" >
                        <input type="text" name="msg" placeholder="Username">
                        <input type="text" name="msg" placeholder="Enter Message">
                        <button  type="submit"><img src="../images/send.png" alt="send-btn">
                        </button>
                    </form>
                </div>
            </div>
     
        <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="../javascript/chatbox.js"></script>
    </body>
</html>

The problem i have is, that i can't scroll down, and when there is no space anymore it doesn't show anymore the messages that comes after. Here is a website with an example of what i want using bootstrap (and don't want to use bootstrap): https://bbbootstrap.com/snippets/bootstrap-chat-box-custom-scrollbar-template-40453784

CodePudding user response:

Do you mean it this way?

*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Raleway', sans-serif;
    outline: none;
    border: none;
}

body
{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #1a1a1a;
}

.container
{
    display: flex;
    justify-content: center;
    align-items: center;
}

/* chat box section */
.chat-box
{
    width: 400px;
    height: 450px;
    background-color: #fff;
    border-radius: 10px;
    scroll-behavior: auto;
    position: relative;
}

/* about client */

.client
{
    display: flex;
    justify-content: start;
    align-items: center;
    height: 80px;
    background-color: #77b3d4;
    padding: 15px;
}

.client img
{
    width: 50px;
    height: 50px;
    border-radius: 50%;
}

.client-info
{
    color: #fff;
    padding: 15px;
}

.client-info p
{
    color: #008000;
    font-weight: bold;
    font-size: 0.8em;
}


/* chat section */

.chats
{
    width: 100%;
    padding: 0 15px;
    color: #fff;
    position: relative;
    font-size: 0.9em;
    height: 300px;
    overflow-y: scroll;
}

.client-chat
{
    width: 75%;
    word-wrap: break-word;
    background-color: #4f5d73c7;
    padding: 7px 10px;
    border-radius: 10px 10px 10px 0;
    margin: 10px 0;
}

.my-chat
{
    width: 75%;
    word-wrap: break-word;
    background-color: #77b3d4c7;
    padding: 7px 10px;
    border-radius: 10px 10px 0 10px;
    margin: 5px 0 5px auto;
}

/* input section */
.chat-input
{
    display: flex;
    align-items: center;
    width: 100%;
    height: 65px;
    background-color: #fff;
    padding: 15px 1px 15px 1px;
    overflow: hidden;
    position: absolute;
    bottom: 0px;
}

.chat-input input
{
    width: calc(100% - 40px);
    height: 100%;
    background-color: #4f5d7321;
    border-radius: 50px;
    color: #000;
    font-size: 1.2em;
    padding: 0 15px;
}

.send-btn
{
    width: 65px;
    height: 40px;
    background-color: transparent;
    overflow: hidden;
    position: relative;
    margin-left: 5px;
    cursor: pointer;
    transition: 0.4s ease-in-out;
}

.send-btn:active
{
    transform: scale(0.85);
}

.send-btn img
{
    width: 100%;
    height: 100%;
}

/* chat box button section */

.chat-btn
{
    width: 70px;
    height: 70px;
    border-radius: 50%;
    background-color: #fff;
    cursor: pointer;
    overflow: hidden;
    position: fixed;
    bottom: 20px;
}

.chat-btn img
{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    position: absolute;
    top: 0;
    left: 0;
    padding: 5px;
    transition: 0.4s ease-in-out;
}

.chat-btn:hover img
{
    transform: rotate(30deg);
}
<html lang="en">
   <head>
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>ChatBox</title>
       <link rel="stylesheet" href="chatbox-style.css"/>
   </head>
    
   <body>
        <div >
            <div >
                <div >
                    <img src="../images/Favicon.svg" alt="logo"/>
                    <div >
                        <h2>Message</h2>
                        <p>online</p>
                    </div>
                </div>

                <div  id="message">
                    <div >Hi There!</div>
                    <div >Please type your username then your message</div>
                    <div >Test 1</div>
                    <div >Test 2</div>
                    <div >Test 3</div>
                    <div >Test 4</div>
                    <div >Test 5</div>
                    <div >Test 6</div>
                    <div >Test 7</div>
                    <div >Test 8</div>
                    <div >Test 9</div>
                    <div >Test 10</div>
                </div>

                <div  id="chatbox">
                    <form action="../htbin/chatsend.py" method="post" id="searchForm" >
                        <input type="text" name="msg" placeholder="Username">
                        <input type="text" name="msg" placeholder="Enter Message">
                        <button  type="submit"><img src="../images/send.png" alt="send-btn">
                        </button>
                    </form>
                </div>
            </div>
     
        <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="../javascript/chatbox.js"></script>
    </body>
</html>

  • Related