为 ajax 内容添加淡入淡出效果

Add fade effect to ajax content

我有一个 wordpress 网站,当我点击一个按钮时,我的加载器后面会出现 ajax 内容。代码运行良好,但我想添加当前内容淡出和新内容淡入的效果,不再需要加载程序。

1.    
jQuery(document).ready(function() {
        jQuery('#main-content').on('click', '.jax a, .page-navigation a', function(e) {
            e.preventDefault();
            var url = jQuery(this).attr('href'),
                title = jQuery(this).attr('title')
            ;
            jQuery('#main-content').html('<img id="loader" alt="loading" width="43" height="11" src="<?php echo get_template_directory_uri(); ?>/library/images/squares.gif"/ >').load(url+ ' #main-content');
            document.title = title;
            history.pushState({url:url,title:title}, title, url );
          });
        });

window.onpopstate = function(event) {
    document.title = event.state.title;
    jQuery('#main-content').html('<img id="loader" alt="loading" width="43" height="11" src="<?php echo get_template_directory_uri(); ?>/library/images/squares.gif"/ >').load(event.state.url+ ' #main-content');
}

我已经尝试了多种变体,例如下面的变体,它可以工作,但它不会像第一个示例中那样更新地址 URL 以及每个 post。

2.
jQuery('.jax a, .page-navigation a').live('click', function(event) {
            var link = $(this).attr('href');
            jQuery('#main-content').fadeOut('slow', function(){
        jQuery('#main-content').load(link+' #main-content', function(){
            jQuery('#main-content').fadeIn('slow');
        });
    });
            return false;
});

知道如何在第一个示例中添加淡入淡出或在第二个示例中添加地址 URL 更新(历史记录 API)吗?

我没有适合您的解决方案,但也许这个对其他人问题的回答可以帮助您:

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.


或者这个:

他能够通过将效果应用到包装器来实现淡出、淡入 div。


您可以尝试以下方法:

jQuery('.jax a, .page-navigation a').live('click', function(event) {
        var url = jQuery(this).attr('href'),
            title = jQuery(this).attr('title');
        jQuery('#main-content').fadeOut('slow', function(){
    jQuery('#main-content').load(url+' #main-content', function(){
        jQuery('#main-content').fadeIn('slow');
        document.title = title;
        history.pushState({url:url,title:title}, title, url );
    });
});
        return false;
});