当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 用JavaScript实现动画效果

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 用JavaScript实现动画效果


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

  大家在使用Dreamweaver中的时间线功能或以做出很有趣的动画效果,Dreamweaver会自动为用户生成特定的程序代码,大家有没有想过动画的实现原理呢?其实原理是很简单的,主要是使用了一个计时器函数,下面我为大家

演示一个简单的动画的制作过程,通过有关的介绍,大家可以举一反三,做出更多很炫的动画效果。

  这个实例的效果是点击网页上的“开始移动”按钮,则其中的指定图层就会从左到右移动,在这个过程中你点击“停止移动”按钮就会停止移动。

  <html>
  <head>
  <title>JavaScript Motion Sample</title>

  <script language="JavaScript">
  var movingID = null;
  var scrolling = false;

  function startMove()
  {
   var left = eval(div1.style.left.replace("px", ""));
   if (left < document.body.scrollWidth - 150)
    div1.style.left = left + 1;
   else
    div1.style.left = 10;
   movingID = setTimeout("startMove()", 10);
  }

  function stopMove()
  {
   clearTimeout(movingID);
  }
  </script>




  </head>
  <body>
  <div id="div1" style="visibility:visible; position:absolute; left:10; top:10; z-index:1;">
   <table bgColor="#FFFFCC" border="1"cellPadding="0" cellSpacing="0">
    <tr>
     <td>I can moving...</td>
    </tr>
   </table>
  </div>

  <br><br>
  <input type="button" value="开始移动" onClick="startMove()">
  <input type="button" value="停止移动" onClick="stopMove()">

   </body>
  </html>
 

  这里主要使用了一个叫setTimeout(function, interval)函数,这个函数的参数格式为:
  第一个参数“function”为超时后调用的函数名,第二个参数“interval”为超时值,以微秒为单位。

  注意一点是如果要停止这个计时器,必须保存调用这个setTimeout()函数后的返回值,通过clearTimeout(id)函数来取消计时器。