English 中文(简体)
修改moototool 懒惰装载,以使用类类作为集装箱而不是id
原标题:Modify mootools lazyload to use class as container instead of id

下面是标准的mootools 懒惰装入代码。 我需要修改代码以接受 div 类作为懒惰装载容器, 而不是 div ID 。 任何想法吗? 使用 ID 和 Ajax 组合来阻止 懒惰装载 的正常工作, 因为页面没有重新装入, 所以它觉得新装入的同一种标识内容是该标识的第二例, 而不是同一元素 。 谢谢 。

    var LazyLoad = new Class({

        Implements: [Options,Events],

        /* additional options */
        options: {
            range: 100,
            elements: ".lazy",
            container: "post-list",
            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.getElementById(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.getElementById(this.options.container);
this.elements = $$(this.options.elements);

与:

// Elementize items passed in
this.container = $$(this.options.container)[0];
this.elements = $$(this.options.elements);

然后您可以给任何选择字符串( 如 < code >. yourClass , 但也给 < code>. some [complex=selector] ) 以 < code> container 类选项。 第一个匹配该选择的元素将被用作容器。 请告诉我它是否有效 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签