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 ...
随机推荐
- 使用NSData来加载文件
1. Loading Data from Files and URLs // Assuming that there is a text file at /Examples/Test.txt: NSS ...
- Codeforces Round #180 (Div. 2) C. Parity Game 数学
C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...
- C#程序通过模板自动创建Word文档
引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示 ...
- 在android market发布个人免费应用的步骤
写了一段时间的android应用了,只是在自己手机上面安装. 上周申请了android developer,需要一次性25美元的程序开发注册费用.费用需要用google checkout,所以还要先申 ...
- Android Compatibility package 兼容性开发套件
我们认为Android 3.0平板电脑操作系统在美国时间2011年2月22日的正式推出,对于Android手机应用程序开发者所象征的意涵是: 之前大家所开发过的Android手机应用,除了可以在And ...
- TP复习15
## ThinkPHP 3.1.2 URL#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.URL规则 1.默认是区分大小写的 2.如果我们不想区分大小 ...
- [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route
In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...
- direction:rtl demo
<style> body {font:14px/18px Consolas;} div { width:100px;padding:10px; margin:10px 0px 30px 0 ...
- oralce 函数 FOR windows 跟踪神器
https://github.com/dennis714 http://www.yurichev.com
- 基于sqlite的Qt 数据库封装
[代码] mydata.h 10 #ifndef MYDATA_H 11 #define MYDATA_H 12 #include <QObject> 13 #include <QS ...