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 ...
随机推荐
- Sql中的union和union all的讲解
SQL UNION 和 UNION ALL操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相 ...
- 一份Java学习路线图
Java学习路线图 本文会持续更新,敬请关注.下划线的文字会在这个博客继续更新. 回顾自己的Java学习经历,从学校的课堂到现在每天的项目开发,一份路线路线图浮出来. 其中有未做好的地方,也有自我感觉 ...
- 大礼包!ANDROID内存优化(大汇总)
写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上把网上搜集的各种内存零散知识点进行汇总.挑选.简化后整理而成. 所以我将本文定义为一个工具类的文章,如果你在A ...
- HDU 4876 ZCC loves cards(暴力剪枝)
HDU 4876 ZCC loves cards 题目链接 题意:给定一些卡片,每一个卡片上有数字,如今选k个卡片,绕成一个环,每次能够再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个 ...
- PS拾色器(前景色背景色)快捷键
快捷键 I 是拾色器 X 是前后色切换
- Creating Your Own Server: The Socket API, Part 1
转:http://www.linuxforu.com/2011/08/creating-your-own-server-the-socket-api-part-1/ By Pankaj Tanwar ...
- Maven项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
刚刚新建完Maven项目,一般都会报这个错误,原因是没有默认添加需要的javax.servelet的jar包,所以打开pom.xml文件添加如下dependency即可: <dependency ...
- 如何删除Weblogic域
1. delete entry in WL_HOME/common/nodemanager/nodemanager.domains 2. delete entry in FMW_HOME/domain ...
- C语言中和指针相关的四道题目
例子1. void fun (int *x , int *y) { printf("%d, %d", *x, *y) ; *x = 3; *y = 4;} main(){ int ...
- 【开源项目13】Volley框架 以及 设置request超时时间
Volley提供了优美的框架,使android程序网络访问更容易.更快. Volley抽象实现了底层的HTTP Client库,我们不需关注HTTP Client细节,专注于写出更加漂亮.干净的RES ...