Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scrolling a document a certain Distance?

Status
Not open for further replies.

Mindloss

Technical User
Feb 27, 2005
4
US
Hey is there any way to scroll a document (by clicking a link) a certain distance? and by scrolling I mean in increments... smoothly.

any help appreciated!
 
You could use something like this:
Code:
<script type='text/javascript'>
scrollInt = '';
vScrollDist = 2; //pixels to scroll window vertically
scrollTime = 2; //milliseconds between scrolls
sUntil = 0;
origScroll = '';
function doScroll(until)
{
if (!origScroll) origScroll = document.documentElement.scrollTop;
sUntil = until;
if (document.documentElement.scrollTop >= parseInt(until+origScroll))
{clearInterval(scrollInt);}
else {
if (!scrollInt) scrollInt = setInterval("doScroll(sUntil)",scrollTime);
else {scrollBy(0,vScrollDist);}
}
}
</script>
then in the link:
Code:
<a href='javascript:void(0)' onClick='doScroll(275)'>Scroll me</a>
where 275 is the distance you want to scroll by

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Hey, thanks.. but that didn't really work.. at all.. :[
 
Here ya go,
Code:
<script type='text/javascript'>
scrollInt = '';
vScrollDist = 2; //pixels to scroll window vertically
scrollTime = 2; //milliseconds between scrolls
sUntil = 0;
origScroll = '';
function doScroll(until)
{
if (!origScroll) origScroll = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
sUntil = until;
if (document.documentElement.scrollTop >= parseInt(until+origScroll) || document.body.scrollTop >= parseInt(until+origScroll))
{clearInterval(scrollInt);}
else {
if (!scrollInt) scrollInt = setInterval("doScroll(sUntil)",scrollTime);
else {scrollBy(0,vScrollDist);}
}
}
</script>
Code:
<a href='javascript:doScroll(275)'>Scroll me</a>

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Thanks, that works much better.. but um, is there a way to scroll back up?
 

Try this:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		var vScrollDist = 2;	// pixels to scroll window vertically
		var scrollTime = 2;		// milliseconds between scrolls
		var originalAmount = 0;

		function smoothScrollBy(totalAmount) {
			if (originalAmount == 0) originalAmount = totalAmount;
			if (originalAmount < 0 && totalAmount < 0) {
				totalAmount += vScrollDist;
				scrollBy(0, 0 - vScrollDist);
			} else if (originalAmount > 0 && totalAmount > 0) {
				totalAmount -= vScrollDist;
				scrollBy(0, vScrollDist);
			} else {
				originalAmount = 0;
				return;
			}
			setTimeout('smoothScrollBy(' + totalAmount + ')', scrollTime);
		}
	//-->
	</script>
</head>

<body>
	<a href="javascript:smoothScrollBy(275);">Scroll me down</a>
	<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla enim sapien, nonummy gravida, egestas vitae, placerat sed, turpis. Mauris blandit felis et est. Nam ultricies euismod wisi. Nulla tincidunt bibendum mauris. Morbi ac arcu vel ante pharetra scelerisque. Sed ac tellus sit amet metus condimentum elementum. Quisque at lectus. Fusce urna magna, sollicitudin at, feugiat in, molestie quis, quam. Praesent pellentesque lobortis turpis. Ut euismod, est vel vehicula facilisis, augue eros aliquam magna, eget imperdiet urna eros sit amet orci.</p>
	<p>In hac habitasse platea dictumst. Donec interdum tellus et pede. Curabitur nonummy varius purus. Vivamus ut mauris. Maecenas felis tortor, cursus nec, consequat et, mattis ut, elit. Donec sed nibh porta enim facilisis suscipit. Ut orci. Quisque libero purus, lacinia a, varius et, aliquet et, libero. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus fringilla purus. Nam vitae dui id risus vehicula lobortis. Maecenas sit amet sapien eget ipsum sollicitudin elementum. Aliquam turpis metus, tempor quis, ultricies quis, convallis eget, est. Morbi tempor nibh sed ipsum.</p>
	<p>Sed ac sapien eu mauris pretium tincidunt. Quisque tempor, augue ut vehicula eleifend, sem leo nonummy dolor, a eleifend diam sapien in felis. Cras augue odio, aliquet sit amet, bibendum ac, sagittis in, sem. Sed rhoncus mauris ac justo. Phasellus fringilla lacus quis nunc. Vestibulum leo eros, ultricies ut, cursus non, suscipit vitae, libero. Aliquam erat volutpat. Donec vulputate porttitor lacus. Duis accumsan. Sed aliquam libero sit amet massa. Aenean eget nulla sit amet neque sollicitudin porttitor. Proin nec elit. Nullam nec urna eu dolor lobortis venenatis. Duis ac sem vel elit pharetra tempus.</p>
	<a href="javascript:smoothScrollBy(-275);">Scroll me up</a>
</body>
</html>

Hope this helps,
Dan

 
Just curious, what exactly was wrong with my solution?

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 

I didn't see anything wrong with it... But the poster asked for a solution that would enable them to scroll up as well as down, and it was easier for me to code one from scratch than to examine how you had chosen to code yours and modify it.

Dan

 
Alright, thanks for the response Dan. I was just wondering if the poster had any problem in implementation .

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top