在我的页面上很多地方都使用了lazyload效果 插件网址http://www.appelsiini.net/projects/lazyload。 对于图像我添加了类"懒惰"和
<input type="button" onclick="fn()" >
<script type="text/javascript">
$(function() {
fn()
$("img.lazy").lazyload();
});
function fn(){
var img = document.createElement('img');
img.setAttribute('data-original','bg.jpg')
img.className='lazy'
document.body.appendChild(img);
}
</script>
但是在其他页面中,我用dinamic创建了图像&#34;懒惰&#34;但懒惰不起作用?怎么解决?
答案 0 :(得分:1)
检查 DEMO
HTML
<input type="button" onclick="fn()" value="load Image" >
JQUERY
function fn(){
var img = $('<img>');
img.attr('data-original','https://www.google.co.in/logos/doodles/2015/126th-anniversary-of-the-public-opening-of-the-eiffel-tower-4812727050567680-hp.jpg');
img.attr('class','lazy');
$(img).appendTo('body');
setTimeout(function(){
$("img.lazy").lazyload();
},100);
}
答案 1 :(得分:1)
只需使用lazySizes即可。这个lazyloader在开发时考虑了动态内容。无需通过在JS中的不同位置调用方法来触发事件或牺牲关注点分离。
只需使用:
<input type="button" onclick="fn()" value="add image">
<img data-src="http://lorempixel.com/400/200/" class="lazyload" />
<script>
function fn() {
var img = document.createElement('img');
img.setAttribute('data-src', 'http://lorempixel.com/400/200/')
img.className = 'lazyload';
document.body.appendChild(img);
}
fn();
</script>