How can I make a html solution here instead of using a javascript hack? -


i trying create chat box (front end) website project.

you may test here: https://jsfiddle.net/ovdzaj2v/5/

now, current fiddle use javascript hack pushchatup() function move messages. don't hack because:

  • it's ugly.
  • it's hard calculate how messages must go because can different sizes (overlap issues).

i want html solution, not sure how it. example, html elements stack below 1 like:

<div id=top> </div> <div id=bottom> </div> 

how can make happen .messages? after javascript appends message, pushchatup() not needed.

js:

function openchat(){     document.getelementbyid("chatinput").style.display = "block";     document.getelementbyid("chatbutton").style.display = "block";     c = document.getelementbyid("chatbutton");     = document.getelementbyid("chatinput");     cm = document.getelementbyid("chatmessage");     c.innerhtml = "";     c.style.height = "100%";     c.style.background = "slategray";     c.style.width = "350px";     c.style.marginleft = "20%";      i.style.display = "block";     c.style.display = "block"; }  function checkkey(e) {     var event = window.event ? window.event : e;     if(event.keycode === 13){ //enter         = document.getelementbyid("chatinput");         sendchat(i.value);         i.value = "";     } }  var clientchat = [];  function sendchat(text){     var div = document.createelement('div');      div.classname = "chatmessage";      div.innerhtml = text;      div.style.display = "block";      pushchatup();      clientchat[50] = div;      document.getelementbyid('chat').appendchild(div); }  function pushchatup(){     var correctorder = [];     var size = 0;     for(var key in clientchat) {         message = clientchat[key];         key = number(key) + 70;         message.style.marginbottom = key.tostring() + "px";         correctorder[key] = message;         ++size;         if(size > 28){             correctorder.splice(key, 1);             message.style.display = "none";             document.getelementbyid('chat').removechild(message);         }         lastmessagesize = message.innerhtml.length;     }     clientchat = correctorder; } 

you cannot set position: absolute each single message, while - - need them positioned relative each other.

that being said, actual problem align messages vertically @ bottom of chat window. best guess use dynamically growing container, absolutely positioned @ bottom.

i've created minified example below (without chat capabilities, idea). you'll still have scroll down using js (scrolltop()), though.

#chat {    position: fixed;    bottom: 0;    right: 0;    width: 200px;    background: #339;    height: 150px;  }    #container {    position: absolute;    bottom: 50px;    max-height: 100px;    overflow-y: auto;    width: 100%;  }    .message {    background: grey;    margin: 4px;    width: fit-content;  }    input {    position: absolute;    margin: 12px;    bottom: 0;  }
<div id="chat">    <div id="container">      <div class="message">hello</div>      <div class="message">hi there</div>      <div class="message">how you? doing?</div>      <div class="message">surfin' web</div>      <div class="message">haha, me toooo :o)</div>    </div>    <input placeholder="i'm useless" />  </div>


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -