Page 1 of 1

Scroll Bar Information

Posted: Thu Dec 19, 2002 8:34 am
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?

Posted: Mon Dec 23, 2002 2:59 am
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 ;)

Posted: Mon Dec 23, 2002 5:15 am
by Gen-ik
Thanks Volka I'll give that a try.

Have a Happy Christmas :)