0

I'm making a simple chatbot system and I want the chatbot to automatically scroll to bottom of the chat after user input. this is my index.php:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0">
    <title>Chatbot</title>
    <?PHP
    echo shell_exec("python test.py 'parameter1'");
    ?>


    <link rel="stylesheet" href="static/css/chat.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
   
</head>

<body>
    <!-- CHAT BAR BLOCK -->
    <div class="container">
        <div class="top-bar">
            <p style="color:white; text-align: center">Security Chatbot
                <i id="chat-icon" style="color: white;" class="fa fa-fw fa-comments-o"></i>
            </p>
        </div>

        <div class="chatbot">
            <!-- Message Container -->
            <div class="outer-container">
                <div class="chat-container">
                    <!-- Messages -->
                        <div id="chatbox">
                            <h5 id="chat-timestamp" style="text-align: center;"></h5>
                            <p id="botStarterMessage" class="botText" name="botresponse"></p>
                            <button id="option1" >Phishing Attack</button>
                            <button id="option2" >Denial of Service (DoS) Attack</button>
                            <button id="option3" >Password Attack</button>
                            <button id="option4" >Malware Attack</button>
                            <button id="option5" >Cloud Attack</button>
                     
                           
                            
                        </div>
                </div>
            </div>
        </div>


        <!-- User input box -->
        <div class="chatbar_input_block">
            <div id="userInput">
                <input id="textInput" class="input-box" type="text" name="textInput"
                    placeholder="Tap 'Enter' to send a message" autofocus="on">
                <p></p>
            </div>


            <div class="chat-bar-icons">
                <i id="chat-icon" style="color: #333;" class="fa fa-fw fa-send" onclick="sendButton()"></i>
            </div>

        </div>

        <div id="chat-bar-bottom">
            <p></p>
        </div>
        
    </div>

</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- <script src="static/scripts/responses.js"></script> -->
<script src="static/scripts/chat.js"></script>

<!-- <script type=text/javascript>
    function scroll(){
        var scrollMsg = document.querySelector('outer-container');
        scrollMsg.scrollTop = scrollMsg.scrollHeight;
    }
    scroll();
</script>     -->

as u can see I've tried the scroll() function. but it doesn't work I've also tried scrollintoview function which is in the chat.js file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

it used to work but after I added some css and html code it just doesn't work anymore may I know any fix for this? this is css

html {
    scroll-behavior: smooth;
    font-family: 'Times New Roman', Times, serif;
}

body {
    background-color: lightblue;
    margin: 0 auto;
    align-items: center;
    overflow: hidden;
}

.container {
    margin: 2% auto;
    cursor: pointer;
    width: 60%;
    min-width: 600px;
    height: 560px;
    font-size: 18px;
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
    border-radius: 10px;
    /* overflow: auto; */
}

.top-bar {
    background-color: darkblue;
    border-radius: 10px 10px 0px 0px;
    height: 40px;
    width: 100%;
    padding: 5px;
}

.chatbot {
    background-color: #f1f1f1;
    height: 445px;
    padding: 3px;
    overflow: auto;
}

.outer-container {
    min-height: 100%;
    bottom: 0%;
    position: relative;
    overflow: auto;
}

.chat-container {
    position: absolute;
    bottom: 0;
    max-height: 100%;
    text-align: center;
    width: 100%;
    overflow-y: scroll;
    overflow: auto;
    hyphens: auto;
    scroll-behavior: smooth;
    flex-direction: column-reverse;
}

::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: #888;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Chat Input Bar */

.chatbar_input_block {
    width: 100%;
    height: 75px;
    bottom: 0;
    float: left;
    box-sizing: border-box;
    justify-content: space-between;
    align-items: center;
    background-color: lightcoral;
    padding: 0px 0px 2px 12px;
    border-radius: 0px 0px 10px 10px;
}

.chat-bar-icons {
    padding: 7px 0px 0px 1px;
    justify-content: space-evenly;
    box-sizing: border-box;
    width: 10%;
    float: right;
    font-size: 25px;
}

/* Chat bubbles */

#userInput {
    width: 87%;
}

