views:

501

answers:

4

I am trying to find a simple way to have a div with just text in it automatically scroll the text vertically. I don't want to use a framework (though I do use Prototype, so if it is easier using Prototype then that is fine, but no Scriptalicious).

I assume there has got to be a way to do this with a few lines of code, but I am not familiar enough with Javascript to know how to most effectively do that.

A: 
function scrollDivUp(id){
    document.getElementById(id).scrollTop-=1
    timerUp=setTimeout("scrollDivUp('"+id+"')",10)
}

try something like that maybe.

you could also change the .scrollTop-=1 to .scrollTop+=1 to scroll the other way.

You would also need a scrollable div which can be done by constraining the size and setting the overflow style property ie. style="width:200px; height:300px; overflow:auto"

Tim
This would work, but it still doesn't solve the problem of looping. I'm trying to have a continuous scrolling loop.
James Simpson
To somewhat loop I can check if the scrollbar has reached the bottom and then just go back to the top, but this looks quite tacky, there must be a better way to do a smooth loop through.
James Simpson
what if the contents of the div had a large area of whitespace at the top and bottom, once the text has scrolled off the bottom, you can jump to the top again and the whitespace should make it look as though the text is scrolling in from the top again?
Tim
you could also probably hide the scrollbar in that case.. overflow:hidden;
Tim
A: 

Try changing the div's scrollTop. There is an example here.

Annie
+2  A: 

This might not be conventional but you can try the <marquee> tag

it works both in IE and FF, and the last time I checked, safari too.

<marquee behavior="scroll" direction="up" height="250" 
   scrollamount="2" scrolldelay="10"">
  Your content goes here
</marquee>

should give you what you want,
and you can style them like any <div>...
and then there is the added advantage of having no javascript...

Edit in response to your comment

It gets better, try this in any browser

onmouseover="this.stop()" onmouseout="this.start()"

And this in IE

style="filter:progid:DXImageTransform.Microsoft.Alpha( Opacity=0,
FinishOpacity=100, 
Style=1, StartX=0,  FinishX=0, StartY=0, FinishY=10) 
progid:DXImageTransform.Microsoft.Alpha( Opacity=100, FinishOpacity=0, 
Style=1, StartX=0, FinishX=0, StartY=90, FinishY=100)"

As attributes of the marquee tag...

pǝlɐɥʞ
Interesting, I wasn't aware that the marquee tag could have that much customization.
James Simpson
A: 

I see that here isnt given the correct answer. I think you have to look at cloneNode() for instance. And clone the element you want to scroll, when the first element is at the last point of scrolling then place the duplicated element after the first element, and when that duplicated element is almost at the end, place the original after the duplicate and so on!

Barry de Rond