当前位置: 移动技术网 > IT编程>开发语言>JavaScript > [js样式效果]具有停顿效果上下滚动方式

[js样式效果]具有停顿效果上下滚动方式

2018年12月01日  | 移动技术网IT编程  | 我要评论
一般用于公告的滚动效果 ...

一般用于公告的滚动效果

<!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,speed){
                    this.element=$(element);
                    this.element.innerhtml+=this.element.innerhtml;
                    this.height=height;
                    this.delay=delay*1000;
                    this.speed=speed;
                    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),this.speed);
                    }else{
                        this.counter=0;
                        this.timer=settimeout(this.scroll.bind(this),this.delay);
                    }
                },
                stop:function(){
                    cleartimeout(this.timer);
                }
            }
            new scroll('a', 22,2.5,15)
        </script>
    </body>
</html>

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网