943,832 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 22nd, 2005
0

AutoScrolling a DIV tag

Expand Post »
Sorry for the double post. but it wasn't even remotely in the right spot before.

I wrote this little code segment to illustrate the idea of what I want. I'd like to be able to automatically scroll the DIV to the bottom when you add stuff to it.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Auto Scrolling Div</title>
  4. <head>
  5. <script type="text/javascript">
  6. <!--
  7. function addStuff() {
  8. var stuff = document.getElementById('foo').value;
  9. oldStuff = document.getElementById('bar').innerHTML;
  10. newStuff = oldStuff + ' ' + stuff;
  11. document.getElementById('bar').innerHTML = newStuff;
  12. }
  13. -->
  14. </script>
  15. </head>
  16. <body>
  17. <div id="bar" style="overflow: auto; height: 150px; width: 100px;">
  18. Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff
  19. Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff
  20. Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff Stuff
  21. </div>
  22. <input type="text" id="foo">
  23. <input type="submit" value="Add Stuff" onclick="addStuff()">
  24. </body>
  25. </html>

So, you type something in and click Add Stuff. It will scroll to the bottom so you can see what you just added. That would be awesome.

Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zeromancer is offline Offline
8 posts
since Aug 2005
Aug 25th, 2005
0

Re: AutoScrolling a DIV tag

Research the JavaScript "scrollTo" method, then post a follow-up if you have any specific questions about it.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Aug 25th, 2005
0

Re: AutoScrolling a DIV tag

I founf that, but the next problem i ran into was finding out where i should scroll to. in other words, finding out where the bottom of the DIV is.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zeromancer is offline Offline
8 posts
since Aug 2005
Aug 26th, 2005
0

Re: AutoScrolling a DIV tag

Understood. Your next assignment, then, is to research "offsetTop" and "offsetParent".
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Sep 21st, 2005
0

Re: AutoScrolling a DIV tag

Zero, you ever get this? I have a div that holds an asp.net Label and need it to scroll to the bottom.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mbowles is offline Offline
2 posts
since Sep 2005
Sep 21st, 2005
0

Re: AutoScrolling a DIV tag

You have to explain exactly what it is you want. You cannot scroll, as far as I know, the inner contents of a DIV element. However, you can scroll the browser window, using the "scrollTo()" method.

You can know the size of an element, using it's "clientHeight" property, for example, and its position on the page, using offsetTop & offsetParent.

Here's a simplistic example of scrolling:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4.  
  5. function myScroll()
  6. {
  7. x = document.getElementById("myDiv");
  8. h= x.clientHeight;
  9. self.scrollTo(0,h);
  10. }
  11.  
  12. </script>
  13. </head>
  14. <body onload="myScroll();">
  15. <div id="myDiv">
  16. <p>
  17. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  18. Donec tristique lectus sit amet est. Vivamus sit amet lacus.
  19. Morbi convallis sem nec lectus.
  20. Nullam in odio ac augue porttitor semper.
  21. Nam leo. Morbi varius molestie felis. Etiam egestas.
  22. Donec vitae mauris vitae nisi gravida pharetra. In molestie eros eu tellus.
  23. Suspendisse a mi vitae nulla vehicula lacinia.
  24. Ut pellentesque lobortis ipsum. Quisque a leo. Curabitur tincidunt.
  25. Fusce est velit, condimentum sed, convallis in, blandit hendrerit, eros.
  26. </p>
  27. </div>
  28. </body>
  29. </html>

I simply measure the clientHeight of the DIV element, and tell the browser to scroll to that position. This doesn't take into account that the DIV might be nested, etc. You'll need to use the offsetTop and offsetParent properties to make the code robust. Search for those terms, you should easily find everything you need, which is why I didn't spell it all out for the previous poster.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Sep 21st, 2005
0

Re: AutoScrolling a DIV tag

