zoukankan      html  css  js  c++  java
  • 文字自动自左向右滚动的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();
  • 相关阅读:
    学习进度笔记06
    学习进度笔记05
    学习进度笔记04
    学习进度笔记03
    学习进度笔记02
    周总结13
    周总结12
    周总结11
    人月神话阅读笔记3
    第一阶段冲刺10
  • 原文地址:https://www.cnblogs.com/hao5599/p/4567825.html
Copyright © 2011-2022 走看看