2013-06-21 234 views
1

我可以将按钮移动到左侧,但在此之后我可以再次将它移动到右侧。 我也可以在这里使用延迟。我想要使用jquery左右和左右移动按钮

这里是我已经试过代码:

$(document).ready(function() { 
     example_animate(10); 


    }); 

    function example_animate(px) { 

    $('#Button1').animate({ 
     'marginLeft': px 

    }); 
} 
+0

'example_animate(whateverWasTheOriginalMargin);'? – Shomz

回答

1

你可以使用这个,这是为我工作很好,它会继续你的元素来回移动,你也可以改变动画的速度。

function animatethis(targetElement, speed) { 
     $(targetElement).animate({ marginLeft: "+=10px" }, 
{ 
    duration: speed, 
    complete: function() { 
     targetElement.animate({ marginLeft: "-=10px" }, 
     { 
      duration: speed, 
      complete: function() { 
       animatethis(targetElement, speed); 
      } 
     }); 
    } 
)}; 
} 

利用这个来实现:

animatethis($('#controlid'), 1500); 
-1

做execly同一只向右,它并不难,如果你可以把它向左走。

+2

这个答案比评论还差。 – defau1t

+0

它只是明显的 – 2013-06-21 12:42:30

+0

这不是一个答案。它应该是对问题本身的评论。对你而言,这可能是显而易见的,但对于提问的人来说,这并不明显。 – andyb

0

如果不查看HTML和CSS,就无法正确回答,但是您所做的是正确的。只需使用一个负值调用你的example_animate()

example_animate(-10); 

或者,如果你想将其带到原始值(假设原来它有0保证金)

注意:这可能不是生成动画的最佳方式

0

是的,animate函数采用在动画完成后调用的函数。所以,你可以这样做:

$(document).ready(function() { 
     example_animate(100); 
    }); 

    function example_animate(px) { 
    $('#Button1').animate({ 
     'marginLeft': px 
    }, function(){ 
     $('#Button1').animate({ 
     'marginLeft': 1 
     }); 
    }); 
} 

http://jsbin.com/ixajol/1/edit

-1

也许

var button_init_marginLeft; 
$(document).ready(function() { 
    button_init_marginLeft = $('#Button1').css("marginLeft"); 
    example_animate(10, true); 
    example_animate(null, false); 
}); 

function example_animate(px, to_left) { 
    if (to_left) 
    { 
    $('#Button1').animate({ 
     'marginLeft': px 
    }); 
    } 
    else 
    { 
    $('#Button1').animate({ 
     'marginLeft': button_init_marginLeft 
    }); 
    } 
}