Yeah, I needed to scroll the contents of a div. I went back and modified my StringBuilder to use insert instead of append and now my text gets added to the top of the display so with aut scroll of style tags kicks in, it scrolls the old stuff out the bottom. Was building a chat system for our portal.

thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mbowles is offline Offline
2 posts
since Sep 2005
Sep 23rd, 2005
0

Re: AutoScrolling a DIV tag

here's the answer. thanks to those that tried to help.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>w00t!</title>
  4. <script type="text/javascript">
  5. <!--
  6. function addStuff()
  7. {
  8. var loc = document.getElementById('myDiv');
  9. var stuff = document.getElementById('in').value;
  10. var old = loc.innerHTML;
  11. loc.innerHTML = old+stuff+'<br />';
  12. var div = document.getElementById('myDiv');
  13. h = div.scrollHeight;
  14. div.scrollTop = h;
  15. }
  16. -->
  17. </script>
  18. </head>
  19. <body>
  20. <div id="myDiv" style="overflow: auto; height: 200px; width: 300px; border: 1px solid #666; background-color: #ccc; padding: 8px"></div>
  21. <br />
  22. <input type="text" id="in">
  23. <input type="button" onclick="addStuff()" value="Add">
  24. </body>
  25. </html>
Reputation Points: 10
Solved Threads: 1
Newbie Poster
zeromancer is offline Offline
8 posts
since Aug 2005
Jan 8th, 2008
0

Re: AutoScrolling a DIV tag

Hi guys, I know this thread was years ago, but I think many many people are looking for a solution for this problem but not finding anything.

Scrolling a scrollable DIV tag is pretty easy by using the fabulous prototypejs framework by www.prototypejs.org.

Lets see what we've got here...

some html:

<div id='scrollContainer'> <!-- set stiggi yiggy in your css file (#scrollContainer) ;) !-->
  <div id='scrollTo1'>maaaany</div>
  <div id='scrollTo2'>maaaany</div>
  <div id='scrollTo3'>maaaany</div>
  <div id='scrollTo4'>maaaany</div>
  <div id='scrollTo5'>...content</div>
</div>

Now we do the script thing...

Whith that you can easily scroll to a specific element in your scroll container:

$('scrollContainer').scrollTop = $('scrollTo3').cumulativeScrollOffset()[1];
Whoops... one line of code? oh yes baby, yes!

I LOVE PROTOTYPE!
Last edited by dRoot84; Jan 8th, 2008 at 8:14 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dRoot84 is offline Offline
2 posts
since Jan 2008
Jan 9th, 2008
0

Re: AutoScrolling a DIV tag

Click to Expand / Collapse  Quote originally posted by dRoot84 ...
Hi guys, I know this thread was years ago, but I think many many people are looking for a solution for this problem but not finding anything.

Scrolling a scrollable DIV tag is pretty easy by using the fabulous prototypejs framework by www.prototypejs.org.

Lets see what we've got here...

some html:

<div id='scrollContainer'> <!-- set stiggi yiggy in your css file (#scrollContainer) ;) !-->
  <div id='scrollTo1'>maaaany</div>
  <div id='scrollTo2'>maaaany</div>
  <div id='scrollTo3'>maaaany</div>
  <div id='scrollTo4'>maaaany</div>
  <div id='scrollTo5'>...content</div>
</div>

Now we do the script thing...

Whith that you can easily scroll to a specific element in your scroll container:

$('scrollContainer').scrollTop = $('scrollTo3').cumulativeScrollOffset()[1];
Whoops... one line of code? oh yes baby, yes!

I LOVE PROTOTYPE!

You don't need prototype for something this simple. The post just before this actually had the solution.

You can also do something as simple as:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. div.scrollTop = 10000;

Giving any element a scrollTop of a number larger than its height will scroll it to the bottom. So any large number you know the div height will not exceed will work.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: sample Programs using Ajax controls links or programing code
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Displaying cookie contents in safari?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC