我想懒惰加载与knockoutjs绑定的图像。 我能够在没有knockoutjs框架的情况下延迟加载图像,但我不知道如何使用knockoutjs绑定来做到这一点。 这里是我的HTML懒惰加载图像与knockoutjs
<div class='liveExample'>
<div class="row">
<div data-bind="foreach: items">
<div class="col-lg-4 col-md-4 col-sm-4 " style="padding: 0px">
<img data-bind=" attr: { src: $data }" class="lazy" />
</div>
</div>
</div>
</div>
的Javascript:
var SimpleListModel = function(items) {
this.items = ko.observableArray(items);
bind(this); // Ensure that "this" is always this view model
};
var pictures = []; //Initialise an empty array
for (var i = 0; i = 10 ; i++) {
var image; //This is a placeholder
image = 'http://dummyimage.com/300x200/000/fff&text=' + i; //Set the src attribute (imageFiles[i] is the current filename in the loop)
pictures.push(image); //Append the new image into the pictures array
}
ko.applyBindings(new SimpleListModel(pictures));
这里是jsfiddle
哪里延迟加载实现的?我只是看到具有普通'src'属性的图像。 – pixelistik
你想要加载图像的时间? – TrueEddie