博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
EF 5.0 帮助类
查看>>
微软BI 之SSIS 系列 - 通过设置 CheckPoints 检查点来增强 SSIS Package 流程的重用性...
查看>>
linux tomcat配置https
查看>>
1z0-052 q209_6
查看>>
TCP三次握手连接
查看>>
Spring.NET学习笔记——目录(原)
查看>>
Java回顾之Spring基础
查看>>
基本二叉搜索树的第K小元素
查看>>
产品需求文档写作方法(一)写前准备+梳理需求
查看>>
JAX-WS(三)构建简单webservice部署到tomcat上
查看>>
mac 设置 代理 上网 步骤 同时设置邮件代理
查看>>
ios 对日期的处理(包括计算昨天时间、明天时间)
查看>>
Java中的Set, List, Map漫谈
查看>>
JS图片自动或者手动滚动效果(支持left或者up)
查看>>
跟vczh看实例学编译原理——零:序言
查看>>
Spring帖子汇总
查看>>
Revit Family API 添加几何实体
查看>>
分享10个创新的扁平风格的简历页面设计
查看>>
elasticsearch 随笔
查看>>
SharePoint 2013 APP 开发示例 (二)获取用户信息
查看>>