Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML Applications
- Creating a Dynamic Site Map
- Creating Drop-Down Menus
- Workshop: Creating a Scrolling Text Box
- Summary
- Q&A
- Quiz
Exercises
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Workshop: Creating a Scrolling Text Box
In Hour 5, "Using Strings and Arrays," you created a simple scrolling message. In Hour 20, "Using Advanced DOM Features," you created a scrolling message using DHTML.
In this section, you'll create a different kind of scrolling message. This one scrolls a large block of text vertically within a window, similar to the credits at the end of a movie. This type of scrolling message is easier to read, and can include links or other HTML features.
Creating the JavaScript Function
This example uses a simple JavaScript function. The following JavaScript code defines the Scroll function:
var pos=100;
function Scroll() {
if (!document.getElementById) return;
obj=document.getElementById("thetext");
pos -=1;
if (pos < 0-obj.offsetHeight+130) return;
obj.style.top=pos;
window.setTimeout("Scroll()",30);
}
The first line defines a variable, pos, to store the current scroll position. Next, this function subtracts 1 from pos and checks its value. If the scrolling has reached the end, the function exits; otherwise, it sets the object position and calls the Scroll function again using a timeout.
Creating the HTML Document
To complete the scrolling message, you can create an HTML document that defines the appropriate layers and includes the Scroll function. Listing 23.4 shows the complete HTML document.
Example 23.4. The HTML Document for the Scrolling Text Box
<html>
<head>
<title>A DHTML Scrolling Window</title>
<script language="JavaScript" type="text/javascript">
var pos=100;
function Scroll() {
if (!document.getElementById) return;
obj=document.getElementById("thetext");
pos -=1;
if (pos < 0-obj.offsetHeight+130) return;
obj.style.top=pos;
window.setTimeout("Scroll()",30);
}
</script>
</head>
<body onLoad="Scroll()">
<h1>Scrolling Window Example</h1>
<p>This example shows a scrolling window created in DHTML. The window
is actually a layer that shows a portion of a larger layer.</p>
<div id="thewindow" style="position:relative;width:180;height:150;
overflow:hidden; border-width:2px; border-style:solid; border-color:red">
<div id="thetext" style="position:absolute;width:170;left:5;top:100">
<p>This is the first paragraph of the scrolling message. The message
is created with ordinary HTML.</p>
<p>Entries within the scrolling area can use any HTML tags. They can
contain <a href="http://www.jsworkshop.com/">Links</a>.</p>
<p>There's no limit on the number of paragraphs that you can include
here. They don't even need to be formatted as paragraphs.</p>
<ul><li>For example, you could format items using a bulleted list.</li></ul>
<p>The scrolling ends when the last part of the scrolling text
is on the screen. You've reached the end.</p>
<p><b>[<a href="javascript:pos=100;Scroll()">Start Over</a>]</b></p>
</div>
</div>
</body>
</html>
The <div> tags in this document create two nested layers: One, thewindow, will form the small window for text to display in. The other, thetext, contains the text to scroll.
Since the text doesn't all fit in the small window, you'll only see part of it at a time. The overflow property on the window layer prevents the rest of the content from showing. Your script will manipulate the text window's style.top property to move it relative to the window, creating a scrolling effect.
The actual text to scroll is placed within the inner <div> element. You can use any HTML here, although it should be able to wrap to the small window.
Figure 23.3 shows this example in action, after the scrolling text has reached the end.
Figure 23.3 The scrolling text box in action.
Summary | Next Section

Account Sign In
View your cart