Scroll DIV, HTML elements using Javascript

JavaScript Add comments

Javascript Code

<script>
var sc, maxscroll, minscroll=0;
var uptime, downtime
var pixelsteps=5;
window.onload = function()
{
	sc = document.getElementById('wn3');
	maxscroll = sc.scrollHeight - sc.offsetHeight
	minscroll = 0		
}
 
function scrollup()
{
	uptime = window.setInterval('scrollUp()',1)
}
function scrolldown()
{
	downtime = window.setInterval('scrollDown()',1)
}
 
function scrollUp()
{
	if(sc.scrollTop>0)
		sc.scrollTop = sc.scrollTop - pixelsteps
	window.location.hash = sc.scrollTop		
}
function scrollDown()
{
	if(sc.scrollTop<maxscroll)
		sc.scrollTop = sc.scrollTop +  pixelsteps
	window.location.hash = sc.scrollTop
}
 
function clearscroll()
{
	if(uptime)window.clearInterval(uptime)
	if(downtime)window.clearInterval(downtime)	
}
</script>

HTML Code

 
<img src="up.gif" onmousedown="scrollup();" onmouseup="clearscroll();" /><br>
<img src="dn.gif" onmousedown="scrolldown();" onmouseup="clearscroll()"  />

== Version 2 ==

var sc, maxscroll, minscroll=0;
var uptime, downtime
var pixelsteps=2;
var delay = 10
function inscroll(src)
{
	if(arguments.length>1)pixelsteps = arguments[1]	
	if(arguments.length>2)delay = arguments[2]		
 
	sc = document.getElementById(src);
	maxscroll = sc.scrollHeight - sc.offsetHeight
	minscroll = 0		
}
 
function scrollup()
{
	uptime = window.setInterval('scrollUp()',delay)
}
function scrolldown()
{
	downtime = window.setInterval('scrollDown()',delay)
}
 
function scrollUp()
{
	if(sc.scrollTop>0)
		sc.scrollTop = sc.scrollTop - pixelsteps
}
function scrollDown()
{
	if(sc.scrollTop<maxscroll)
		sc.scrollTop = sc.scrollTop +  pixelsteps
}
 
function clearscroll()
{
	if(uptime)window.clearInterval(uptime)
	if(downtime)window.clearInterval(downtime)	
}

You have to call inscroll(‘divid’,pixelincrementvalue,delay);

One Response to “Scroll DIV, HTML elements using Javascript”

  1. david b Says:

    Will this code work, if I want to scroll a div inside of a div? Do you understand?

Leave a Reply

Entries RSS