Native js achieves seamless Horizontal Rolling and js horizontal seamless

Source: Internet
Author: User

Native js achieves seamless Horizontal Rolling and js horizontal seamless

Seamless horizontal scrolling

Supports image and text scrolling.
Principle:A large box contains two boxes, which are implemented by setting the offsetWidth and scrollLeft relationships. The timer function setInterval is also used. When the finger moves up, the scroll is defined and the scrolling continues. Compatible with various browsers.
HTML code

<Div id = "demo"> <div id = "demoin"> <div id = "demo1"> <a href = ""> test text 1 </a> 

CSS code

# Demo {width: 1000px; height: 30px; overflow: hidden; line-height: 30px; font-size: 13px; font-family: ' '; background: # ddd url (images/notice.png) no-repeat 25px center; color: # 0C77CF; font-weight: bold; margin: 0 auto ;}# demoin {width: 900px; height: 30px; margin: 0 auto; white-space: nowrap; overflow: hidden;} # demo # demo1, # demo # demo2 {display: inline}

Javascript code

 window.onload = function(){    scrollLeft();  };  function scrollLeft(){    var speed = 20;    var tab = document.getElementById('demoin');    var tab1 = document.getElementById('demo1');    var tab2 = document.getElementById('demo2');    tab2.innerHTML = tab1.innerHTML;    function Marquee(){      if(tab2.offsetWidth - tab.scrollLeft <=0) {            tab.scrollLeft = 0;      }else{        tab.scrollLeft ++;      }    }    var timer = setInterval(Marquee,speed);    tab.onmouseover = function(){      clearInterval(timer);    };    tab.onmouseout = function(){      timer = setInterval(Marquee,speed);    }  }

:

The effect is always rolling.

Note: The content width must be greater than the container width, otherwise it cannot be rolled. The test text is generally obtained using the ajax background.

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.