JS实现的颜色实时渐变效果完整实例

 更新时间:2016年03月25日 11:48:28   作者:xiao张  
这篇文章主要介绍了JS实现的颜色实时渐变效果,结合实例形式分析了JavaScript结合时间函数定时触发动态改变页面元素属性的相关技巧,需要的朋友可以参考下

本文实例讲述了JS实现的颜色实时渐变效果。分享给大家供大家参考,具体如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
</head>
<body>
<div id="div1" style="font-size:36px;">我的闪烁文字 abc123</div>
<span id="span1"></span>
<script type="text/javascript">
var begin = getRGB('#33FFAA');
var end = getRGB('#FF0000');
var curColor = getRGB('#33FFAA');
var bo = true;
var rate = getRate(begin, end);
function blink()
{
 window.setInterval(function(){
  curColor.r = getCur(begin.r, end.r, curColor.r, bo, rate.r);
  curColor.g = getCur(begin.g, end.g, curColor.g, bo, rate.g);
  curColor.b = getCur(begin.b, end.b, curColor.b, bo, rate.b);
  document.getElementById('div1').style.color = getColor(curColor);
  document.getElementById('span1').innerHTML = getColor(curColor);
  if(curColor.r == begin.r && curColor.g == begin.g && curColor.b == begin.b)
  {
   bo = true;
  }
  if(curColor.r == end.r && curColor.g == end.g && curColor.b == end.b)
  {
   bo = false;
  }
 } , 100);
}
function getCur(beginValue, endValue, curValue, bo, rateValue)
{
 if(beginValue == endValue)
 {
  return beginValue;
 }
 rateValue = beginValue < endValue ? rateValue : -rateValue;
 curValue += bo ? rateValue : -rateValue;
 if(curValue < Math.min(beginValue, endValue))
 {
  curValue = Math.min(beginValue, endValue);
 }
 if(curValue > Math.max(beginValue, endValue))
 {
  curValue = Math.max(beginValue, endValue);
 }
 return curValue;
}
function getRate(b, e)
{
 var obj = new Object();
 obj.r = Math.abs(b.r - e.r) / 5;
 obj.g = Math.abs(b.g - e.g) / 5;
 obj.b = Math.abs(b.b - e.b) / 5;
 return obj;
}
function getRGB(color)
{
 var obj = new Object();
 obj.r = parseInt(color.substr(1,2), 16);
 obj.g = parseInt(color.substr(3,2), 16);
 obj.b = parseInt(color.substr(5,2), 16);
 return obj;
}
function getColor(obj)
{
 obj.r = Math.round(obj.r);
 obj.g = Math.round(obj.g);
 obj.b = Math.round(obj.b);
 var color = '#';
 color += (obj.r < 16 ? '0':'') + obj.r.toString(16);
 color += (obj.g < 16 ? '0':'') + obj.g.toString(16);
 color += (obj.b < 16 ? '0':'') + obj.b.toString(16);
 return color;
}
blink();
</script>
</body>
</html>

PS:这里再为大家推荐几款本站的相关在线工具:

在线RGB、HEX颜色代码生成器:
http://tools.jb51.net/color/rgb_color_generator

RGB颜色查询对照表_颜色代码表_颜色的英文名称大全:
http://tools.jb51.net/color/jPicker

在线网页调色板工具:
http://tools.jb51.net/color/color_picker

在线颜色选择器工具/RGB颜色查询对照表:
http://tools.jb51.net/color/colorpicker

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结

希望本文所述对大家JavaScript程序设计有所帮助。

相关文章

  • 关闭ie窗口清除Session的解决方法

    关闭ie窗口清除Session的解决方法

    本篇文章主要是对关闭ie窗口清除Session的解决方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • 关于IE中getElementsByClassName不能用的问题解决方法

    关于IE中getElementsByClassName不能用的问题解决方法

    今天使用getElementsByClassName写了段小程序,在IE6、8中测试的时候就出现问题了,浏览器报错,经搜索找到了一个比较好的方法,是个老外写的一个方法
    2013-08-08
  • JavaScript每天必学之事件

    JavaScript每天必学之事件

    JavaScript每天必学之事件,聊一聊事件冒泡与捕获、事件冒泡的好处,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • JS实现表单中点击小眼睛显示隐藏密码框中的密码

    JS实现表单中点击小眼睛显示隐藏密码框中的密码

    这篇文章主要介绍了JS实现表单中点击小眼睛显示隐藏密码框中的密码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下
    2020-04-04
  • form表单转Json提交的方法(推荐)

    form表单转Json提交的方法(推荐)

    下面小编就为大家带来一篇form表单转Json提交的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Bootstrap的图片轮播示例代码

    Bootstrap的图片轮播示例代码

    Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的。本文给大家分享Bootstrap的图片轮播示例代码,小伙伴们快来围观吧。
    2015-08-08
  • alert和confirm功能介绍

    alert和confirm功能介绍

    这篇文章主要为大家介绍了alert和confirm的功能,需要的朋友可以参考下
    2014-05-05
  • window.location和document.location的区别分析

    window.location和document.location的区别分析

    用户不能改变document.location(因为这是当前显示文档的位置)。但是,可以改变window.location (用其它文档取代当前文档)window.location本身也是一个对象,而document.location不是对象
    2008-12-12
  • 详解JS中统计函数执行次数与执行时间

    详解JS中统计函数执行次数与执行时间

    这篇文章给大家分享了JS中统计函数执行次数与执行时间的相关知识点内容,有兴趣的朋友们分享下。
    2018-09-09
  • 基于js中style.width与offsetWidth的区别(详解)

    基于js中style.width与offsetWidth的区别(详解)

    下面小编就为就大家带来一篇基于js中style.width与offsetWidth的区别(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论