渐变

原创 2012年03月23日 13:30:22

<!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> new document </title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <center> <input type="button" name="fx" value="宽度渐变" id="wt-cg"/> <input type="button" name="fx" value="高度渐变" id="hi-cg"/> <input type="button" name="fx" value="透明度渐变" id="op-cg"/> <input type="button" name="fx" value="边框渐变" id="bd-cg"/> <input type="button" name="fx" value="综合渐变" id="zh-cg"/> <div id="demo" style="width:370px;height:100px;background-color:#00CC33;border:2px solid #990033; font-size:12px;"></div> </center> <script> function Animate(el, prop, opts) { this.el = el; this.prop = prop; this.from = opts.from; this.to = opts.to; this.time = opts.time; this.callback = opts.callback; this.animDiff = this.to - this.from; } Animate.prototype._setStyle = function(val) { switch(this.prop) { case 'opacity': this.el.style[this.prop] = val; this.el.style.filter = 'alpha(opacity=' + val * 100 + ')'; break; default: this.el.style[this.prop] = val + 'px'; break; } } Animate.prototype._animate = function() { var that = this; this.now = new Date(); this.diff = this.now - this.startTime; if (this.diff > this.time) { this._setStyle(this.to); if (this.callback) { this.callback.call(this); } clearInterval(this.timer); return; } this.percentage = (Math.floor((this.diff / this.time) * 100) / 100); this.val = (this.animDiff * this.percentage) + this.from; this._setStyle(this.val); } Animate.prototype.start = function() { var that = this; this.startTime = new Date(); clearInterval(this.timer); this.timer = setInterval(function() { that._animate.call(that); }, 4); } Animate.canTransition = function() { var el = document.createElement('foo'); el.style.cssText = '-webkit-transition: all .5s linear;'; return !!el.style.webkitTransitionProperty; }(); window.onload = function() { var T$ = function(id) { return document.getElementById(id); } var T$$ = function(n) { return document.getElementsByName(n); } var demo = T$('demo'); var btns = T$$('fx'); function disableButton() { for (var i = 0, len = btns.length; i < len; ++i) { btns[i].disabled = 'disabled'; } } function resetButton() { for (var i = 0, len = btns.length; i < len; ++i) { btns[i].disabled = ''; } } // 宽度渐变 function changeWidth() { var fx = 'width', from = demo.clientWidth, to = from - 270, time = 1000; var callback = function() { from = demo.clientWidth; to = from + 270; new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton }).start(); } new Animate(demo, fx, { from: from, to: to, time: time, callback: callback }).start(); } // 高度渐变 function changeHeight() { var fx = 'height', from = demo.clientHeight, to = from + 100, time = 1000; var callback = function() { from = demo.clientHeight; to = from - 100; new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton}).start(); } new Animate(demo, fx, { from: from, to: to, time: time, callback: callback }).start(); } // 透明度渐变 function changeOpacity() { var fx = 'opacity', from = 1, to = 0, time = 1000; var callback = function() { from = 0; to = 1; new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton}).start(); } new Animate(demo, fx, { from: from, to: to, time: time, callback: callback }).start(); } // 边框渐变 function changeBorderWidth() { var fx = 'borderWidth', from = 2, to = 10, time = 1000; var callback = function() { from = 10; to = 2; new Animate(demo, fx, { from: from, to: to, time: time, callback: resetButton }).start(); } new Animate(demo, fx, { from: from, to: to, time: time, callback: callback }).start(); } T$('wt-cg').onclick = function() { disableButton(); changeWidth(); } T$('hi-cg').onclick = function() { disableButton(); changeHeight(); } T$('op-cg').onclick = function() { disableButton(); changeOpacity(); } T$('bd-cg').onclick = function() { disableButton(); changeBorderWidth(); } T$('zh-cg').onclick = function() { disableButton(); changeWidth(); changeHeight(); changeOpacity(); } //if (Animate.canTransition) { //demo.style.webkitTransition = 'opacity 3s ease-in'; //demo.style.opacity = 0; //} else { //} } </script> </body> </html>

相关文章推荐

J.渐变效果 2D 3D

  • 2017年07月14日 10:01
  • 251KB
  • 下载

单片机七彩灯渐变程序

  • 2015年08月07日 11:21
  • 4KB
  • 下载

《unity shader入门精要》学习笔记--渐变纹理与遮罩纹理

渐变纹理 渐变纹理可用来控制漫反射光照效果。   // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld' Shader...

photoshop怎样使用渐变工具

  • 2015年12月03日 10:17
  • 138KB
  • 下载

51单片机控制LED七彩渐变灯

  • 2017年05月24日 16:58
  • 700KB
  • 下载

HTML5基础加强css样式篇(background-image线性渐变函数:linear-gradient)(四十)

1.linear-gradient基础: .box { margin: 200px 0 0 200px; ...

PS渐变工具

  • 2014年12月18日 12:40
  • 269KB
  • 下载

导航栏随滚动透明渐变

  • 2017年11月08日 11:23
  • 1.56MB
  • 下载

ArcGIS教程:河流渐变效果的实现

当我们在ArcMap中加载河流数据时,得到的效果往往如图1a所示,仅仅是表示河流位置的线要素,既无法真实地反映河流的实际情况,同时在出图的时候也远没有任何美化效果。   为了能够更好地向读图者提供信...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:渐变
举报原因:
原因补充:

(最多只允许输入30个字)