Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否按其可见性加载所有照片?_Javascript - Fatal编程技术网

Javascript 是否按其可见性加载所有照片?

Javascript 是否按其可见性加载所有照片?,javascript,Javascript,我有原稿: 我需要出现在图像中,图像显示。当他使用html时,脚本工作正常。但是还有一个问题,把这个脚本放在一个单独的文件中。但是我没有,让它工作,所以它工作了 我有一个library.js,该文件自动包含在每个页面中: var LIBRARY = { init: function() { $('image[itemprop = image]').each(LIBRARY.lazyLoad()); }, /** * LazyLoad images

我有原稿: 我需要出现在图像中,图像显示。当他使用html时,脚本工作正常。但是还有一个问题,把这个脚本放在一个单独的文件中。但是我没有,让它工作,所以它工作了

我有一个library.js,该文件自动包含在每个页面中:

var LIBRARY = {
   init: function() {
       $('image[itemprop = image]').each(LIBRARY.lazyLoad());
    },
    /**
     * LazyLoad images
     */
    lazyLoad: function(event) {
        $(function() {
            var $window = $(window),
                images = [],
                imagesToBeLoaded = 0,
                i,
                src;

            function throttle(func, wait) {
                var timeout;
                return function() {
                    var context = this, args = arguments;
                    if(!timeout) {
                        timeout = setTimeout(function() {
                            timeout = null;
                        }, wait);
                        func.apply(context, args);
                    }
                };
            }

            function inViewport($el) {
                var top = $window.scrollTop(),
                    left = $window.scrollLeft(),
                    bottom = top + $window.height(),
                    right = left + $window.width(),
                    offset = $el.offset(),
                    thisTop = offset.top,
                    thisLeft = offset.left,
                    thisBottom = thisTop + $el.outerHeight(),
                    thisRight = thisLeft + $el.outerWidth();

                return !(
                    bottom < thisTop ||
                        top > thisBottom ||
                        right < thisLeft ||
                        left > thisRight
                    );
            }

            // throttle so we don't do too many calls
            var lazyScroll = throttle(function() {
                // have all images been loaded?
                if(imagesToBeLoaded > 0) {
                    for(i = 0; i < images.length; i++) {
                        // data is there if nothing has been done to it
                        src = images[i].data('src');
                        // see if the image is in the view
                        if(src && inViewport(images[i])) {
                            // create a nice closure here
                            (function(img, src, i, $img) {
                                img.onload = function() {
                                    console.log('Loaded ' + i + ' ' + img.src);
                                    $img.attr('src', img.src);
                                    imagesToBeLoaded--;
                                };
                                img.onerror = function() {
                                    console.log('Could not load ' + i + ' ' + img.src);
                                    imagesToBeLoaded--;
                                };
                                // important to remove this to avoid duplicate calls
                                $img.removeData('src');
                                // start loading the image
                                img.src = src;
                            })(new Image(), src, i, images[i]);
                        }
                    }
                } else {
                    // cleanup
                    images = void 0;
                    // why keep listening if there is nothing to listen
                    $window.off('resize scroll touchmove', lazyScroll);
                    // all images are loaded
                    console.log('Unloaded event listener');
                }
            }, 50);

            $('image[itemprop = image]').each(function() {
                var $this = $(this),
                    $img = $(this.innerText || $this.text()).filter('img');
                // make sure we got something
                if($img.length === 1) {
                    // remember the real image
                    $img.data('src', $img.attr('src'));
                    // use a blank image
                    $img.attr('src', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
                    // cache a reference
                    images.push($img);
                    // replace noscript element with the image
                    $this.replaceWith($img);
                    imagesToBeLoaded++;
                }
            });
            // only add if we need it
            if(imagesToBeLoaded) {
                lazyScroll();
                $window.on('resize scroll touchmove', lazyScroll);
            }
        });
    }
};

我需要连接init:event,它将运行脚本并正确显示图像,换句话说,如果用户访问图像,那么它将被加载。告诉我如何操作以及需要修复什么?

您可以使用document ready:

比如:

$(document).ready(LIBRARY.init);

我建议使用此代码-揭幕不使用,因为我无法将某些属性添加到img标记。您不需要添加任何属性。您可以向我展示代码示例吗?我给您的链接中有一些代码示例。他们怎么了?不!这不对!我需要在我的站点中的每个图像上运行此脚本。此脚本已连接到站点。我不知道我需要如何正确地编写这段代码,这应该可以正常工作。这不是你的代码$'image[itemprop=image]'。eachLIBRARY.lazyLoad;正在做什么?不。我需要为站点上的每个图像运行此脚本。您认为$'image[itemprop=image]'如何。每个。。。是吗?我需要运行一个脚本来延迟加载图片