t-comm
Version:
专业、稳定、纯粹的工具库
86 lines (83 loc) • 2.78 kB
JavaScript
import { getHttpsUrl, getCompressImgUrl, getCdnUrl } from '../image/image.mjs';
import '@babel/runtime/helpers/typeof';
var windowLoaded = false;
var stoppedImages = [];
if (typeof window !== 'undefined') {
window.addEventListener('load', function () {
windowLoaded = true;
stoppedImages.forEach(function (img) {
// 因为被置空会导致前一次加载失败,重新加载
if (img.el.getAttribute('lazy') !== 'loading' && img.src) {
img.el.src = img.src;
}
});
});
}
/**
* 获取 vue-lazyload 插件参数
* @param options 选项
* @returns 插件参数
*/
function getVLazyOptions(options) {
if (options === void 0) {
options = {};
}
var loadingImg = options.loadingImg,
errorImg = options.errorImg;
return {
preLoad: 1.3,
attempt: 2,
filter: {
// 设置加载中图片
loading: function loading(listener) {
if (loadingImg) {
listener.loading = loadingImg;
}
},
// 设置加载失败图片
error: function error(listener) {
if (errorImg) {
listener.error = errorImg;
}
},
// 修改成https
https: function https(listener) {
listener.src = getHttpsUrl(listener.src);
},
// 裁剪压缩
compress: function compress(listener) {
var _a, _b;
// 10.31去除filter中图片压缩,保留loading中即可,否则会导致重复刷新时无法加载
// 11.13反馈微社区图片闪动,src和data-src不一致导致,上面的问题验证暂时没发现问题(重试次数修改为1了),放开解决闪动问题
listener.src = getCompressImgUrl(listener.src, ((_a = listener.el) === null || _a === void 0 ? void 0 : _a.width) ? listener.el.width : 0, ((_b = listener.el) === null || _b === void 0 ? void 0 : _b.height) ? listener.el.height : 0);
},
// 转换为cdn
cdn: function cdn(listener) {
listener.src = getCdnUrl(listener.src);
},
// 拦截window.onload前的图片加载
stopBeforeLoad: function stopBeforeLoad(listener) {
if (!windowLoaded) {
// 保存被拦截的图片
stoppedImages.push({
el: listener.el,
src: listener.src
});
listener.stoppedSrc = listener.src;
listener.src = '';
}
}
},
adapter: {
error: function error(listener) {
// 被拦截的图片在onload之后失败,需要重试
if (windowLoaded && listener.stoppedSrc && listener.stoppedSrc.length > 0) {
listener.src = listener.stoppedSrc;
listener.attempt = listener.attempt - 1;
listener.load();
}
}
}
};
}
export { getVLazyOptions };