js跑马灯

<!DOCTYPE html>


<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        #msg {
            position:absolute;
            width:100px;
            background-color:blue;
            color:white;
            border:1px solid red;
        }
    </style>
    <script>
        var pos =0;
        var direction=1;
        onload = function () {
            setInterval(run, 100);
        }


        function run() {
            if (pos+100>=innerWidth) {
                direction=-1;
            }
            if (pos<=0) {
                direction = 1;
            }
            pos+=10*direction;

            document.getElementById('msg').style.left = pos + "px";
        }
    </script>
</head>
<body>
    <div id="msg">
        跑马灯文本
    </div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/80248409