2

我需要克隆一个元素,一旦调整页面大小,就会覆盖那里的当前元素。

我当前的代码不起作用,如下所示:

var clone = false;
window.addEvent("domready",function(){
    clone = $$('.view').clone();
    /* Wait for images to be 100% loaded otherwise they might not have width/height */
    var lazyloader = new LazyLoad({
        onComplete: function(){
            setupWall();
            window.addEvent('resize:throttle(1000)', function(){
                /* Delete old viewport and add back with cloned version */
                var view = $$('.view');
                if(view){
                    wall = null;
                    view.dispose();
                }
                var cloneElement = $$('#wrapper').inject(clone);
                console.log(cloneElement);
                //setupWall();
            });
        }
    });
});

运行时$$('#wrapper').inject(clone);出现以下错误:

无法调用 null 的方法“appendChild”

正如这里的评论中所建议的那样,html也是如此。

<div id="wrapper">  
    <section class="view" id="wall">
        <div class="panel" id="panel1"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/701/310" /></div>
        <div class="panel" id="panel2"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/702/320" /></div>
        <div class="panel" id="panel3"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/703/330" /></div>
        <div class="panel" id="panel4"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/704/340" /></div>
        <div class="panel" id="panel5"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/705/350" /></div>
        <div class="panel" id="panel6"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/706/360" /></div>
        <div class="panel" id="panel7"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/707/370" /></div>
        <div class="panel" id="panel8"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/708/380" /></div>
        <div class="panel" id="panel9"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/709/390" /></div>
        <div class="panel" id="panel10"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/710/400" /></div>
        <div class="panel" id="panel11"><img src="_assets/images/loading.gif" data-src="http://placekitten.com/711/410" /></div>
    </section>
</div>
4

1 回答 1

3

$$()返回一个集合。当你真的想要一个元素时,你不应该克隆一个集合。

如果您的意思是通过 id 获取单个元素,请使用$("id");- if 通过选择器document.getElement('.class')(或任何node.getElement)。$ 不像 jquery 中的,它更像document.getElementById().

而且,element.inject(target)- 单个 el,而不是集合 - vstarget.adopt(element(s))

window.addEvent("domready",function(){
    var clone = document.getElement('.view').clone();
    /* Wait for images to be 100% loaded otherwise they might not have width/height */
    var lazyloader = new LazyLoad({
        onComplete: function(){
            setupWall();
            window.addEvent('resize:throttle(1000)', function(){
                /* Delete old viewport and add back with cloned version */
                var view = $$('.view');
                if(view.length){
                    wall = null;
                    view.destroy();
                }
                var cloneElement = $('wrapper').adopt(clone);
                console.log(cloneElement);
                //setupWall();
            });
        }
    });
});
于 2013-02-27T17:13:23.917 回答