文字自動自左向右滾動的js代碼


重要的一點,就是scrollLeft一直在變化。對象一直在移動,參照物沒有動。

代碼:

css:

#div1{display:black;width:110px;height:50px;line-height:50px;white-space:nowrap;overflow:hidden;background-color:#a2a2a2;margin:15px;padding:5px 15px;}
span{display:inline-block;color:#fff;padding-right:20px;}

html:

<!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" xml:lang="en">
<head>
<meta charset="utf-8"/>
<title>mq</title>
 </head>
 <body>
    <div id="div1">
        <span id="span1">天天幣專享項</span><span id="span2"></span>
    </div><span id="span1">天天幣專享項目A</span>
 </body>
</html>

 

js:

var div,span,span2,i,timer;
        function init(){
            setInterval(ttb,50);
        }
        function ttb(){
            div = getId('div1');
            span = getId('span1');
            span2 = getId('span2');
            
            span2.innerHTML = span.innerHTML;
            if(span.offsetWidth <= div.scrollLeft){
                div.scrollLeft -= span.offsetWidth;
                
            }
            else {
                div.scrollLeft++;
            }
            
        }
        function getId(Id){
            return document.getElementById(Id);
        }
        window.onload = init();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM