Forum Moderators: open
I want to be able to use the setTimeout function to allow the user to leave the mouse pointer over the image and have the text continue to scroll.
But the way that I've built the scroll function requires that the name of the div ( which the text is in ) is passed into the function using getElementById().
When I've used setTimeout it has been like this:
setTimeout(scrollUp(div),1); <- This one give me an error saying that i didn't wrap the function name in "".
setTimeout("scrollUp(div)",1); <- This one doens't do anything.
setTimeout("scrollUp("+div+")",1); <- This one says "missing ] after element list"
Is there a way that I can get it to work with the function argument?
Thank you for your time.
function scrollUp(div)
{
// we first have to get the position of the div.
var topOfDiv = div.scrollTop;
var heightOfDiv = div.scrollHeight;
// now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
div.scrollTop = div.scrollTop - (div.scrollAmount = 20);
setTimeout("scrollUp('"+div+"')", 1);
}
}
I'm trying to make it so that it can be run anywhere but it wont do the continuous scroll because div is gotten when using the onmouseover="scrollUp(getElementById('divName')".
I tried what you posted but it didn't work unfortunately.
// Global variable. Set this before calling scrollUp
var divToScroll = null;
function scrollUp()
{
if(!divToScroll ) return;
// we first have to get the position of the div.
var topOfDiv = divToScroll.scrollTop;
var heightOfDiv = divToScroll.scrollHeight;
// now we have to check that the div when scrolling
// up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
divToScroll.scrollTop = divToScroll.scrollTop - (divToScroll.scrollAmount = 20);
setTimeout("scrollUp()", 1);
}
}
function scrollUp(divId)
{
div = document.getElementById( divId );
// we first have to get the position of the div.
var topOfDiv = div.scrollTop;
var heightOfDiv = div.scrollHeight;
// now we have to check that the div when scrolling up doesn't scroll past the topOfDiv value.
if(topOfDiv > 0)
{
div.scrollTop = div.scrollTop - (div.scrollAmount = 20);
setTimeout("scrollUp('"+divId+"')", 1);
}
}
As Fotiman said you could use a global variable instead, this would limit you to one active scroll at a time, which in this case is probably Ok. I assume the div has an Id. eg <div id="myDiv" >...