Can I combine the .load function with fadeIn in jQuery?

孤者浪人 提交于 2019-12-12 04:25:00

问题


I am using the .load function to load in another page using jQuery.

Here is my code:

$('#page1').click(function(){                           
  $("#content").load("page1.html");
});

It's working great, but I would like to fade in the new page. Is there anyway I can combine the load and fadeIn function? I attempted it, but it's not working.

Here is my attempt:

$('#page1').click(function(){                           
  $("#content").load("page1.html").fadeIn("normal");
});

How can I combine the .load and .fadeIn function?


回答1:


The call to load will use AJAX and will be run asynchronously. You'll want to fade in right after the call is terminated. You can achieve that by passing a callback to load. Your code will look like this:

$('#content').load("page1.html", {}, function() { $(this).fadeIn("normal"); }));

See documentation on jQuery's .load() for more information.




回答2:


Hide #content before the load, and fade in the entire div when load is complete. I'd guess load acceps a callback function...?

$('#content').hide();
$('#content').load('page1.html', function() { $('#content').fadeIn('normal'); });

EDIT do what miguel said, anyway =)



来源:https://stackoverflow.com/questions/1789331/can-i-combine-the-load-function-with-fadein-in-jquery

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