lazyload.js 864 Bytes
import $ from 'jquery';
import imgSrcHandle from './img-src-handle';
import 'jquery-lazyload';

/**
 * 图片链接预处理后使用原生 lazyload
 */
const rawLazyload = (selector, options) => {
    let $imgs;
    let params = {
        threshold: 700 * 2 // 一页大概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);
    }
};

export default rawLazyload;