我可以将按钮移动到左侧,但在此之后我可以再次将它移动到右侧。 我也可以在这里使用延迟。我想要使用jquery左右和左右移动按钮
这里是我已经试过代码:
$(document).ready(function() {
example_animate(10);
});
function example_animate(px) {
$('#Button1').animate({
'marginLeft': px
});
}
我可以将按钮移动到左侧,但在此之后我可以再次将它移动到右侧。 我也可以在这里使用延迟。我想要使用jquery左右和左右移动按钮
这里是我已经试过代码:
$(document).ready(function() {
example_animate(10);
});
function example_animate(px) {
$('#Button1').animate({
'marginLeft': px
});
}
你可以使用这个,这是为我工作很好,它会继续你的元素来回移动,你也可以改变动画的速度。
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);
如果不查看HTML和CSS,就无法正确回答,但是您所做的是正确的。只需使用一个负值调用你的example_animate()
即
example_animate(-10);
或者,如果你想将其带到原始值(假设原来它有0保证金)
注意:这可能不是生成动画的最佳方式
是的,animate函数采用在动画完成后调用的函数。所以,你可以这样做:
$(document).ready(function() {
example_animate(100);
});
function example_animate(px) {
$('#Button1').animate({
'marginLeft': px
}, function(){
$('#Button1').animate({
'marginLeft': 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
});
}
}
?
'example_animate(whateverWasTheOriginalMargin);'? – Shomz