jQuery fadeOut, replaceWith, animate almost working

早过忘川 提交于 2020-01-03 18:34:13

问题


I am trying to accomplish the following: 1. On click, have a div with id="fader" fadeout 2. replaceHtml of fader with new html (this new HTML will appear below the fold of the browser) 3. Animate new HTML to slide up to the specified location

Step 1 and 2 are working, step 3 is not and I'm stumped as to why.

Here's the javascript:

$("#fader").fadeOut(1000, function() {
  $(this).replaceWith('<div id=\"fader\" style=\"margin-top:-500px;width:500px;height:400px;border:1px solid black;\">new div</div>', function() {
    $("#fader").animate({marginTop: "500px"});
  });
});

Any thoughts on why the div won't animate would be greatly appreciated, thanks in advance!


回答1:


In your case .replaceWith() has no callback, it's has a different signature than animations have.

Try this instead:

$("#fader").fadeOut(1000, function() {
  $(this).replaceWith('<div id="fader" style="margin-top:-500px;width:500px;height:400px;border:1px solid black;">new div</div>');
  $("#fader").animate({marginTop: "500px"});
});

Note you can't chain this, .replaceWith() returns the original object, not the one you just created.



来源:https://stackoverflow.com/questions/2465431/jquery-fadeout-replacewith-animate-almost-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!