2013-02-25 68 views
3

我知道我在问一个非常常见的问题,但我想知道是否有另一种方式通过仅使用jQuery animateelement?我创建了一个动画,但我想改进它,但我不知道该怎么做。 Please check this fiddle to see the animation或者你可以在这里看到:walkoverapps如何通过仅使用jquery动画元素底部到顶部

我只是有点困惑。是否有其他方法来动画元素?

+5

动画是很好的工作,你想要什么改进? JQuery'动画'是由专家设计的,你想用另一种方式来制作你的火箭呢? – sdespont 2013-02-27 13:28:39

+2

“改进”是什么意思?你的动画是有效的!你想添加更多的动画?否则,这是[如果它没有被破坏,不修复它]的情况(http://en.wikipedia.org/wiki/Wikipedia:If_it_ain't_broke,_don't_fix_it)。 – Dom 2013-02-27 17:33:24

+0

你是否试图在没有jQuery UI动画功能的帮助下使用jQuery? – 2013-02-27 17:34:37

回答

11

我再次猜测你想获得该动画中每个步骤的坐标。 jQuery的动画功能提供了step:function (number, tween)这是在动画的每一步调用。有关详细信息,http://jsfiddle.net/nmGRt/5/

$('#rocket').delay(2000).animate({ 
    bottom: '600px' 
}, { 
    duration: 5000, 
    complete: function() {}, 
    step: function (n, t) { 
     var pos = $(this).position(); 
     $('#curVal') 
      .text('X: ' + pos.left.toFixed(2) + ' Y: ' + pos.top.toFixed(2)) 
      .css({ 
      left: pos.left - 150, 
      top: pos.top 
     }); 
    } 
}); 

我猜你是没有jQuery UI尝试此观看演示。如果您只针对现代浏览器,那么请在下面使用CSS3进行尝试。见Browser Compatibility表。

JS:

setTimeout(function() { 
    $('#rocket').addClass('fire').css('bottom', 600); 
}, 2000); 

CSS3:

.fire {  
    -moz-transition: bottom 5s; 
    -webkit-transition: bottom 5s; 
    -ms-transition: bottom 5s; 
    -o-transition: bottom 5s; 
    transition: bottom 5s; 
} 

DEMO:http://jsfiddle.net/nmGRt/2/

此外,您可以使用一些很酷的功能cubic-bezier控制动画。观看演示http://jsfiddle.net/nmGRt/3/embedded/result/

.fire { 
-webkit-transition: all 5000ms cubic-bezier(0.880, 0.045, 0.750, 0.750); 
    -moz-transition: all 5000ms cubic-bezier(0.880, 0.045, 0.750, 0.750); 
    -ms-transition: all 5000ms cubic-bezier(0.880, 0.045, 0.750, 0.750); 
    -o-transition: all 5000ms cubic-bezier(0.880, 0.045, 0.750, 0.750); 
     transition: all 5000ms cubic-bezier(0.880, 0.045, 0.750, 0.750); /* custom */ 
} 

你可以从这个网站产生这样的功能:http://matthewlein.com/ceaser/

附加

快速播放与转换http://jsfiddle.net/nmGRt/4/embedded/result/

+0

这是好的,但我不想这样做'CSS3' – 2013-02-28 10:17:10

+0

@Sarfaraz检查我更新的答案。 – 2013-02-28 17:39:34

+0

@Sarfaraz接受答案不会给予答案赏金。您可能需要选择您想要给予奖励价格的答案。您应该将赏金金额设置在接受答案的勾号下方。 http://meta.stackexchange.com/questions/163513/bounty-expired-before-i-could-assign-it-to-answer-punish-me-not-my-answerer – 2013-03-06 17:10:40

2

也许你与x轴和y轴的意思是这样,你可以说这样的确切位置......

$(document).ready(function(){ 
    $('#rocket').delay(2000).animate({ 
     bottom:'+=600px', 
     right:'-=200px' 
    }, 5000,function(){}); 
}); 

你也可以改变“底”到“顶”和“右”到“左”和“+ =”到“ - =”或只是“ - ”

+0

这也不错@caramba但是已经这样做了,如果你给'top'而不是'bottom',那么动画在所有浏览器中都不能正常工作,我在制作这个动画时检查了它,谢谢你的帮助 – 2013-03-01 07:31:58

2

请尝试fiddle连接。 您可能需要根据您的要求进行改进。

function animateCustom(cobj, clength, cintveral, cdirection) { 

    var i = 0; 

    var int = setInterval(function() { 
    if (i < clength) { 
     if (cdirection == 'y') 
      $(cobj).css("bottom", i + "px"); 
     else if (cdirection == 'x') 
      $(cobj).css("right", i + "px");    
     i++; 
    } else { 
     window.clearInterval(int); 
    } 
    }, cintveral); 

} 

animateCustom('#rocket', 100, 5, 'x'); 
animateCustom('#rocket', 200, 5, 'y'); 
5

尝试http://jsfiddle.net/naveendalmeida/JVMmE/2/

HTML

<div id="content"> 
    <div id="left"> 
     <div class="pr" id="ani"> 
      <h3 id="rocket" style="bottom:50px"></h3> 

      <div id="rocket-smoke"></div> 
     </div> 
    </div> 
</div> 

JS

<script type="text/javascript"> 

    function nav_animation(nav_obj, length, nav_intveral, direction) { 
     var i = 0; 
     var int = setInterval(function() { 
      if (i < length) { 
        $(nav_obj).css(direction, i + "px");  
       i++; 
      } else { 
       window.clearInterval(int); 
      } 
     }, nav_intveral); 
    } 

    nav_animation('#rocket', 50, 1, 'bottom'); 
    nav_animation('#rocket', 100, 1, 'right'); 
    nav_animation('#rocket', 150, 1, 'bottom'); 
    nav_animation('#rocket', 200, 1, 'bottom'); 
    nav_animation('#rocket', 350, 1, 'right'); 
    setInterval("$('#rocket').css('-webkit-transform',' rotate(0deg)');",1700); 
    nav_animation('#rocket', 400, 1, 'bottom'); 

</script> 

CSS

#rocket { 
    position:absolute; 
    bottom:50px; 
    right:91px; 
    background:url(http://walkoverapps.com/landing-content/rct.png) no-repeat; 
    width:68px; 
    height:84px; 
    z-index:20; 
    -webkit-transform: rotate(-45deg); 
} 

相关问题