How do I make a simple scrolling text box in JavaScript

I have a project where we need to make a small box that vertically scrolls through a number of customer testimonials. I'm not sure scroll is the appropriate word, as I need basically one testimonial to be up, and after seven seconds, it switches to the next testimonial. I have an HTML document with all the testimonials written out and seperated by divs. I also don't want the testimonial text to be within my main html page, so I need to reference it from another HTML document.

So, is there a way to have these testimonials switch from one to the next based off divs? Thanks for the help, and I hope this was clearly communicated. This is my first posting.
VanStoneAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
numberkruncherConnect With a Mentor Commented:
How about something like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Demo Page</title>
		<style type="text/css">
			/* Use CSS to control visibility */
			div.testimonial {
				display: none;
			}
			div.testimonial.active {
				display: block;
			}
		</style>
		<script type="text/javascript">
			var activeTestimonial = 0, testimonialCount = 0;
			function onLoad() {
				/* Initialize count and timer. */
				updateTestimonail();
				var seconds = 5; // Every 5 seconds...
				setInterval(onTick, seconds * 1000);
			}
			function onTick() {
				/* Move to next testimonial and update. */
				++activeTestimonial;
				/* Wrap back to first one! */
				if (activeTestimonial >= testimonialCount)
					activeTestimonial = 0;
				updateTestimonail();
			}
			function updateTestimonail() {
				var el = document.getElementById('testimonials').firstChild;
				testimonialCount = 0;
				/* Count testimonials and add 'active' to class name. */
				while (el) {
					if (el.tagName == 'DIV') {
						if (testimonialCount == activeTestimonial)
							el.className = 'testimonial active';
						else
							el.className = 'testimonial';
						++testimonialCount;
					}
					el = el.nextSibling;
				}
			}
		</script>
	</head>
	<body onload="onLoad();">
		<h1>Demo Page</h1>
		<div id="testimonials">
			<h2>Testimonial</h2>
			<div class="testimonial">Great stuff!</div>
			<div class="testimonial">Excellent!</div>
			<div class="testimonial">Recommended!</div>
		</div>
	</body>
</html>

Open in new window

0
 
scrathcyboyCommented:
"So, is there a way to have these testimonials switch from one to the next based off divs?"

Sure, but you don't have to have the DIV code in the other file.  You could simply have a DIV structure in the main file, and then a simple javascript array of the various testimonials, separated by "Quote" and new line.  

Then the javascript just reads the next testimonial into the DIV layer (all you need is a name and ID for it) based on a timer.  That is how I would do it, and the text file for the testimonials would be a simple javascript array.  Can give an example if you want.
0
All Courses

From novice to tech pro — start learning today.