Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix the scrollbar always at the bottom of a div?

Tags:

jquery

css

I am doing a simple chat application,I want to fix the scrollbar of a div always at the bottom.just like thisenter image description here

When loading the index page the scrollbar must be at the bottom

Here is my style.css

body{
font: 0.9em monospace;
}
   .messages{
    border: 1px solid #ccc;
    width: 250px;
    height: 210px;
    padding: 10px;
    overflow-y:scroll;

}
.message{
    color: slategrey;

}
 .message p{
    margin: 5px 0;
}
.entry{
   width: 260px;
   height: 40px;
   padding: 5px;
   margin-top: 5px;
   font: 1em fantasy;

}

Here is my index.php

<?php
session_start();
$_SESSION['user'] = (isset($_GET['user']) === TRUE) ? (int) $_GET['user'] : 0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
      <link rel="stylesheet" href="css/styles.css"></link>
  </head>
   <body>
      <div class="chat">
           <div class="messages" id="messages">
           </div>
        <textarea class="entry" placeholder="type here and press enter"></textarea>
    </div>
    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/chat.js"> </script>
</body>

How can i set this,please help me.. Thanks

like image 636
User Avatar asked Mar 26 '13 04:03

User


People also ask

How do I keep my div scrolled to the bottom?

use css position top keep it at the bottom {position : relative; bottom:0;} . Remove the css property once the user has scroll.

How do I change the position of my scroll bar?

We can use the CSS “::-webkit-scrollbar” property which is responsible for changing the shape, color, size, shade, shadow, etc. of the scroll bar. But, here the property which we will use is the direction property of CSS for changing the position of the scroll bar.

Why is there a scroll bar at the bottom of my page CSS?

In your <div id='page'> your CSS for margin is set to margin: 0 auto; Set it to margin: 0 0; (or remove the margin attribute entirely) and the scroll bar goes away.

How do I fix the scrollbar in CSS?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.


2 Answers

Try this jquery :

$(".messages").animate({ scrollTop: $(document).height() }, "slow");
  return false;

and here is the fiddle : http://jsfiddle.net/EX6vs/

or refers to the height of the element (many agree is the right way), as below:

$(".messages").animate({ scrollTop: $(this).height() }, "slow");
  return false;

and here is the fiddle : http://jsfiddle.net/69vpnyu1/

like image 121
Fredy Avatar answered Oct 11 '22 12:10

Fredy


you want something like this, where box is the div that contains your chat. call these on page load.

var box = document.getElementById('Box');
box.scrollTop = box.scrollHeight;

also call this when you post a new chat.

i had created a similar application using google app engine. you can have look at it here

http://chatter-now.appspot.com/

feel free to use it as reference. although you are using php, the visual aspects might be helpful.

like image 36
btevfik Avatar answered Oct 11 '22 10:10

btevfik