当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 颜色梯度和渐变效果

Javascript
jQuery中isFunction方法的BUG修复
将函数的实际参数转换成数组的方法
javascript 删除数组中重复项(uniq)
js 巧妙去除数组中的重复项
javascript下一种表单元素获取方法存在的问题
javascript 三种数组复制方法的性能对比
js 多层叠的TAB选项卡
javascript 自动标记来自搜索结果页的关键字
起点页面传值js,有空研究学习下
javascript 的Document属性和方法集合
JavaScript 使用简略语法创建对象的代码
使用JQuery进行跨域请求
jquery 经典动画菜单效果代码
jquery 常用操作方法
js提示信息jtip封装代码,可以是图片或文章
javascript面向对象的方式实现的弹出层效果代码
jquery中的sortable排序之后的保存状态的解决方法
js或css实现滚动广告的几种方案
使用JavaScript库还是自己写代码?
js 右键菜单,支持不同对象不同菜单(兼容IE、Firefox)

Javascript 中的 JavaScript 颜色梯度和渐变效果


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 33 ::
收藏到网摘: n/a

程序ColorGrads的作用是通过StartColor和EndColor生成颜色梯度集合。 程序说明
【ColorGrads颜色梯度】
程序ColorGrads的作用是通过StartColor和EndColor生成颜色梯度集合。
颜色都可以用红(r)、绿(g)、蓝(b)三个颜色来表示。
程序中先通过GetColor把一般的颜色表示形式转化成一个用红(r)、绿(g)、蓝(b)三个颜色值作元素的集合。
那就首先要知道有什么颜色表示形式,从w3c的Colors部分可以知道有以下形式:
关键词模式:
em { color: red }
RGB颜色模式:
em { color: #f00 }
em { color: #ff0000 }
em { color: rgb(255,0,0) }
em { color: rgb(100%, 0%, 0%) }
以上都是表示同一种颜色(红色)。
获取颜色属性的形式在ie和ff并不同,ff统一返回RGB颜色模式的第三种形式,ie则按照设置时的形式返回。
先说说RGB颜色模式,前两种比较常用应该都明白他们的区别吧,它用的是16进制表示形式,而我们想要10进制的。
把一个16进制表示的字符转成10进制数字,一般用parseInt,在substr截取字符之后就可以用parseInt转换。
对于#ff0000这个形式可以这样转换:
复制代码 代码如下:
【ColorTrans颜色渐变】
有了颜色梯度集合,只需要设个定时器把集合的值依次显示就是一个渐变效果了。
这个渐变一般是分两个步骤,分别是(FadeIn)和渐出(FadeOut)。
原理就是通过改变_index集合索引,渐入时逐渐变大,渐出时逐渐变小:
复制代码 代码如下:
ColorTrans部分:
复制代码 代码如下:

var ColorTrans = function(obj, options){
this._obj = $(obj);
this._timer = null;//定时器
this._index = 0;//索引
this._colors = [];//颜色集合
this._grads = new ColorGrads();
this.SetOptions(options);
this.Speed = Math.abs(this.options.Speed);
this.CssColor = this.options.CssColor;
this._startColor = this.options.StartColor || CurrentStyle(this._obj)[this.CssColor];
this._endColor = this.options.EndColor;
this._step = Math.abs(this.options.Step);
this.Reset();
this.SetColor();
};
ColorTrans.prototype = {
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
StartColor: "",//开始颜色
EndColor: "#000",//结束颜色
Step: 20,//渐变级数
Speed: 20,//渐变速度
CssColor: "color"//设置属性(Scripting属性)
};
Extend(this.options, options || {});
},
//重设颜色集合
Reset: function(color) {
//修改颜色后必须重新获取颜色集合
color = Extend({ StartColor: this._startColor, EndColor: this._endColor, Step: this._step }, color || {});
//设置属性
this._grads.StartColor = this._startColor = color.StartColor;
this._grads.EndColor = this._endColor = color.EndColor;
this._grads.Step = this._step = color.Step;
//获取颜色集合
this._colors = this._grads.Create();
this._index = 0;
},
//颜色渐入
FadeIn: function() {
this.Stop(); this._index++; this.SetColor();
if(this._index < this._colors.length - 1){
this._timer = setTimeout(Bind(this, this.FadeIn), this.Speed);
}
},
//颜色渐出
FadeOut: function() {
this.Stop(); this._index--; this.SetColor();
if(this._index > 0){
this._timer = setTimeout(Bind(this, this.FadeOut), this.Speed);
}
},
//颜色设置
SetColor: function() {
var color = this._colors[Math.min(Math.max(0, this._index), this._colors.length - 1)];
this._obj.style[this.CssColor] = "rgb(" + color[0] + "," + color[1] + "," + color[2] + ")";
},
//停止
Stop: function() {
clearTimeout(this._timer);
}
};

点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]