javascript - 无限滚动 - 将 jquery 效果应用于所有滚动页面上的元素

标签 javascript jquery ajax infinite-scroll

我正在使用 infinite-ajax-scroll plugin连同 countdown plugin和一个 hover Caption plugin和 Metro JS(http://www.drewgreenwell.com/projects/metrojs)。

JQuery 效果在第一页上运行良好,但不适用于通过无限滚动插件加载的页面。

我知道我需要在 Infinite Scroll 初始化中使用 onLoadItemsonRenderComplete,但是,我对 JQuery 语法不是很好,希望能得到任何帮助这个工作。

//code for countdown applied to every element
echo "<script type=\"text/javascript\">
    $(window).load(function(){
        $('#countdown_$id').countdown({until: new Date($year, $month - 1, $day, $hour, $minute, $secs)});
    });
</script>";
echo '<div class="defaultCountdown" id="countdown_'.$id.'"></div>';

//code that applied hover captions and infinite scroll
$(document).ready(function () {
    $('.hcaption').hcaptions({
        effect: "fade"
    });
    //animate tiles
    $(".live-tile").liveTile({pauseOnHover: true});

    // Infinite Ajax Scroll configuration
    jQuery.ias({
        container: '#main', // main container where data goes to append
        item: '.element', // single items
        pagination: '.paginate', // page navigation
        next: '.paginate a', // next page selector
        loader: '<img src="public/img/ajax-loader.gif"/>', // loading gif
        loaderDelay: 200,
        thresholdMargin: -600,
        noneleft: 'No more discounts', //Contains the message to be displayed when there are no more pages left to load
        triggerPageThreshold: '10', // show "load more" if scroll more than this to stop
        trigger: "",
        onLoadItems: function (newElements) {
            // hide new items while they are loading
            var $newElems = $(newElements).css({
                opacity: 0
            });
            // ensure that images load before adding to isotope layout
            $newElems.imagesLoaded(function () {
                // show elems now they're ready
                $newElems.animate({
                    opacity: 1
                });
                $container.isotope('insert', $newElems, true);
            });
            return true;
        }
    });
    //end infinite
});

最佳答案

初始化滚动时,您需要使用onLoadItems onRenderComplete:

jQuery.ias({
    container: '#main', // main container where data goes to append
    item: '.element', // single items
    pagination: '.paginate', // page navigation
    next: '.paginate a', // next page selector
    loader: '<img src="public/img/ajax-loader.gif"/>', // loading gif
    loaderDelay: 200,
    thresholdMargin: -600,
    noneleft: 'No more discounts'
    triggerPageThreshold: '10',
    trigger: "",
    onLoadItems: function (newElements) {
        // hide new items while they are loading
        var $newElems = $(newElements).css({
            opacity: 0
        });
        // ensure that images load before adding to isotope layout
        $newElems.imagesLoaded(function () {
            // show elems now they're ready
            $newElems.animate({
                opacity: 1
            });
            $container.isotope('insert', $newElems, true);
        });
        setTimeout("$('.hcaption').hcaptions({effect: 'fade'})",1000);
        setTimeout('$(".live-tile, .flip-list").not(".exclude").liveTile()',1000);
        return true;
    }
});
//end infinite

DEMO

我也试过

onRenderComplete: function(items) {
    $('.hcaption').hcaptions({effect: "fade"});
    $(".live-tile, .flip-list").not(".exclude").liveTile();
}

而且效果很好。

关于javascript - 无限滚动 - 将 jquery 效果应用于所有滚动页面上的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20122108/

相关文章:

javascript - 轮播 HTML 不工作

javascript - jQuery的幻灯片效果

javascript - 如何等待 _.each 循环与 ajax 调用完成?

jquery - 如何使用 DJANGO REST 框架和 ajax 发出 POST 请求

javascript - 使用 requireJS 异步插件和 requireJS jsonp 格式有区别吗

javascript - minicart.js 不工作,购物车不弹出

javascript - 为具有 cookie 的弹出窗口设置到期日期(和时间)

javascript - Jquery在点击后禁用href并在3秒后启用

jquery - 如何在不让 .height 返回不准确的值的情况下更改默认字体大小

javascript - php 数组中转换 json 字符串时出现问题