Scroll is always at the base of a div

2

I'm automatically fetching information from a database, from which you create a scroll in a div. The problem is that I have to scroll down to follow the information that appears on the screen. How would I have the scroll scroll down automatically with the information, regardless of how much the database brings? I tested this code, but it did not work:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script><script>$("#resultados").scrollTop($("#resultados")[0].scrollHeight);
</script>

<div id="resultados" style="overflow: scroll">

Aqui aparece os resultados

</div>
    
asked by anonymous 04.12.2015 / 15:21

1 answer

1

You have to set a height fixed to your div .

$("#resultados").scrollTop($("#resultados")[0].scrollHeight);
#resultados {
  height: 300px;
  width: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><divid="resultados" style="overflow: scroll">

  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis doloremque dolorum expedita accusamus error asperiores harum ipsam culpa odit blanditiis rerum aperiam et praesentium ea autem, esse veniam nulla deleniti!

  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Hic molestiae ratione iusto laudantium illum velit sit reprehenderit sed officiis consequuntur alias fugit optio aspernatur voluptatum dolores earum, aut. Eum, reiciendis?
  </p>

  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore consectetur reprehenderit et incidunt laudantium fugiat sed commodi reiciendis rerum beatae sit repudiandae ipsum rem facere neque, autem? Reiciendis, modi eveniet!</p>

  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae, asperiores dolores illo, error nihil incidunt hic vero, pariatur tempora tempore corporis! Repellat doloremque et cum neque blanditiis nam, quaerat alias!
  </p>
</div>
    
04.12.2015 / 15:49