Scroll Bar Information

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Scroll Bar Information

Post by Gen-ik »

You can control a scroll bar on a page using window.scrollBy and window.scrollTo... but how can I get the current position of the scrollbar and the maximum position it can be?

I'm working on something that needs the scrollbar position as a percentage and in order to do that I need to know how to do the above.

I've checked out serveral web sites with no luck.


Anyone?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try this one

Code: Select all

<html>
	<head>
		<script langauge="javascript">
			var otherWin;
			function showPos()
			&#123;
				if(otherWin!=null)
				&#123;
					sT = otherWin.document.body.scrollTop;
					sH = otherWin.document.body.scrollHeight;
					cH = otherWin.document.body.clientHeight;
				
					document.getElementById("scrTop").value = sT;
					document.getElementById("scrHeight").value = sH;
					document.getElementById("clntHeight").value = cH;
					document.getElementById("visFrom").value = sT*100/sH;
					document.getElementById("visTo").value = (sT+cH)*100/sH;
				&#125;
			&#125;
			
			function initWindow()
			&#123;
				otherWin = window.open("about:blank","win2","width=310,height=50,left=0,top=0,scrollbars=yes,resizable=yes,toolbar=yes,dependent=yes");
				otherWin.document.write("<html><head></head><body><h1>1<br/>2<br/>3<br/>4<br/>5<br/>6<br/>7<br/>8<br/></h1></body></html>\n");
				window.setInterval("showPos()",100);
			&#125;
		</script>
	</head>
	<body onLoad="initWindow();">
		<input type="text" id="scrTop" />scrollTop<br/>
		<input type="text" id="scrHeight" />scrollHeight<br/>
		<input type="text" id="clntHeight" />clientHeight<br/>
		visible: <input type="text" id="visFrom" />%to<input type="text" id="visTo" />%

	</body>
</html>
and do not close the client-window first ;)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Thanks Volka I'll give that a try.

Have a Happy Christmas :)
Post Reply