简体   繁体   English

jquery在将新页面加载到带动画的div中时出现问题

[英]jquery issue in loading a new page into a div with animation

I am looking to have a div, which holds a php page to fade out, unload, and then fade in a new page on every menu: 我希望有一个div,它拥有一个淡出,卸载的PHP页面,然后在每个菜单上淡入新页面:

        $('#home').click(function() {
        $('#page_container').fadeOut(500).unload();
        $('#page_container').load('php/home.php').fadeIn(550);
    });

For some reason, it flickers the new page, before it fades out in the first instance, fades out and reloads. 出于某种原因,它会在新页面消失之前闪烁,然后逐渐淡出并重新加载。

Could someone please help me with this. 有人可以帮我这个。 I am sure it's simple, but I cannot seem to find the right syntax to get it to work? 我确信这很简单,但我似乎找不到正确的语法来让它工作?

You should set the load() call to occur in the callback of the fade function: 你应该在fade函数的回调中设置load()调用:

$("#home").on("click", function () {
    $('#page_container').fadeOut('fast', function() {
        $(this).unload();
        $(this).load('php/home.php');
    });
})

This will fire load() once the fade is completed. 一旦淡入淡出完成,这将触发load()

fadeOut() is asynchronous, and so the next line is executed immediately after it is called. fadeOut()是异步的,因此下一行在调用后立即执行。 Your problem is caused because load() is loading content into your div immediately as it starts to fade out. 您的问题是由于load()在内容开始淡出时立即将内容加载到div中引起的。

Without a demo it's hard to fully understand, but have you looked at using callbacks? 没有演示就很难完全理解,但是你看过使用回调吗?

$('#home').click(function() {
    $('#page_container').fadeOut(500, function(){ 
        $(this).unload(); 
        $('#page_container').load('php/home.php', function(){ 
            $(this).fadeIn(550) 
        });
    });

});

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

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