lazyload.js
884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const $ = require('yoho-jquery');
const imgSrcHandle = require('./img-src-handle');
require('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);
}
};
module.exports = rawLazyload;