lazyload ajax同步,Jquery lazyload with ajax

Shortly, the best optimized way to do this is to call the lazyload in your ajax's success function:

$.ajax({

url: "your url here",

type: "Post",

success: function(response){

// handle your success callback here

$("img.lazy").lazyload({

effect : "fadeIn"

});

}

});

But if you want to centralize your call to the lazyload plugin, you have 2 options:

First: (not recommended due to performance hit)

$(document).on('DOMNodeInserted', '#container', function() {

$("img.lazy").lazyload({

effect: 'fadeIn'

});

});

but note here the "#container", it is the container's selector where you will show your new loaded stuff, so the code above will listen inside this container for any newly added stuff to run the lazyload again on new images,

Second: (recommended)

Calling your custom lazyload by adding it to your JQuery:

$.fn.myLazyLoad = function() {

this.lazyload({

effect : "fadeIn"

});

};

then, call it in all your AJAX requests:

$.ajax({

url: "your url here",

type: "Post",

success: function(response){

// handle your success callback here

$("img.lazy").myLazyLoad();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery LazyLoad是一个非常实用的图片懒加载插件,可以在页面滚动到图片位置时再去加载图片,避免一次性加载过多的图片导致页面加载缓慢的问题,提高页面的加载速度和用户体验。以下是具体使用方法: 1. 引入jQueryLazyLoad插件 ```html <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script> ``` 2. 设置HTML结构 在HTML中,将需要懒加载的图片的src属性改为data-original属性,例如: ```html <img src="loading.gif" data-original="image.jpg" alt="example"> ``` 其中,loading.gif是一个占位符图片,用于在图片未加载完成前显示,避免页面布局变形。 3. 初始化LazyLoad插件 在JavaScript中,使用以下代码初始化LazyLoad插件: ```javascript $(function() { $("img.lazy").lazyload({ threshold: 200, effect: "fadeIn", placeholder: "loading.gif" }); }); ``` 其中,$("img.lazy")表示需要懒加载的图片,threshold表示图片距离可视区域多少像素时开始加载,effect表示图片加载效果,placeholder表示占位符图片。 4. 配置LazyLoad插件 除了上述配置外,还有其他一些配置可以进行设置,例如: ```javascript $(function() { $("img.lazy").lazyload({ threshold: 200, effect: "fadeIn", placeholder: "loading.gif", skip_invisible: false, failure_limit: 10, event: "click", container: $("body"), data_attribute: "original", appear: function() {}, load: function() {}, error: function() {} }); }); ``` 其中,skip_invisible表示是否跳过隐藏的图片,默认为true;failure_limit表示尝试加载图片的次数,默认为0;event表示触发加载的事件,默认为scroll;container表示滚动容器,默认为window;data_attribute表示需要加载的图片的属性,默认为data-original;appear表示图片出现在可视区域时的回调函数;load表示图片加载完成时的回调函数;error表示图片加载失败时的回调函数。 以上是jQuery LazyLoad的使用方法,可以根据具体情况进行配置和使用,提升页面的加载速度和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值