首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有定义inView -instant-Render.js

没有定义inView -instant-Render.js
EN

Stack Overflow用户
提问于 2019-08-22 17:02:57
回答 1查看 108关注 0票数 0

我的网站在第一页上有大量的照片,这会降低页面的性能,并使加载时间过长。为了解决这个问题,我找到了lazy-instant-render.js,它可以防止在页面准备就绪时加载所有图像。我在控制台中得到这个错误:没有定义inView。

代码语言:javascript
复制
// Performance tests such as Google Lighthouse often complain about below the fold images while lazy loading 
// techniques may introduce a flash effect for images on bigger screens
// This snippet shows a solution to instantly display images as if loaded using src="" while maintaining 
// the lazy loading benefit on smaller screens.

// Tip: a second and potentially better technique is to rewrite lazy loaded images to regular src="" images in a Service Worker.

// this snippet depends on in-view (5kb)
// @link https://github.com/camwiegert/in-view/

// tiny cross browser domready method
// @link https://gist.github.com/dciccale/4087856
var DOMReady = function(a,b,c){b=document,c='addEventListener';b[c]?b[c]('DOMContentLoaded',a):window.attachEvent('onload',a)}

// requestAnimationframe
var RAF = window.requestAnimationframe || setTimeout;

var lazyToken = 'data-lzy';

// display image
var lazyResolve = function(el) {

    // already loaded
    if (!el.hasAttribute(lazyToken)) {
        return;
    }

    // verify if image is hidden by parent
    if (el.offsetParent !== null) {

        // remove lazy marker (to mark as completed)
        el.removeAttribute(lazyToken);

        // requestAnimationFrame to prevent layout trashing
        RAF(function() {
            el.setAttribute('src', el.getAttribute(lazyToken));
        });
    }
}

// setup lazy load method
DOMReady(function() {
    inView('['+lazyToken+']').on('enter', lazyResolve);
});

// instantly load visible images on render
var mutationObserver = window.MutationObserver || window.WebKitMutationObserver,
    observer;
if (mutationObserver) {
    observer = new mutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            var node;
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                node = mutation.addedNodes[i];
                if (node.nodeName === 'IMG' && node.hasAttribute(lazyToken)) {
                    if (inView.is(node)) {

                        // start download
                        new Image().src = node.getAttribute(lazyToken);

                        // display image with requestAnimationFrame
                        lazyResolve(node);
                    }
                }
            }
        });
    });
    observer.observe(document.documentElement, {
        childList: true,
        subtree: true
    });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-28 12:59:51

您可以使用新的标准loading=“惰性”-attribute功能来惰性加载图像(如果需要,还可以使用iframe)。此外,为了支持更老的浏览器,您可以使用我为此开发的polyfill:https://github.com/mfranzke/loading-attribute-polyfill

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57605695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档