vForums Support > Programming & Coding :: Programming Discussion :: > scroll()

scroll() - Posted By Marc (cr0w) on 1st Mar 08 at 12:24am
Now, I know that in JS you can use scroll(x,y) to scroll to a specific xy coordinate on the page. But, is there a way to scroll to a specific coordinate within a div, or td, or something similar?

I've been reading up on it lately, but haven't been able to find an answer.

Re: scroll() - Posted By Michael (wrighty) on 1st Mar 08 at 12:30am
have you tried:

document.getElementById('x').scroll(x,y);


*hasn't tried it*

Re: scroll() - Posted By Ross (admin) on 1st Mar 08 at 12:32am
You'd have to find the position of the div/td first and then add that to the coordinates you want to go to. Take the get_position function I use as an example of how to get that:

Code: JavaScript
 
  1. function get_position(id) {
  2. var top = 0;
  3. var left = 0;
  4. var obj = document.getElementById(id);
  5. if (obj.offsetParent) {
  6. left = obj.offsetLeft;
  7. top = obj.offsetTop;
  8. while (obj = obj.offsetParent) {
  9. left += obj.offsetLeft;
  10. top += obj.offsetTop;
  11. }
  12. }
  13. return Array(top, left);
  14. }
 



Edit: Reading Wrightys reply it looks like we're both thinking of different things {Unsure}

Re: scroll() - Posted By Marc (cr0w) on 1st Mar 08 at 12:39am
@Wrighty: I've tried that, no avail.

@Ross: I mean if I have a div with the overflow set to scroll, how can I make that scroll to a certain coordinate rather than the whole page. {Wink}

Re: scroll() - Posted By Simie (simie) on 3rd Mar 08 at 8:31pm
I remembered looking at Panic: Coda's site a few weeks ago, and they use a scrolling div.
From what they use, try this:

element.scrollTop = amount;

element.scrollLeft = amount;


{Smile}

Re: scroll() - Posted By Marc (cr0w) on 3rd Mar 08 at 8:33pm
 
I remembered looking at Panic: Coda's site a few weeks ago, and they use a scrolling div.
From what they use, try this:

element.scrollTop = amount;

element.scrollLeft = amount;


{Smile}


Knew there was a way to do it; I'll try it in a sec. {Smile}

Yay, it works! {Grin}

Thanks Simie. {Smile} Smiley

Re: scroll() - Posted By Simie (simie) on 3rd Mar 08 at 8:50pm
No problem {Smile}

Btw:
Error: x is not a function
Source File: http://www.cr0wonline.com/_/scrollDiv.php
Line: 1

{Sad}

Re: scroll() - Posted By Michael (wrighty) on 3rd Mar 08 at 8:52pm
Neither is y! {Sad}

Re: scroll() - Posted By Marc (cr0w) on 3rd Mar 08 at 8:57pm
I was testing some stuff. {Tongue Out}

Re: scroll() - Posted By Simie (simie) on 3rd Mar 08 at 8:58pm
Glad to know it worked {Smile}

Re: scroll() - Posted By Marc (cr0w) on 3rd Mar 08 at 9:02pm
 
Glad to know it worked {Smile}


Thanks for the help. {Smile}

*Heads outside*