封装一个方法,实现元素A(width:100px,height:100px)从页面位置一(top:50px,left:100px)移动到页面位置二(top:500px,left:400px),并且有3种移动效果可以根据参数进行切换,3种效果分别是:匀速,加速,减速。要求采用HTML+css+js实现,注重代码的精简和执行效率,有清楚的代码注释,不能采用第三方框架实现(如:jquery,YUI等)。

解决方案 »

  1.   

    http://www.jslab.org.cn/?tag=JSTweener
      

  2.   

    谢谢sohighthesky提供的Sample,不过能否提供对角线的移动,而不是直角移动的案例呢?万分感谢!
      

  3.   


    你给定两点的位置,JSTweener算法会自动算的。
      

  4.   

    一个加速的.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html> 
       <head>
          <title>移动</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
          <link rel="stylesheet" href="css.css" type="text/css" media="screen" />
          <script type="text/javascript">
             window.onload=function(){
                document.getElementById("mov").style.top="50px";
                document.getElementById("mov").style.left="100px";
             }
             var t;
             function move(){  //(100,50)-->(400,500)--> 300/450
                var mov=document.getElementById("mov");
                var speed=7;
                if(parseInt(mov.style.top)<500){
                   mov.style.top=parseInt(mov.style.top)+3+"px";
                   mov.style.left=parseInt(mov.style.left)+2+"px";
                   t=setInterval("move()",100);
                }else{
                   clearInterval(t);
                }
             }
          </script>
       </head>
       <body>
          <div id="mov" style="width:100px;height:100px;background-color:green;z-index:100;position:absolute;"></div>
          <div id="mov" style="width:400px;height:550px;background-color:yellow;top:50px;left:100px;position:absolute;z-index:99"></div>
          <input type="button" onclick="move()" value="move"/>
       </body>
    </html>
      

  5.   

    谢谢你的回答,这个我试过了,在Google浏览器和Firefox浏览器都没问题,但是在IE8上就无法通过。
    修改代码为:
    [code]      function move1() {
            JSTweener.addTween(square.style, {
                time: getTime(),
                transition: getTranstion(),
                onComplete: move2,
                top: 300
                left: 400
            });
          }
          function move2() {
            JSTweener.addTween(square.style, {
                time: getTime(),
                transition: getTranstion(),
                onComplete: move3,
                left: 400
                top: 300
            });
          }
          function move3() {
            JSTweener.addTween(square.style, {
                time: getTime(),
                transition: getTranstion(),
                onComplete: move4,
                top: 100
                left: 100
            });
          }
          function move4() {
            JSTweener.addTween(square.style, {
                time: getTime(),
                transition: getTranstion(),
                onComplete: move1,
                left: 100
                top: 100
            });
          }
    [/code]
      

  6.   

    谢谢archko的支持,用Interval的时间参数设定可以实现加速和匀速,但是减速做不到网上有平移的加减速,但是一旦加上Y坐标就不能实现功能