JavaScript之延迟加载
本文参阅http://www.appelsiini.net/projects/lazyload
Javascript Lazyload延迟加载特效,有效降低HTPP连接次数,提高首屏加载时间
1、增加了图片预加载可选
2、修改了图片本身就在可视范围的时候直接显示而不需要滚动条触发
3、修改了Splice删除数组的时候,会跳过下一张图片BUG
4、浏览器窗口resize的时候图片出现也会加载
5、判断图片父层包裹顶部或者底部出现在可视范围内即可显示图片
我们来看看官方的介绍,我拷贝过来并且翻译了一部分:
Lazy Load is delays loading of images in long web pages. Images outside of viewport are not loaded until user scrolls to them. This is opposite of image preloading.
Using Lazy Load on long web pages will make the page load faster. In some cases it can also help to reduce server load.
Plugin is inspired by YUI ImageLoader Utility by Matt Mlinac.
延迟加载在长网页图片的延迟加载。图像窗口不加载,直到用户以外的卷轴。这是相反的图像预加载。
在长时间使用延迟加载web页面将使页面加载速度更快。在某些情况下,它也可以帮助减少服务器负载。
插件是由马特Mlinac受YUI ImageLoader效用。
For those in hurry there are several demo pages: basic options, with fadein effect, noscript fallback, horizontal scrolling, horizontal scrolling inside container, vertical scrolling inside container, page with gazillion images, load images using timeout and load images using AJAX(H).
When checking the demos clear browser cache between each request. You can check what is actually loaded with developers console (Chrome, Safari and IE) or FireBug (Firefox).
这里点有几个演示页:基本选项,与渐显效果,noscript撤退,水平滚动,水平滚动容器内,容器内垂直滚动,与无数图片页面,使用AJAX加载图像使用超时和加载图像(H)。
当检查每个请求之间的演示清除浏览器缓存。您可以检查实际装载与开发人员控制台(Safari和Chrome IE)或FireBug(Firefox)。
Lazy Load depends on Jquery. Include them both in end of your HTML code:
延迟加载取决于Jquery。包括他们在结束你的HTML代码:
<script src="jquery.js"></script>
<script src="jquery.lazyload.js"></script>
You must alter your image tags. Address of the image must be put into data-originalattribute. Give lazy loaded images a specific class. This way you can easily control which images plugin is binded to.
你必须改变你的图像标记。图像的地址必须放入data-original属性。给懒加载图像一个特定的类。这样你可以很容易地控制哪些图片插件绑定。
<img class="lazy" data-original="img/example.jpg" width="640" height="480">
$(function() {
$("img.lazy").lazyload();
});
Setting Threshold
设置阈值
By default images are loaded when they appear on the screen. If you want images to load earlier use threshold parameter. Setting threshold to 200 causes image to load 200 pixels before it appears on viewport.
默认加载图片时出现在屏幕上。如果你想要图片加载之前使用阈值参数。阈值设置为200年导致图像加载之前出现在视窗200像素。
$("img.lazy").lazyload({
threshold : 200
});
Event to Trigger Loading
事件触发加载
You can use jQuery event such as click or mouseover. You can also use custom events such as sporty or foobar. Default is to wait until user scrolls down and image appears on the viewport. To load images only when user clicks them you could do:
您可以使用jQuery点击或鼠标悬停等事件。您还可以使用定制的事件,如运动或foobar。默认是等到用户向下滚动和形象出现在视窗。加载图片只有当用户点击他们你可以:
$("img.lazy").lazyload({
event : "click"
});
Using Effects
使用效果
By default plugin waits for image to fully load and calls show(). You can use any effect you want. Following code uses fadeIn effect. Check how it works at effect demo p
默认插件等待形象完全加载和调用显示()。你可以使用任何你想要的效果。下面的代码使用渐显效果。这是演示页面。
$("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 horizonta and vertical container.
您还可以使用插件的图片滚动容器内部,比如div滚动条。只是通过jQuery对象的容器。有一个演示卧式锻机和垂直容器。
#container {
height: 600px;
overflow: scroll;
}
$("img.lazy").lazyload({
container: $("#container")
});
When Images Are Not Sequential
当图像并不是连续的
After scrolling page plugin loops though unloaded images. Loop checks if image has become visible. By default loop is stopped when first image outside viewport 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 setting.
滚动页面插件后循环虽然卸载图片。循环检查图片是否在可视区域。默认时停止循环以外的首家形象窗口。这是基于以下的假设。图片在页面的次序是HTML代码相同图片的顺序。与一些布局中,这样的假设是错误的。你可以控制failure_limit设置加载行为。
$("img.lazy").lazyload({
failure_limit : 10
});
Dealing With Invisible Images
处理不可见的图像
There are cases when you have images which are in viewport but not :visible. To improve performance you can ignore .not(":visible") images.
在某些情况下当你有图片的窗口而不是:可见。以提高性能可以忽略自身之外(":可见")图像。
$("img.lazy").lazyload({
skip_invisible : true
});
JavaScript之延迟加载的更多相关文章
- Echo.js – 简单易用的 JavaScript 图片延迟加载插件
Echo.js 是一个独立的延迟加载图片的 JavaScript 插件.Echo.js 不依赖第三方库,压缩后不到1KB大小. 延迟加载是提高网页首屏显示速度的一种很有效的方法,当图片元素进入窗口可视 ...
- ECHO.js 纯javascript轻量级延迟加载
演示 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf ...
- 第一百五十六节,封装库--JavaScript,延迟加载
封装库--JavaScript,延迟加载 延迟加载的好处,就是在浏览器视窗外的图片,不加载,拖动鼠标到浏览器视窗内后加载,用户不看的图片就不用加载,可以减少服务器大量流量 将图片的src地址用一张统一 ...
- javascript图片延迟加载(转载)
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 简单的JavaScript图像延迟加载库Echo.js
插件描述:和 Lazy Load 一样,Echo.js 也是一个用于图像延迟加载 JavaScript.不同的是 Lazy Load 是基于 jQuery 的插件,而 Echo.js 不依赖于 jQu ...
- 简单的Javascript图片延迟加载库Echo.js
简介: 和 Lazy Load 一样,Echo.js 也是一个用于图像延迟加载 JavaScript.不同的是 Lazy Load 是基于 jQuery 的插件,而 Echo.js 不依赖于 jQue ...
- JavaScript脚本延迟加载的方式有哪些?
延迟加载就是等页面加载完成之后再加载 JavaScript 文件. js 延迟加载有助于提高页面加载速度. 一般有以下几种方式: defer 属性: 给 js 脚本添加 defer 属性,这个属性会让 ...
- javascript 图片延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Javascript 图片延迟加载之理论基础
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
随机推荐
- PostgreSQL的注释嵌套的例子
pgsql=# -- Multiline comments pgsql=# SELECT 'Multi' /* This comment extends across pgsql*# * number ...
- ISA中的WEB链
在ISA Server 2004中提供了Web链功能,它就相当于将ISA Server配置为二级代理,可以将你的请求转发到上游的代理服务器或其他站点.使用Web链,你就可以实现条件路由,对不同的目的地 ...
- C#中简单调用MD5方法以及MD5简介
MD5简介: MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2.M ...
- 关于android 图像格式问题
这算是篇总结吧.6月份开始做的一个android上的ar项目结束了.我做的部分是二维码识别和图像识别的预处理.这个项目虽然很累,但是让我学到了很多东西,特别是严格的编码规则,和java代码的效率优化, ...
- python中import失败解决的简单办法
例如:import pkg_resources失败 可以print sys.path查看,从其他机器上cp -r过来即可,如下例子: 从另外一个正常的机器上scp过来/usr/ali/lib/pyth ...
- Eclipse使用jre的原理与配置
近期要配置Eclipse环境,Mark当中的一些方法. 下载Eclipse SDK之后我们就要关联JRE,由于Eclipse启动须要JRE. Eclipse启动时寻找JRE的顺序: 1.假设eclip ...
- Delphi和JAVA用UTF-8编码进行Socket通信例子
最近的项目(Delphi开发),需要经常和java语言开发的系统进行数据交互(Socket通信方式),数据编码约定采用UTF-8编码. 令我无语的是:JAVA系统那边反映说,Delphi发的数据他们收 ...
- Redis缓存服务搭建及实现数据读写
发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached.Redis.mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下.由于小弟水 ...
- window.onload与$.ready的差别
在做图书管理系统的时候.实用到window.onload(){}方法.可是遇到了一个问题.就是怎么都不运行,究竟是为什么呢?愁了半天.后来经师姐指点改用了$.ready(){}. 在我的浅浅的了解中觉 ...
- google对js延迟加载方案的建议
浏览器在执行JavaScript代码时会停止处理页面,当页面中有很多JavaScript文件或代码要加载时,将导致严重的延迟.尽管可以使用defer.异步或将JavaScript代码放到页面底部来延迟 ...