實現公告欄無縫滾動的js代碼(轉)


<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>       
        <style>
            ul {
                margin:100px;
                height:22px; 
                border:1px solid red;
                overflow:hidden;
            }
            li {
                height:22px; line-height:22px; font-size:12px;
            }
        </style>       
    </head>
    <body>
        <ul id="a">
            <li>1-1</li>
            <li>1-2</li>
            <li>1-3</li>
            <li>1-4</li>
        </ul>
        <script>
            //document.getElementById()的最簡化應用
            function $(element){
                if(arguments.length>1){
                    for(var i=0,length=arguments.length,elements=[];i<length;i++){
                        elements.push($(arguments[i]));
                    }
                    return elements;
                }
                if(typeof element=="string"){
                    return document.getElementById(element);
                }else{
                    return element;
                }
            }
            //類創建函數
            var Class={
                create:function(){
                    return function(){
                        this.initialize.apply(this,arguments);
                    }
                }
            }
            //對象屬性方法擴展
            Function.prototype.bind=function(object){
                var method=this;
                return function(){
                    method.apply(object,arguments);
                }
            }
            var Scroll=Class.create();
            Scroll.prototype={
                //第一個參數定義要滾動的區域,第二個參數定義每次滾動的高度
                initialize:function(element,height,delay){
                    this.element=$(element);
                    this.element.innerHTML+=this.element.innerHTML;
                    this.height=height;
                    this.delay=delay*1000;
                    this.maxHeight=this.element.scrollHeight/2;
                    this.counter=0;
                    this.scroll();
                    this.timer="";
                    this.element.onmouseover=this.stop.bind(this);
                    this.element.onmouseout=function(){this.timer=setTimeout(this.scroll.bind(this),1000);}.bind(this);
                },
                scroll:function(){
                    if(this.element.scrollTop<this.maxHeight){
                        this.element.scrollTop++;
                        this.counter++;
                    }else{
                        this.element.scrollTop=0;
                        this.counter=0;
                    }
                     
                    if(this.counter<this.height){
                        this.timer=setTimeout(this.scroll.bind(this),5);
                    }else{
                        this.counter=0;
                        this.timer=setTimeout(this.scroll.bind(this),this.delay);
                    }
                },
                stop:function(){
                    clearTimeout(this.timer);
                }
            }
            new Scroll('a', 22, 2)
        </script>
    </body>
</html>

 


免責聲明!

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



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