2009-10-09 44 views
6

我开始学习jQuery。我编写了一个代码,它将通过调用存储在数组中的函数来循环给定元素的位置。这此代码工作正常:当动画时取消设置css参数

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#right_column').css({zIndex:1000, position: 'absolute', right: 0, top:200 }); 
    $('body').css({overflow:'hidden'}); 
    var funcs = new Array(
        function(o){ 
         $(o).animate({right: 0, top:200}, 1000); 
        }, 
        function(o){ 
         $(o).animate({top: 0},1000); 
        }, 
        function(o){ 
         $(o).animate({right: 300},1000); 
        }, 
        function(o){ 
         $(o).animate({right: 300, top:200},1000); 
        } 
       ); 
    var flip=0; 
    $('#right_column').click(function self(){ 
      funcs[++flip % funcs.length]('#right_column'); 
    }); 
}); 
</script> 

,但如果我改变位置参数,这样

var funcs = new Array(
        function(o){ 
         $(o).animate({right: 0, top:200}, 1000); 
        }, 
        function(o){ 
         $(o).animate({top: 0},1000); 
        }, 
        function(o){ 
         $(o).animate({left: 0},1000); 
        }, 
        function(o){ 
         $(o).animate({right: 300, bottom:0},1000); 
        } 
       ); 

它打破。

我假设互补参数(top top < - > bottom; left < - > right)会干扰,就像他们在普通的css中所做的一样。

所以我的问题是:如何将css参数重置为undefined?

编辑

animate似乎并不能够处理auto可能。它没有改变行为。 与css()它的工作原理。

var funcs = new Array(
        function(o){ 
         $(o).css({right:0, bottom:0,left:'auto', top:'auto'}) 
         console.log('funcs 0'); 
        }, 
        function(o){ 
         $(o).css({top:0, right:0, left:'auto', bottom:'auto'}) 
         console.log('funcs 1'); 
        }, 
        function(o){ 
         $(o).css({left:0, top:0, right:'auto', bottom:'auto'}) 
         console.log('funcs 2'); 
        }, 
        function(o){ 
         $(o).css({right:'auto', top:'auto',left:0, bottom:0}) 
         console.log('funcs 3'); 
        } 
       ); 

css({...:'auto'})运行前/后animate()破坏动画

任何其它提示(原因)?

回答

6

重置这些值并将其更改为 '自动'

top: auto; 
left: auto; 
right: 0px; 
bottom: 0px; 

编辑:

您可以添加 “目标”。

<div id="top" style="position:absolute;left:0;top:0"></div> 
<div id="bottom" style="position:absolute;left:0;top:auto;bottom:0px"></div> 

现在你可以使用你的目标的值动画:

$("#bottom").offset().top 
$("#bottom").offset().left 

动画:

$(o).animate({ top : $("#bottom").offset().top, left : $("#bottom").offset().left }); 
+0

哦,很简单:d我在心里对复杂... – vikingosegundo 2009-10-09 13:33:35