[Javascript][Prototype]无间隔循环滚动图片(imgLoopView)

效率更好的模拟Marquee见这

--------------------------------------

最近写了一个无间隔循环滚动图片代码效果,这个东西的原理和原来用FLASH做的道理是一样的,将要滚动的元素克隆一个,然后交替这两个元素的次序就好。

类的构造函数包括两个参数,一个是DOM的ID,另一个是运动方向,默认向左。

同时提供了六个公有方法,分别是:运动(start),停止(stop),开关(toggle),向左(goLeft),向右(goRight),改变方向(toggleDir)。

演示地址|Demo:
http://www.kxbd.com/mylab/080709imgLoopView/

I wrote a Class to implement looping images, the theory is just as the same as I made it before with Flash, first to clone the element and then to change the order of these two elements.

The constructor of Class include two parameters, one is the ID of element, the other is the direction of move, default is set to be left.

I also supply six public methods, they are: move(start), stop(stop), toggle(toggle), move to left(goLeft), move to right(goRight), change direction(toggleDir).

程序代码 程序代码

/******************************
*                             *
*   Name:   imgLoopView       *
*   Author: Aken li           *
*   Date:   2008.07.10        *
*   Blog:   www.kxbd.com      *
*                             *
******************************/
var imgLoopView = Class.create();
imgLoopView.prototype = {
    initialize:function(id,direction){
        this.step = 1;
        this.interval = 0.01;
        this.gap = 10;//图片之间的间隔
        this.direction = direction || "Left";
        this.scrollId;
        this.oA = $(id);//遮罩
        this.o1;//滚动内容
        this.o2;//滚动内容克隆
        this.maskW = this.oA.getWidth();//遮罩的宽度,由style里的width获得
        this.scrollW = 0; //滚动元素的总宽度
        this.init();
    },

    init:function(){
        this.render();
        this.start();
        this.oA.observe("mouseover",this.stop.bind(this));
        this.oA.observe("mouseout",this.start.bind(this));
        this.oA.observe("mouseover",this.imgOver);
        this.oA.observe("mouseout",this.imgOut);
    },

    render:function(){
        this.oA.insert(new Element("ul"));
        this.o1 = this.oA.down();
        this.o2 = this.o1.next();

        this.o1.setStyle({
            left:"0px"
        });
        this.o1.childElements().each(function(o,i){
            var oW = o.getWidth();
            o.setStyle({
                position:"absolute",
                left:this.scrollW+"px"
            })
            this.scrollW += this.gap+oW;
        }.bind(this));
        this.o2.innerHTML = this.o1.innerHTML;
        this.o2.setStyle({
            left:this.scrollW+"px"
        });
    },

    start:function() {
        if(!this.scrollId){
            if (this.direction == "Left"){
                this.scrollId = new PeriodicalExecuter(this.moveLeft.bind(this,this.step), this.interval);
            }else{
                this.scrollId = new PeriodicalExecuter(this.moveRight.bind(this,this.step), this.interval);
            }
        }
    },

    stop:function() {
        if(this.scrollId){
            this.scrollId.stop();
            this.scrollId = 0;
        }
    },

    toggle:function() {
        this.scrollId?this.stop():this.start();
    },

    goLeft:function() {
        this.direction = "Left";
        this.stop();
        this.start();
    },

    goRight:function() {
        this.direction = "Right";
        this.stop();
        this.start();
    },

    toggleDir:function() {
        this.direction=="Left"?this.goRight():this.goLeft();
    },

    moveLeft:function(step) {
        var o1Left = parseInt(this.o1.getStyle("left"));
        var o2Left = parseInt(this.o2.getStyle("left"))
        if (-o1Left>this.scrollW)
        {
            this.o1.setStyle({
                left:o2Left+this.scrollW+"px"
            });
        }
        if (-o2Left>this.scrollW)
        {
            this.o2.setStyle({
                left:o1Left+this.scrollW+"px"
            });
        }
        
        this.o1.setStyle({
            left: parseInt(this.o1.getStyle("left"))-step+"px"
        });
        this.o2.setStyle({
            left: parseInt(this.o2.getStyle("left"))-step+"px"
        });
    },

    moveRight:function(step) {
        var o1Left = parseInt(this.o1.getStyle("left"));
        var o2Left = parseInt(this.o2.getStyle("left"))
        if (o1Left>this.scrollW)
        {
            this.o1.setStyle({
                left:o2Left-this.scrollW+"px"
            });
        }
        if (o2Left>this.scrollW)
        {
            this.o2.setStyle({
                left:o1Left-this.scrollW+"px"
            });
        }
        
        this.o1.setStyle({
            left: parseInt(this.o1.getStyle("left"))+step+"px"
        });
        this.o2.setStyle({
            left: parseInt(this.o2.getStyle("left"))+step+"px"
        });
    },

    imgOver:function(e) {
        var o = e.element();
        if (o.tagName == "IMG")
        {
            o.setStyle({ border:"1px solid #FFF" });
            new Effect.Opacity(o, { from: 0, to: 1, duration: 0.3 });
        }
    },

    imgOut:function(e) {
        var o = e.element();
        if (o.tagName == "IMG")
        {
            o.setStyle({ border:"1px solid #666" });

        }
    }
};



[本日志由 勤卓 于 2009-07-18 06:04 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Javascript Prototype
评论: 1 | 引用: 0 | 查看次数: 1807
回复回复zxgcqupt[2010-06-08 10:57 PM | del]
麻烦发给我一份 谢谢了 我的网站正好要用到这个效果!
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 关闭 | [img]标签 关闭