lazyload.js 884 Bytes
const $ = require('yoho-jquery');
const imgSrcHandle = require('./img-src-handle');

require('jquery-lazyload');

/**
 * 图片链接预处理后使用原生 lazyload
 */
const rawLazyload = (selector, options) => {
    let $imgs;
    let params = {
        threshold: 700 * 3 // 一页大概700 ,提前加载三、四页
    };

    if (selector instanceof $) {
        $imgs = selector;
    } else {
        $imgs = $(selector).find('img.lazy');
    }

    $.extend(params, options);

    const imgsProcess = () => {
        $imgs.each((index, elem) => {
            $(elem).attr('data-original', imgSrcHandle($(elem).data('original'), options));
        });
        $imgs.lazyload(params);
    };

    if (typeof window.supportWebp !== 'undefined') {
        imgsProcess();
    } else {
        $(document).on('supportWebp', imgsProcess);
    }
};

module.exports = rawLazyload;