JS Tween 颜色渐变

 更新时间:2008年12月30日 15:16:52   作者:  
从我写的as tween改写的,基本功能跟as里面写的一样,只是没有扩展特定功能的接口(比如alpha2,move2,size2,color2等接口,这些在as tween里面都有实现)。
有31中缓动算法,实现了颜色的自动转换(#f00 #ff0000 rgb(255,0,0)格式到颜色运算格式,最后返回#ff0000格式)、px单位的自动转换。
调用接口:
/**
* 对外接口
* Tween的示例
* @param startProps 开始属性,单个属性或者数组
* @param endProps 结束属性,单个属性或者数组
* @param timeSeconds 运动消耗时间,单位秒
* @param animType 动作类型,字符串型,内部自己转换算子
* @param delay 延迟时间,多长时间后开始运动,单位秒
*/
window.rtween = function(startProps, endProps, timeSeconds, animType, delay)
{
var tw = new Tween();
tw.start(startProps, endProps, timeSeconds, animType, delay);
return tw;
}
示例如下:
http://img.jb51.net/online/Tween.htm

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

选择列表里面的缓动算法,点前面的按钮,就会以想对的缓动算法运动

源代码: http://img.jb51.net/jslib/jquery/rtween.js
核心代码:
function Tween()
{
this._frame=20;
//
this._animType = linear;
this._delay = 0;
//
this.run = function(){}
this.complete = function(){}
}
//
Tween.prototype.getValue = function(prop)
{
this._valueType = ”;
if(prop.constructor == Array) return prop;
//
if(typeof(prop) == 'string')
{
if(isColor(prop))
{
this._valueType = ‘color';
return c2a(prop);
}
if(prop.split('px').length>1)
{
this._valueType = ‘px';
return [prop.split('px')[0]];
}
}
return [prop];
}
Tween.prototype.setValue = function(prop)
{
if(this._valueType == ‘color')return a2c(prop);
if(this._valueType == ‘px')return prop[0]+'px';
return prop;
}
Tween.prototype.start = function(startProps, endProps, timeSeconds, animType, delay)
{
if(animType != undefined)this._animType = this.animTypes[animType];
if(delay != undefined)this._delay = delay;
//
this._timeSeconds = timeSeconds;
this._startTimer = new Date().getTime() + this._delay * 1000;
//
this._endProps = this.getValue(endProps);
this._startProps = this.getValue(startProps);
this._currentProps = [];
//
var $this = this;
clearInterval(this._runID);
this._runID = setInterval(
function(){$this._run();}
,this._frame);
}
Tween.prototype.stop = function(state)
{
for(var i in this._startProps)
{
if(Number(state)>0)
this._currentProps[i] = this._endProps[i];
else if(Number(state)<0)
this._currentProps[i] = this._startProps[i];
}
this.callListener();
this.complete();
//
clearInterval(this._runID);
}
Tween.prototype.callListener = function()
{
this.run(this.setValue(this._currentProps));
}
Tween.prototype._run = function()
{
if ( new Date().getTime()- this._startTimer< 0) return;
var isEnd = false;
//
for(var i in this._startProps)
{
this._currentProps[i] = this._animType( new Date().getTime()-this._startTimer,Number(this._startProps[i]),Number(this._endProps[i])-Number(this._startProps[i]),this._timeSeconds * 1000);
//
if(this._startTimer + (this._timeSeconds * 1000) <= new Date().getTime())
{
this._currentProps[i] = this._endProps[i];
isEnd = true;
}
}
//
if(isEnd)this.stop();
else this.callListener();
}

相关文章

  • JavaScript面试技巧之数组的一些不low操作

    JavaScript面试技巧之数组的一些不low操作

    这篇文章主要给大家介绍了关于JavaScript面试技巧之数组的一些不low操作的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用js具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • JS 类型转换常见方法小结

    JS 类型转换常见方法小结

    JS 类型转换常见方法小结,需要的朋友可以参考下。
    2010-05-05
  • js保留两位小数方法总结

    js保留两位小数方法总结

    本篇文章给大家总结了js保留两位小数的各种方法以及每种方法的实例代码分析,如果大家对此有需要,一起来学习下js保留两位小数的方法吧。
    2018-01-01
  • bootstrap table表格插件之服务器端分页实例代码

    bootstrap table表格插件之服务器端分页实例代码

    Bootstrap Table是基于Bootstrap的轻量级表格插件,只需要简单的配置就可以实现强大的支持固定表头、单复选、排序、分页、搜索以及自定义表头等功能。这篇文章主要介绍了bootstrap table表格插件之服务器端分页实例代码,需要的朋友可以参考下
    2018-09-09
  • Bootstrap中表单控件状态(验证状态)

    Bootstrap中表单控件状态(验证状态)

    这篇文章主要介绍了Bootstrap中表单控件状态(验证状态) 的相关资料,还给大家介绍了在Bootstrap框架中提供的机制验证效果,非常不错,需要的朋友可以参考下
    2016-08-08
  • js实现多选项切换导航菜单的方法

    js实现多选项切换导航菜单的方法

    这篇文章主要介绍了js实现多选项切换导航菜单的方法,可实现动态生成多选项切换导航菜单的功能,是非常实用的技巧,需要的朋友可以参考下
    2015-02-02
  • JS限制Textarea文本域字符个数的具体实现

    JS限制Textarea文本域字符个数的具体实现

    这篇文章介绍了JS限制Textarea文本域字符个数的具体实现,有需要的朋友可以参考一下
    2013-08-08
  • 浅谈Javascript事件处理程序的几种方式

    浅谈Javascript事件处理程序的几种方式

    事件就是用户或浏览器自身执行的某种动作。比如说click,mouseover,都是事件的名字。而相应某个事件的函数就叫事件处理程序(或事件侦听器)。为事件指定处理程序的方式有好几种
    2012-06-06
  • input点击后placeholder中的提示消息消失

    input点击后placeholder中的提示消息消失

    placeholder属性是HTML5 中为input添加的。在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示
    2016-01-01
  • JS如何为promise增加abort功能

    JS如何为promise增加abort功能

    这篇文章主要介绍了JS为promise增加abort功能,想了解JS异步的同学,可以参考下
    2021-04-04

最新评论