How to Implement continuous subtitle scrolling using JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use JavaScript to achieve the effect of continuous scrolling subtitles. It provides a sample script running on the browser, for more information, see. We generally use the Marquee tag 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 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.

<P id = "marquees"> <! -- These are subtitle content, you can define anything --> <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 --> </p> <! -- 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 style is not automatically wrapped in the content area of this table. width = 0; // we can set its width to 0 because it will support a large style. height = marqueesHeight; style. overflowY = "hidden"; // The onmouseover = new Function ("stopscroll = true") is invisible to the scroll bar; // move the cursor over, stop rolling onmouseout = new Function ("stopscroll = false"); // move the mouse away and 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 ('<p id = "templayer" style = "position: absolute; z-index: 1; visibility: hidden"> </p>'); function init () {// initialize the scrolling content // copy the original content to "templayer" multiple times until the height of "templayer" is greater than the height of the content area: while (templayer. offsetHeight <marqueesHeight) {templayer. innerHTML + = marquees. innerHTML;} // copy twice the content of "templayer" back to the original content area: marquees. innerHTML = templayer. innerHTML + templayer. innerHTML; // set the continuous timeout. Call "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 the end of function scrollUp () {// if (stopscroll = true) return; // If the variable "stopscroll" is true, stop rolling preTop = marquees. scrollTop; // record the position of the scroll bar before rolling marquees. scrollTop + = 1; // the scroll bar moves one pixel down. // If the scroll bar does not move, it will scroll up to the same position as the current screen. // Of course not only that, you also need 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.