Google动画脚本收藏

Google动画脚本收藏

 

网上搜索的,呵呵!!

 

/*
author      :   DK
email       :   derek.qin@yomeier.com
date        :   2011.10.12
description :   仿google首页动画
*/
/*
options参数说明:
element:string,动画容器的id
url:string,用于播放动画的图片地址
speed:int,每秒帧数
hover:function,鼠标移动到element上时调用
blur:function,鼠标离开element时调用
*/
function animation(options) {
    var defaultOptions = { element: "", url: "", speed: 11, hover: function () { }, blur: function () { } };
    this.init(options, defaultOptions);
};

animation.prototype = {
    init: function (o1, o2) {
        var me = this;
        for (var p in o2) {
            me[p] = o1[p] || o2[p];
        }
        me.element = document.getElementById(me.element);
        me.delay = 1000 / me.speed;
        me.step = 0;
        me.defaultBackground = me.element.style.background;
        me.defalutUrl = me.element.style.backgroundImage;
        me.url = me.url || me.defalutUrl;
        me.width = me.element.clientWidth;
        me.height = me.element.clientHeight;
        var img = new Image();
        img.onload = function () {
            me.cols = parseInt(img.width / me.width);
            me.rows = parseInt(img.height / me.height);
            me.frames = me.cols * me.rows;
            me.element.onclick = function () {
                me.play();
            };
            me.element.onmouseover = function () {
                me.onhover();
            };
            me.element.onmouseout = function () {
                me.onblur();
            };
        };
        img.src = me.url;
    }

, play: function () {
    if (this.playing) {
        return;
    }
    this.playing = true;
    this.next();
},
    onhover: function () {
        if (!this.playing) {
            this.hover();
        }
    },
    onblur: function () {
        if (!this.playing) {
            this.blur();
        }
    },
    next: function () {
        var me = this;
        me.step++;
        if (me.step > me.frames) {
            me.stop();
            return;
        }
        var x = me.step % me.cols;
        var y = parseInt(me.step / me.rows);
        if (me.step % me.rows > 0) {
            y++;
        }
        var position = "-" + (x - 1) * me.width + "px -" + (y - 1) * me.height + "px";
        me.element.style.background = "url(" + me.url + ") " + position;
        setTimeout(function () { me.next(); }, me.delay);
    },
    stop: function () {
        var me = this;
        me.step = 0;
        me.playing = false;
        me.element.style.background = me.defaultBackground;
    } 
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值