0

I have made a simple chat UI using bootstrap 4 cards, I need a way to fix the message input to the bottom and to scroll the conversation automatically when the height of the window is filled by the sent and incoming messages. I've tried to fix the message input using a fixed position with the bottom set to 0 but the input then will take all the window width and this isn't what I want. Also for the text messages, I've tried to scroll the window using jQuery but it will not work and I need to scroll the window manually. another issue is with the code I'm using to add the sent and received messages to the DOM, I don't want to duplicate the code, but I can't imagine another solution to differentiate the incoming messages from the outgoing.

Here is the code I've:

function scrollConversation() {
  $(".list-group").stop()
    .animate({
      scrollTop: $(".list-group")[0].scrollHeight
    }, 1000);
}

function outMessage(message) {
  //var html = '<li class="list-group-item">';
  var html = '<p class="speech-bubble-out float-right">' + message + '</p>';
  //html += '</li>';
  $('.list-group').append(html);
  //return ;
}

function inMessage(message) {
  //var html = '<li class="list-group-item">';
  var html = '<p class="speech-bubble-in float-left">' + message + '</p>';
  //html += '</li>';
  $('.list-group').append(html);
}
body {
  background-color: #f5f5f5;
}

body .speech-bubble-out {
  padding: 1em;
  position: relative;
  background: #dad8da;
  border-radius: .4em;
}

body .speech-bubble-out:after {
  content: '';
  position: absolute;
  right: 0;
  top: 50%;
  width: 0;
  height: 0;
  border: 0.563em solid transparent;
  border-left-color: #dad8da;
  border-right: 0;
  border-bottom: 0;
  margin-top: -0.281em;
  margin-right: -0.562em;
}

body .speech-bubble-in {
  padding: 1em;
  position: relative;
  background: #efe8b8;
  border-radius: .4em;
}

body .speech-bubble-in:after {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: 0;
  height: 0;
  border: 0.563em solid transparent;
  border-right-color: #efe8b8;
  border-left: 0;
  border-bottom: 0;
  margin-top: -0.281em;
  margin-left: -0.562em;
}
<div class="container">
  <div class="row justify-content-center">
    <div class="col-sm-12 col-md-7 col-lg-7 card">
      <div class="card-body">
        <!-- message list -->
        <ul class="list-group"></ul>
        <!-- text input -->
        <div class="input-group">
          <input type="text" class="form-control" name="message" id="">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary" id="sendMessage">Send</buttom>
                  <button class="btn btn-outline-secondary" id="sendAudio">Send Audio</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Any help will be appreciated.

cloned
  • 6,346
  • 4
  • 26
  • 38
jihuuNI
  • 551
  • 2
  • 5
  • 17
  • 1
    Are you getting any errors when you hit f12 under 'Console' on the Chrome window (or whatever browser)? – EGC Sep 12 '19 at 06:46
  • @EGC no, no I don't get any error – jihuuNI Sep 12 '19 at 06:49
  • 1
    Okay, good start. With the 'message input' sitting at the bottom of the page, you've tried fixed, as per this example, yeah? (If so please explain why it didnt work for you / you didn't like it - like, if the only issue was that it took 100% of the width, remove the width:100% from your css) https://stackoverflow.com/questions/8824831/make-div-stay-at-bottom-of-pages-content-all-the-time-even-when-there-are-scrol – EGC Sep 12 '19 at 06:50
  • @EGC I've tried with 100% of width but also with auto or with the width not setted from the css. The results are different, with the 100% it will take all the window width, using auto the input will become smaller than the `col-lg-7` that is the width of the parent container and without setting it, it will be displayed on the bottom right corner of the window, consider that the row is set to be on the center of the window. This is a small problem, the important thing is also to scroll the `
      ` that will contain the messages.
    – jihuuNI Sep 12 '19 at 07:04
  • 1
    Okay, thats all okay. I'm very confused by what you're asking for here. It might be an idea to redraft your question in dot points or something - or even just articulate it a little more here in the comments. I'm struggling to grasp the parts / problems you are facing + what you've tried + what the results from your attempts were. Sorry, happy to help, just need more info! – EGC Sep 12 '19 at 07:11
  • @EGC Ok, sorry for the confusion, I explain better what help I need. 1) I want to fix to the bottom the text input and mantaining the `col-7` width (if I use the fixed position it will ignore the parent container div and row). 2) I need to scroll the messages appended inside the `
      `, the code I've tested will not execute, 3) I want to find a better way to append the incoming and the outcoming messages without duplicating the code
    – jihuuNI Sep 12 '19 at 07:26
  • Cool, have you tried something like: 'position: absolute; bottom: 0;' on the div/section where a user types a message? Note: if you use this, you will need to also fix the outer div which contains incoming messages to 'position: absolute; bottom: /* The height of the user input section */;' so that it also sticks to the bottom of the page, but it will need an offset as to not be on top of where the user types in their message. Hopefully that helps some? – EGC Sep 12 '19 at 22:04
  • I need to try this solution for the text input. – jihuuNI Sep 13 '19 at 08:39

0 Answers0