博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片延迟 jquery lazyload.js
阅读量:6587 次
发布时间:2019-06-24

本文共 5350 字,大约阅读时间需要 17 分钟。

 

Lazy Load Plugin for jQuery

Lazy Load is a jQuery plugin written in JavaScript. It delays loading of images in long web pages. Images outside of viewport (visible part of web page) wont be loaded before user scrolls to them. This is  opposite of .

Using Lazy Load on long web pages containing many large images makes the page load faster. Browser will be in ready state after loading visible images. In some cases it can also help to reduce server load.

Lazy Load is inspired by Utility by Matt Mlinac.

          For those in hurry there are several demo pages available: , , , , , , and .         

When checking the demos clear browser cache between each request. You can check what is actually loaded with developers console (Chrome and Safari), FireBug (Firefox) or HTTPHeaders (IE).         

How to Use?

Lazy Load depends on . Include them both in end of your HTML code:

You must alter your HTML code. Put the place holder image into src attribute. Demo pages use 1×1 pixel grey gif. URL of the real image must be put into data-original attribute. It is good idea to give Lazy Loaded image a specific class. This way you can easily control which images plugin is binded to.

then in your code do:

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

This causes all images of class lazy to be lazy loaded. See the demo.

Fallback for Non JavaScript Browsers

Practically everyone has JavaScript enabled. However there are cases when you want to show the real images even if request come from client which does not support JavaScript. Google crawlers are one good candidate. Google crawlers do not execute JavaScript but also seem to ignore noscript content. To degrade gracefully when JavaScript is not enabled you can include the real image tag inside <noscript> block.

To prevent both placeholder and the real image showing at the same time hide the place holder with css.

.lazy {
display: none;}

For JavaScript enabled browser you must enable displaying the placeholders when documents loads. This can be done at the same time when initializing the plugin.

$("img.lazy").show().lazyload();

All this is optional can should be done only if you want the plugin to degrade gracefully.

Setting Sensitivity

By default images are loaded when they appear on the screen. If you want images to load earlier you can set threshold parameter. Setting threshold to 200 causes image to load 200 pixels before it is visible.

$("img.lazy").lazyload({
threshold : 200 });

Event to Trigger Loading

Event can be any jQuery event such as click or mouseover. You can also use your own custom events such as sporty or foobar. Default is to wait until user scrolls down and image appears on the window. To prevent all images to load until their placeholder image is clicked you could do:

$("img.lazy").lazyload({
event : "click"});

Using Effects

By default plugin waits for image to fully load and calls show() to show it. You can use any effect you want. Following code uses fadeIn effect. Check how it works at .

$("img.lazy").lazyload({
effect : "fadeIn"});

Images Inside Container

You can also use plugin for images inside scrolling container, such as div with scrollbar. Just pass the container as jQuery object. There is a demo for and container.

#container {
height: 600px; overflow: scroll;}
$("img.lazy").lazyload({
container: $("#container")});

When Images Are Not Sequential

After scrolling page Lazy Load loops though unloaded images. In loop it checks if image has become visible. By default loop is stopped when first image below the fold (not visible) is found. This is based on following assumption. Order of images on page is same as order of images in HTML code. With some layouts assumption this might be wrong. You can control loading behaviour with failure_limit option.

$("img.lazy").lazyload({
failure_limit : 10});

Setting failure_limit to 10 causes plugin to stop searching for images to load after finding 10 images below the fold. If you have a funky layout set this number to something high.

Delayed Loading of Images

Not exactly feature of Lazy Load but it is also possible to delay loading of images. Following code waits for page to finish loading (not only HTML but also any visible images). Five seconds after page is finished, below the fold images are loaded automatically. You can also check the .

$(function() {
$("img:below-the-fold").lazyload({
event : "sporty" });});$(window).bind("load", function() {
var timeout = setTimeout(function() {
$("img.lazy").trigger("sporty")}, 5000);});

Load Invisible Images

There are cases when you have invisible images. For example when using plugin in together with a large filterable list of items that can have their visibility state changed dynamically. To improve performance Lazy Load ignores hidden images by default. If you want to load hidden images set skip_invisible to false.

$("img.lazy").lazyload({
skip_invisible : false});

Download

Latest or . Plugin has been tested with Safari 5.1, Safari 6, Chrome 20, Firefox 12 on OSX and Chrome 20, IE 8 and IE 9 on Windows and Safari 5.1 on iOS 5 both iPhone and iPad.

When asking a question please include an URL to example page where the problem occurs. If you have longer code examples please use

 

 

地址:

转载于:https://www.cnblogs.com/hejunrex/archive/2012/08/17/2643923.html

你可能感兴趣的文章
TCP/IP协议族(一) HTTP简介、请求方法与响应状态码
查看>>
MVC View显示详解(RenderBody,RenderPage,RenderSection,Partial)
查看>>
kafka负载均衡相关资料收集(一)
查看>>
二进制数组
查看>>
C语言的工具集
查看>>
Android电池驱动【转】
查看>>
java-信息安全(八)-迪菲-赫尔曼(DH)密钥交换【不推荐,推荐Oakley】
查看>>
重写和重载
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2-新增锁定用户与解除锁定用户的功能...
查看>>
Win7 系统管理员设置了系统策略_禁止进行此安装_怎么办
查看>>
R语言环境变量的设置 环境设置函数为options()
查看>>
ELK学习总结(3-1)elk的基本查询
查看>>
do{...}while(0)的意义和用法
查看>>
微信小程序支付简单小结与梳理
查看>>
scikit-learn:4.2.3. Text feature extraction
查看>>
android 图片特效处理之光晕效果
查看>>
vue1.0 的过滤器
查看>>
如何删除anaconda
查看>>
关于https中的算法
查看>>
Unity3D - 资源管理
查看>>