20 lines of code let Your webpage content scroll freely!

Source: Internet
Author: User
Remember last year, csdn was named the simplest seamless rolling. Program , Only five lines are required. Code The post has aroused a heated discussion!

At that time, I tried to use this principle to implement smooth scrolling. In csdn, I published an article titled modifying the five lines of code in the current version to implement endless scrolling Code. There were about 10 lines of code to implement smooth scrolling! But the Code does not solve the problem of smoothness. The reason is that when the scolltop attribute is used, there is always a refresh problem, which causes flickering, And the Row Height setting cannot be well controlled.

Now we use the margin attribute to implement scrolling. 20 lines of javascript can be used to complete all the designed Scrolling on the page !!

Demo: attachment/1181020134_0.html

The Code is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> seamless scrolling </title>
</Head>

<Body>
<Style> A {display: block; font-size: 15px };</style>
<Div id = "div1" style = "width: 300px; Height: 68px; overflow: hidden">
<Div id = "div2" style = "margin-top: 0px; line-Height: 20px;">
<A href = "javascript:"> 1. You have had countless dreams. </a>
<A href = "javascript:"> 2, but it is lost in the passage of time </a>
<A href = "javascript:"> 3. Have you ever looked forward to the future? </a>
<A href = "javascript:"> 4, but lost in the years of growth </a>
<A href = "javascript:"> 5. csdn China programmer Forum </a>
<A href = "javascript:"> 6. Join us </a>
<A href = "javascript:"> 7. It looks good. </a>
</Div>
</Div>
<SCRIPT>
Function scrolln (ID, samont, step ){
VaR d = Document. getelementbyid (ID );
If (! D. scrolln ){
If (STEP) {d. Step = step; D. samont = samont ;}
D. scrolln = setinterval ("scrolln ('" + ID + "')", D. samont );
D. onmouseover = function () {clearinterval (this. scrolln); this. scrolln = NULL ;}
D. onmouseout = function () {scrolln (this. ID )}
}
VaR Top = parseint (D. style. margintop );
VaR lineheight = parseint (D. style. lineheight );
If (top>-lineheight ){
D. style. margintop = (top-d.step) + "PX ";}
Else {
Do {
VaR o = D. firstchild;
D. removechild (O );
D. appendchild (O );
} While (! D. firstchild. tagname)
D. style. margintop = "0px ";
}
}
// The first parameter is the tag ID of the Rolling Block, the second parameter is the rolling interval, and the third parameter is the rolling distance PX.
Scrolln ("div2", 100,2 );
</SCRIPT>
</Body>
</Html>

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.