.input-box {
    float: left;
    border: none;
    box-sizing: border-box;
    width: 100%;
    border-radius: 10px;
    padding: 12px;
    font-size: 16px;
    color: #000;
    background-color: white;
    outline: none
}

.userText {
    color: white;
    font-family: Helvetica;
    font-size: 16px;
    font-weight: normal;
    text-align: right;
    clear: both;
}

.userText span {
    line-height: 1.5em;
    display: inline-block;
    background: #5ca6fa;
    padding: 10px;
    border-radius: 8px;
    border-bottom-right-radius: 2px;
    max-width: 80%;
    margin-right: 10px;
    animation: floatup .5s forwards
}

.botText {
    color: #000;
    font-family: Helvetica;
    font-weight: normal;
    font-size: 16px;
    text-align: left;
}

.botText span {
    line-height: 1.5em;
    display: inline-block;
    background: #e0e0e0;
    padding: 10px;
    border-radius: 8px;
    border-bottom-left-radius: 2px;
    max-width: 80%;
    margin-left: 10px;
    animation: floatup .5s forwards
}

/* @keyframes floatup {
    from {
        transform: translateY(14px);
        opacity: .0;
    }

    to {
        transform: translateY(0px);
        opacity: 1;
    }
}

/* Desktops and laptops ----------- */
@media screen and (min-width : 1024px) and (max-height: 1310px) {
    .container {
        width: 60%;
        height: 60%;
    }
}


/* Large screens ----------- */
@media only screen and (min-width : 1324px) and (max-height: 1600px) {
    .container {
        width: 60%;
        height: 80%;
    }
} 
Kah Shen
  • 1
  • 2
  • Did you check the console for errors? **Always check the browser console for errors** - such as `Cannot read properties of null (reading 'scrollIntoView')` - meaning `document.getElementById("#chat-bar-bottom")` didn't find an element. Possibly because you don't have any elements with id `#chat-bar-bottom` (hint: the ID is `chat-bar-bottom`). Typo removed, appears to [work fine](https://jsfiddle.net/oytf9ngz/) – freedomn-m Aug 17 '22 at 09:07
  • no the console has no errors I've changedthe ID as chat-bar-bottom and still it doesn't work – Kah Shen Aug 17 '22 at 09:22
  • Then please update your code to include a *complete* snippet ([edit] and click `[<>]`) that *demonstrates* the issue (with relevant css), as, with the html/js provided, the only issue is `getElementById("#id")`, as shown in the above fiddle. – freedomn-m Aug 17 '22 at 09:35
  • alright updated – Kah Shen Aug 17 '22 at 10:15
  • Added your css to the above jsfiddle, giving: https://jsfiddle.net/rsdoxgqz/. Your scrollable chat div is a fixed height, and your body is set to `overflow:hidden` so the body is not scrollable. – freedomn-m Aug 17 '22 at 10:40
  • Change `body { overflow:scroll }` and make the window smaller than the chat box so your body can be scrolled - and it works fine: https://jsfiddle.net/rsdoxgqz/1/ – freedomn-m Aug 17 '22 at 10:41
  • I sent the 7th message to the chatbot and the scroll is not working anymore – Kah Shen Aug 17 '22 at 12:47
  • You mean the *chat messages panel*. My bad, I was looking at your (now removed) code for: `document.getElementById("chat-bar-bottom").scrollIntoView(true);` which scrolls the *window*(``) to show #chat-bar-bottom. As your css fixes this to the bottom of the chat scroll window, there's nothing to scroll. You need to scroll the chat window, not the whole document/window/body. – freedomn-m Aug 17 '22 at 12:55
  • Your `.chat-container` css just needs `display:flex`. You already have `flex-direction:` but that only works with `display:flex`. Then it works fine as a css-only solution (with js removed) https://jsfiddle.net/jg04xLqb/ – freedomn-m Aug 17 '22 at 13:08
  • As provided in this existing answer: [css-only automatically scroll to the bottom](https://stackoverflow.com/a/68874831/2181514) – freedomn-m Aug 17 '22 at 13:08

0 Answers0