仅在为其左边缘设置动画时更改图像Src

时间:2014-04-17 09:28:36

标签: javascript jquery html css animation

对于浏览器游戏目的,我想仅在图像动画时将图像src更改为动画gif。

  

$(the_image_selector).animate({marginLeft:amount},time);

我该怎么做?

我的想法是在调用animate之前将图像更改为gif,然后使用animate的回调来更改图像。它会起作用吗?

4 个答案:

答案 0 :(得分:2)

是的,我认为您的策略会起作用,例如

var image = $(the_image_selector);

image.attr('src', 'url to show while animated');

image.animate({marginLeft: amount}, time, function(){
    image.attr('src', 'normal url');
});

答案 1 :(得分:2)

你可以运行函数然后动画是complet。来自jquery.com

$( "#clickme" ).click(function() {
  $( "#book" ).css('background-image','any-image.gif');
  $( "#book" ).animate({
    opacity: 0.25,
    left: "+=50",
  }, 5000, function() {
  // Animation complete.
     $( "#book" ).css('background-image','any-image.png');
  });
});

答案 2 :(得分:0)

像这样使用:

$(the_image_selector).css('background-image','path-to-image');
$(the_image_selector).animate({marginLeft: amount}, time);

或者,像这样结合:

$(the_image_selector).css('background-image','path-to-image').animate({marginLeft: amount}, time);

或者,如果你想改变img的src,那么:

$(the_image_selector).attr('src','path-to-image').animate({marginLeft: amount}, time);

答案 3 :(得分:0)

Demo Fiddle

如我的评论所述,这将有效:

var src = $('img').attr('src');
$('img').attr('src', 'https://disney-animation.s3.amazonaws.com/uploads/production/project_image/frozen/72/image/project_image.jpg');
$('img').animate({
    marginLeft: '100px'
}, 2000, function () {
    $(this).attr('src', src);
});