Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery添加/删除类&;AJAX不起作用_Javascript_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript jQuery添加/删除类&;AJAX不起作用

Javascript jQuery添加/删除类&;AJAX不起作用,javascript,jquery,ajax,wordpress,Javascript,Jquery,Ajax,Wordpress,我在WordPress中使用同位素和AJAX来获取一些帖子,几乎所有的东西都在那里,除了我在内容中使用AJAX时运行的默认同位素动画,这看起来有点奇怪。不过,我还是希望它在过滤时具有动画效果 所以我想我可以在AJAX函数中使用add/removeClass在需要时关闭/打开它,但是如果我这样做,动画就永远不会运行 你知道我哪里做错了吗 var page = 1; var loading = true; var $window = $(window); var $content = $('.iso

我在WordPress中使用同位素和AJAX来获取一些帖子,几乎所有的东西都在那里,除了我在内容中使用AJAX时运行的默认同位素动画,这看起来有点奇怪。不过,我还是希望它在过滤时具有动画效果

所以我想我可以在AJAX函数中使用add/removeClass在需要时关闭/打开它,但是如果我这样做,动画就永远不会运行

你知道我哪里做错了吗

var page = 1;
var loading = true;
var $window = $(window);
var $content = $('.isotope');

if( $('body').hasClass('home') ) {
    var loopHandler = '/inc/loop-handler.php';
} else {
    var loopHandler = '/inc/loop-handler-archive.php';
}

var load_posts = function(){
        $.ajax({
            type       : "GET",
            data       : {numPosts : 1, pageNumber: page},
            dataType   : "html",
            url        : templateDir+loopHandler,
            beforeSend : function(){
                if(page != 1){
                    $('.loader').append('<div id="temp_load" style="text-align:center; z-index:9999;">\
                        <img src="'+templateDir+'/img/ajax-loader.gif" />\
                        </div>');
                }
            },
            success    : function(data){

                $data = $(data);
                if($data.length){

                    var itemHeight = $('.item').height();
                    var height = $content.height()+itemHeight*2;


                    $content.append($data);

                    $content.css('min-height', height);

                    $data.hide();

                    // should stop the animation
                    $('.isotope').addClass('no-transition');

                    $content.isotope('destroy');

                    $content.imagesLoaded( function(){

                        $content.isotope({
                          // options
                          layoutMode: 'fitRows',
                          itemSelector : '.item'
                        });

                    });

                    $data.fadeIn(700, function(){
                        $("#temp_load").fadeOut(700).remove();
                        loading = false;
                    }); 

                    // should start up the animation again
                    $('.isotope').removeClass('no-transition');

                    $content.css('min-height', '0');



                } else {
                    $("#temp_load").remove();
                }
            },
            error     : function(jqXHR, textStatus, errorThrown) {
                $("#temp_load").remove();
                alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
    });
}


$window.scroll(function() {
    var content_offset = $content.offset(); 
    console.log(content_offset.top);
    if(!loading && ($window.scrollTop() + 
        $window.height()) > ($content.scrollTop() +
        $content.height() + content_offset.top)) {
            loading = true;
            page++;
            load_posts();
    }
});
load_posts();
var-page=1;
var加载=真;
变量$window=$(window);
变量$content=$('.isotox');
if($('body').hasClass('home')){
var loopHandler='/inc/loopHandler.php';
}否则{
var loopHandler='/inc/loopHandler archive.php';
}
var load_posts=函数(){
$.ajax({
键入:“获取”,
数据:{numPosts:1,页码:page},
数据类型:“html”,
url:templateDir+loopHandler,
beforeSend:function(){
如果(第!=1页){
$('.loader').append('\
\
');
}
},
成功:功能(数据){
$data=$(数据);
if($data.length){
var itemHeight=$('.item').height();
var height=$content.height()+itemHeight*2;
$content.append($data);
$content.css('min-height',height);
$data.hide();
//应该停止动画
$('.同位素').addClass('no-transition');
$content.同位素('destroy');
$content.imagesLoaded(函数(){
$content.同位素({
//选择权
布局模式:“fitRows”,
itemSelector:“.item”
});
});
$data.fadeIn(700,函数(){
$(“#临时加载”).fadeOut(700).remove();
加载=假;
}); 
//应该重新启动动画
$('同位素').removeClass('无过渡');
$content.css('min-height','0');
}否则{
$(“#临时加载”).remove();
}
},
错误:函数(jqXHR、textStatus、errorshown){
$(“#临时加载”).remove();
警报(jqXHR+“:”+textStatus+“::”+errorshown);
}
});
}
$window.scroll(函数(){
var content_offset=$content.offset();
console.log(content\u offset.top);
如果(!loading&&($window.scrollTop()+
$window.height())>($content.scrollTop())+
$content.height()+content\u offset.top)){
加载=真;
page++;
加载_柱();
}
});
加载_柱();

如果您需要任何HTML/PHP,我很乐意发布。另外,如果你想查看它。

我不知道同位素,我还没有测试它是否有效。但是我已经用注释重构了您的代码,以帮助您更好地理解这个问题

我认为它来自于您依次调用removeClass和addClass的方式,这两个类会立即取消

var page = 1;
var loading = true;
var $window = $(window);
var $content = $('.isotope');
var loopHandler = '/inc/loop-handler.php';

var isotopeController = {
    append: function($data) {

        // Add AJAX data
        var itemHeight = $('.item').height();
        var height = $content.height() + itemHeight * 2;
        $content.append($data);
        $content.css('min-height', height);

        // Stop
        isotopeController.stop($data);

        // Restart
        $content.imagesLoaded(function() {
            // When images loaded !
            isotopeController.start($data);
        });
    },
    start: function($data) {

        // Start isotope
        $content.isotope({
            layoutMode: 'fitRows',
            itemSelector: '.item'
        });

        // Show data
        $data.fadeIn(700, function() {
            $("#temp_load").fadeOut(700).remove();
            loading = false;
        });

        // Start the animation
        $content.removeClass('no-transition');
        $content.css('min-height', '0');
    },
    stop: function($data) {

        // Hide data
        $data.hide();

        // Stop the animation
        $content.addClass('no-transition');

        // Stop isotope
        $content.isotope('destroy');
    },
    loadPosts: function() {
        $.ajax({
            type: "GET",
            data: {
                numPosts: 1,
                pageNumber: page
            },
            dataType: "html",
            url: templateDir + loopHandler,
            beforeSend: function() {
                if (page != 1) {
                    $('.loader').append('' +
                        '<div id="temp_load" style="text-align:center; z-index:9999;">' +
                        '<img src="' + templateDir + '/img/ajax-loader.gif" />' +
                        '</div>'
                    );
                }
            },
            success: function(data) {
                var $data = $(data);
                if ($data.length) {
                    isotopeController.append($data);
                } else {
                    $("#temp_load").remove();
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $("#temp_load").remove();
                alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    }
};

$window.scroll(function() {
    var content_offset = $content.offset();
    if (!loading && ($window.scrollTop() + $window.height()) > ($content.scrollTop() + $content.height() + content_offset.top)) {
        loading = true;
        page++;
        isotopeController.loadPosts();
    }
});

// On load
$(function() {
    if (!$('body').hasClass('home')) {
        loopHandler = '/inc/loop-handler-archive.php';
    }
    isotopeController.loadPosts();
});
var-page=1;
var加载=真;
变量$window=$(window);
变量$content=$('.isotox');
var loopHandler='/inc/loopHandler.php';
var同位素控制器={
附加:函数($data){
//添加AJAX数据
var itemHeight=$('.item').height();
var height=$content.height()+itemHeight*2;
$content.append($data);
$content.css('min-height',height);
//停止
同位素控制器停止($data);
//重新启动
$content.imagesLoaded(函数(){
//当图像加载!
isotoconcontroller.start($data);
});
},
开始:函数($data){
//起始同位素
$content.同位素({
布局模式:“fitRows”,
itemSelector:“.item”
});
//显示数据
$data.fadeIn(700,函数(){
$(“#临时加载”).fadeOut(700).remove();
加载=假;
});
//启动动画
$content.removeClass('no-transition');
$content.css('min-height','0');
},
停止:函数($data){
//隐藏数据
$data.hide();
//停止动画
$content.addClass('no-transition');
//停止同位素
$content.同位素('destroy');
},
loadPosts:function(){
$.ajax({
键入:“获取”,
数据:{
数量:1,
页码:第页
},
数据类型:“html”,
url:templateDir+loopHandler,
beforeSend:function(){
如果(第!=1页){
$('.loader')。追加(''+
'' +
'' +
''
);
}
},
成功:功能(数据){
var$data=$(数据);
if($data.length){
ISOTOCONTROLER.append($data);
}否则{
$(“#临时加载”).remove();
}
},
错误:函数(jqXHR、textStatus、errorshown){
$(“#临时加载”).remove();
警报(jqXHR+“:”+textStatus+“::”+errorshown);
}
});
}
};
$window.scroll(函数(){
var content_offset=$content.offset();
如果(!loading&&($window.scrollTop()+$window.height())>($content.scrollTop()+$content.height()+content_offset.top)){
加载=真;
page++;
同位素控制器。loadPosts();
}
});
//装载
$(函数(){
if(!$('body').hasClass('home')){
loopHandler='/inc/loop ha