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

Javascript
动态改变图片尺寸(一)
JavaScript+PHP 应用一:网页制作中双下拉菜单的动态实现
JavaScript + PHP 应用二:网页设计中树形菜单的动态实现
在Javascript中为String对象添加trim,ltrim,rtrim方法
纯JavaScript时钟
网页之定时器详解
为网页添加活动的背景音乐
Javascript Game
实用的检测分辨率的程序代码
【推荐】一个非常漂亮的列表框
绝对精彩:在网页里做类似window右键的弹出式菜单
怎样使网页中的元素可编辑??
JavaScript和Java的区别
怎样编写IE和NN6通用的闪烁(blank)效果
关于如何动态地在同一页面实现两个 < select > 互传 (s1 <==> s2)
COOKIE欺骗
连串英文自动换行的方法
JavaScript中的正则表达式(1)
JavaScript中的正则表达式(2)
JavaScript窗口功能指南之定制新窗口

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 23 ::
收藏到网摘: 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 全选 提示:你可先修改部分代码,再按运行]