Use Javascript to create continuous scrolling subtitles

Source: Internet
Author: User

We generally use the Marquee label to control the scrolling of elements. However, the one-way rolling of Marquee is not continuous. After each rolling scene is completed, a blank space will appear. The scrolling in the following section is continuous and uninterrupted.

The following section describes how this is implemented.

To achieve "continuous" scrolling, We need to copy the subtitle content multiple times until the content height is no less than twice the height of the scroll area. Then we hide the overflow scroll bar and use the code to control the scroll bar to move down (the content will move up at this time ). When the scroll bar is at the bottom, it cannot be rolled down theoretically, So we immediately adjust the scroll bar and Scroll it up to the same position as the current screen. As a result, we can see continuous scrolling. Well, it's that simple. How can this problem be solved? Let's take a look at how it is implemented gradually.

<Div id = "marquees"> <! -- These are subtitle content, which you can define at will --> <a href = "#"> link 1 </a>
<Br> <a href = "#"> link 2 </a>
<Br> <a href = "#"> link 3 </a>
<Br> <a href = "#"> Link 4 </a>
<Br> <! -- Subtitle content ends -->
</Div>
<! -- The following is the java-script code -->
<Script language = "java-script">
<! --
MarqueesHeight = 200; // content area height
Stopscroll = false; // This variable controls whether to stop scrolling.
With (marquees ){
NoWrap = true; // The content area of this table does not automatically wrap.
Style. width = 0; // we can set its width to 0, because it will be extended.
Style. height = marqueesHeight;
Style. overflowY = "hidden"; // the scroll bar is invisible.
Onmouseover = new Function ("stopscroll = true"); // move the mouse over to stop scrolling.
Onmouseout = new Function ("stopscroll = false"); // move the mouse away to start rolling.
}
// At this time, the height of the content area cannot be read. The following output is an invisible layer "templayer", and the content will be copied to it later:
Document. write ('<div id = "templayer"
Style = "position: absolute; z-index: 1; visibility: hidden"> </div> ');
Function init () {// initialize the scrolling content
// Copy the original content multiple times to "templayer" until the height of "templayer" is greater than that of the content area:
While (templayer. offsetHeight <marqueesHeight ){
Templayer. innerHTML + = marquees. innerHTML;
} // Copy twice the content of "templayer" to the original content area:
Marquees. innerHTML = templayer. innerHTML + templayer. innerHTML;
// Set the continuous timeout. Call the "scrollUp ()" function to drive the scroll bar:
SetInterval ("scrollUp ()", 10 );
}
Document. body. onload = init;
PreTop = 0; // This variable is used to determine whether the scroll bar has reached its end.
Function scrollUp () {// The driver function of the scroll bar
If (stopscroll = true) return; // if the variable "stopscroll" is true, stop scrolling.
PreTop = marquees. scrollTop; // record the position of the scroll bar before scrolling
Marquees. scrollTop + = 1; // the scroll bar moves down a pixel.
// If the scroll bar does not move, it will scroll up to the same position as the current screen.
// Of course not only that, but also to scroll down a pixel (+ 1 ):
If (preTop = marquees. scrollTop ){
Marquees. scrollTop = templayer. offsetHeight-marqueesHeight + 1;
}
}
-->
</Script>

This is done, and it is not difficult to do it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.