Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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 如何在mootools lazyload中加载常规图像_Javascript_Mootools_Lazy Loading - Fatal编程技术网

Javascript 如何在mootools lazyload中加载常规图像

Javascript 如何在mootools lazyload中加载常规图像,javascript,mootools,lazy-loading,Javascript,Mootools,Lazy Loading,大卫·沃尔什的懒散生活有一个问题,有些人似乎在与之斗争 我不希望页面上的每个图像都懒散地加载,问题是,没有'datasrc'但只有'src'的图像根本不会加载 我曾尝试将选项“container:window”中的替换为“container:”mydiv“”,但lazyload代码仍然适用于整个页面 您能否向代码中添加一些内容,以便lazyload仅在看到包含数据的图像时才能工作 var LazyLoad = new Class({ Implements: [Option

大卫·沃尔什的懒散生活有一个问题,有些人似乎在与之斗争

我不希望页面上的每个图像都懒散地加载,问题是,没有'datasrc'但只有'src'的图像根本不会加载

我曾尝试将选项“container:window”中的替换为“container:”mydiv“”,但lazyload代码仍然适用于整个页面

您能否向代码中添加一些内容,以便lazyload仅在看到包含数据的图像时才能工作

    var LazyLoad = new Class({
        Implements: [Options,Events],

        /* additional options */
        options: {
            range: 200,
            elements: "img",
            container: window,
            mode: "vertical",
            realSrcAttribute: "data-src",
            useFade: true
        },

        /* initialize */
        initialize: function(options) {

            // Set the class options
            this.setOptions(options);

            // Elementize items passed in
            this.container = document.id(this.options.container);
            this.elements = $$(this.options.elements);

            // Set a variable for the "highest" value this has been
            this.largestPosition = 0;

            // Figure out which axis to check out
            var axis = (this.options.mode == "vertical" ? "y": "x");

            // Calculate the offset
            var offset = (this.container != window && this.container != document.body ? this.container : "");

            // Find elements remember and hold on to
            this.elements = this.elements.filter(function(el) {
                // Make opacity 0 if fadeIn should be done
                if(this.options.useFade) el.setStyle("opacity",0);
                // Get the image position
                var elPos = el.getPosition(offset)[axis];
                // If the element position is within range, load it
                if(elPos < this.container.getSize()[axis] + this.options.range) {
                    this.loadImage(el);
                    return false;
                }
                return true;
            },this);

            // Create the action function that will run on each scroll until all images are loaded
            var action = function(e) {

                // Get the current position
                var cpos = this.container.getScroll()[axis];

                // If the current position is higher than the last highest
                if(cpos > this.largestPosition) {

                    // Filter elements again
                    this.elements = this.elements.filter(function(el) {

                        // If the element is within range...
                        if((cpos + this.options.range + this.container.getSize()[axis]) >= el.getPosition(offset)[axis]) {

                            // Load the image!
                            this.loadImage(el);
                            return false;
                        }
                        return true;

                    },this);

                    // Update the "highest" position
                    this.largestPosition = cpos;
                }

                // relay the class" scroll event
                this.fireEvent("scroll");

                // If there are no elements left, remove the action event and fire complete
                if(!this.elements.length) {
                    this.container.removeEvent("scroll",action);
                    this.fireEvent("complete");
                }

            }.bind(this);

            // Add scroll listener
            this.container.addEvent("scroll",action);
        },
        loadImage: function(image) {
            // Set load event for fadeIn
            if(this.options.useFade) {
                image.addEvent("load",function(){
                    image.fade(1);
                });
            }
            // Set the SRC
            image.set("src",image.get(this.options.realSrcAttribute));
            // Fire the image load event
            this.fireEvent("load",[image]);
        }
    });

    /* do it! */
        window.addEvent("domready",function() {
            var lazyloader = new LazyLoad({/*
                onScroll: function() { console.warn("scroll!"); },
                onLoad: function(img) { console.warn("load!", img); },
                onComplete: function() { console.warn("complete!"); }
                */
            });
        });

嗯。看看代码:

      // Elementize items passed in
        this.container = document.id(this.options.container);
        this.elements = $$(this.options.elements);
看起来没有引用容器

改为:

      // Elementize items passed in
        this.container = document.id(this.options.container);
        this.elements = this.container.getElements(this.options.elements);

并确保options.elements是一个合理的选择器

我不知道如何“使options.elements成为合理的选择器”。。。但我发现这种代码类似于
img[data src]
img.someclassname
——只要它是获取所需元素的唯一方法。他们将是父母的孩子。
      // Elementize items passed in
        this.container = document.id(this.options.container);
        this.elements = $$(this.options.elements);
      // Elementize items passed in
        this.container = document.id(this.options.container);
        this.elements = this.container.getElements(this.options.elements);