繁体   English   中英

仅在为图像Src设置边距动画时更改图像Src

[英]Change image Src only while animating its margin-left

出于浏览器游戏的目的,我只想在对图像进行动画处理时将图像src更改为动画gif。

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

我该怎么做?

我的想法是在调用animate之前将图像更改为gif,然后使用animate的回调来变回图像。 能行吗

是的,例如,我认为您的策略会奏效

var image = $(the_image_selector);

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

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

您可以运行功能,然后动画就完成了。 来自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');
  });
});

像这样使用:

$(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);

演示小提琴

如我的评论所述,这将起作用:

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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM