第一次自己写jquery图片延迟加载插件,不通用,但修改一下还是可以使用到很多页面上的
不断修改完善中……
/*!
* jquery.lazyoading.js
*自定义的页面图片延迟加载插件,比网上的jquery.lazyload简单,也更适合自己的网站
*使用方法:
把img 的class加上 lazyloading
然后先引用jquery,再引用jquery.lazyoading.js,再调用:$("img.lazyloading").lazyloading({loadfirst:true});
* by pukuimin
* 2013-11-01
*2013-11-08 解决了图片没有指定高度的问题
*2013-11-14 解决了没有指定高度加载图片之后有间隔的问题
*/
/// <reference path="jquery-1.8.2.min.js" />
(function ($) {
$.fn.lazyloading = function (options) {
var defaults = {
preyimg: "/Content/images/Imgpreview/grey.gif",
picpath: "data-original",
container: $(window),
loadfirst: false, //进入页面后是否加载当前页面的图片
defaultHeightID: "lazyloadingHeight"//页面上默认高度的input标签id
//imgPaddingID: "lazyloadingPadding"//img的padding值
};
var params = $.extend({}, defaults, options || {});
params.cache = [];
$(this).each(function () {
var node = this.nodeName.toLowerCase(), url = $(this).attr(params["picpath"]), preyimg = params["preyimg"];
var defaultheight = $("#" + params["defaultHeightID"]).val(); //, padding = $("#" + params["imgPaddingID"]).val(); //
//重组
var data = {
obj: $(this),
tag: node,
url: url,
preyimg: preyimg,
defaultheight: defaultheight
};
params.cache.push(data);
}); var init = function () {
$.each(params.cache, function (i, data) {
var thisImg = data.obj, tag = data.tag, url = data.url, preyimg = data.preyimg;
if (typeof (url) != "undefined")// 判断是否需要延迟加载
{
var parent1 = thisImg.parent(); //a
var Inner = null; //
if (parent1.is("a") == true) {//img wrap by a
Inner = parent1;
}
else {
Inner = thisImg;
}
var width = thisImg.attr("width") || thisImg.css("width");
var height = data.defaultheight || thisImg.css("height");
//if (i == 0) alert(data.defaultheight);
var attrheight = thisImg.attr("height");
if (attrheight != null) height = attrheight;
if (width != null && width.indexOf("px") > -1) width.replace("px", "");
if (height != null && height.indexOf("px") > -1) height.replace("px", "");
var divstr = "<div class='.loading' style='text-align: left;position:relative;float:left;width:" + width + "px;";
var HasHeight = true; //图片是否指定了高度
divstr = divstr + "height:" + height + "px;";
if (attrheight == null || attrheight == "") {
HasHeight = false;
} thisImg.css("position", "relative"); divstr = divstr + "' ></div>"
//修正外层div:text-align的影响
Inner.wrap(divstr);
//修正img外面不是a标签时parent()已经改变的问题
parent1 = thisImg.parent();
if (HasHeight == true) { parent1.attr("lazyloading_hasheight", "1"); } //是否指定了高度
else { { parent1.attr("lazyloading_hasheight", "0"); } } parent1.append("<img class='loadhiddenimg' width='0' height='0' src='' />");
thisImg.attr("src", preyimg);
thisImg.removeAttr("width").removeAttr("height");
thisImg.attr("width1", width).attr("height1", attrheight); ////thisImg.attr("width", "50px").attr("height", "50px"); //loading图大小
//thisImg.css("margin", "0 auto");
thisImg.css("margin", ((height / 2) - 25) + "px auto auto " + ((width / 2) - 25) + "px");
$(".lazyloading").css("display", "table"); //.css("position", "relative");
}
});
}
//动态显示数据
var loading = function () {
//窗口的高度+看不见的顶部的高度=屏幕低部距离最顶部的高度
var thisButtomTop = parseInt($(window).height()) + parseInt($(window).scrollTop());
var thisTop = parseInt($(window).scrollTop()); //屏幕顶部距离最顶部的高度 $.each(params.cache, function (i, data) {
var thisImg = data.obj, tag = data.tag, url = data.url, post, posb; if (thisImg) {//对象不为空
if (typeof (url) != "undefined") {// 判断是否需要延迟加载
var PictureTop = parseInt(thisImg.offset().top);
//如果处理可见范围内,并且原图地址data-original不等于src,则加载图片
if (PictureTop >= thisTop && PictureTop <= thisButtomTop && thisImg.attr("data-original") != thisImg.attr("src")) {
var hiddenImg = thisImg.siblings("img.loadhiddenimg"); hiddenImg.load(function () { //隐藏图片加载完之后的回调函数
var width = thisImg.attr("width1");
var height = thisImg.attr("height1");
thisImg.attr("width", width).attr("height", height);
thisImg.removeAttr("width1").removeAttr("height1");
thisImg.css("margin", "0 auto");
if (thisImg.parent().attr("lazyloading_hasheight") == "0") {//没有指定高度时,加载图片后去掉div高度自适应
if (thisImg.parent().is("a") == true) {
thisImg.parent().parent().css("height", "");
}
else {
thisImg.parent().css("height", "");
}
}
thisImg.load(function () {
if (thisImg.parent().is("a") == true) {
thisImg.parent().parent().css("height", thisImg.height());
}
else {
thisImg.parent().css("height", thisImg.height());
}
});
thisImg.attr("src", hiddenImg.attr("src"));
}).error(function () {
thisImg.attr("src", hiddenImg.attr("src")); //alert("error");
});
hiddenImg.attr("src", url);
}
}
}
});
};
//初始化
init();
//事件触发
//加载完毕即执行
if (params["loadfirst"] == true) loading();
//滚动执行
params.container.bind("scroll", loading).bind("resize", loading);
};
})(jQuery);
查看效果:http://architecture.kinpan.com/
第一次自己写jquery图片延迟加载插件,不通用,但修改一下还是可以使用到很多页面上的的更多相关文章
- jQuery图片延迟加载插件jQuery.lazyload
插件描述:jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度.在某些情况下,它也能帮助减轻服务器负载. 使用方法 引用jquery和jquery.lazy ...
- JQuery图片延迟加载插件,动态获取图片长宽尺寸
以前的网站带宽小,没有特别多的大图,现在不同了,各种图片网站如同雨后春笋层出不穷.服务器是抗住了,但是客户端就有意见了,太多的图片必然导致页面加载缓慢,特别是有些table结构的站点更是如此.能否让图 ...
- jQuery图片延迟加载插件
在一些图片较多的页面上,如果图片都一起加载网页的速度会比较慢,而且也浪费流量. 使用图片延时加载插件就解决这些问题. 方法: 引入jquery和插件文件 <script src="jq ...
- jQuery图片延迟加载插件jQuery.lazyload 的使用
使用方法 引用jquery和jquery.lazyload.js到你的页面 1 2 <script src="jquery-1.11.0.min.js"></sc ...
- jQuery图片延迟加载插件jquery.lazyload.js
在实际的项目开发中,我们通常会遇见这样的场景:一个页面有很多图片,而首屏出现的图片大概就一两张,那么我们还要一次性把所有图片都加载出来吗?显然这是愚蠢的,不仅影响页面渲染速度,还浪费带宽.这也就是们通 ...
- jQuery图片延迟加载插件:jquery.lazyload
----------------------------------------------------------------------------------------------- clas ...
- jQuery图片延迟加载插件jQuery.lazyload使用方法(转)
使用方法 1.引用jquery和jquery.lazyload.js到你的页面 <script src="jquery-1.11.0.min.js"></scri ...
- 图片延时加载原理 和 使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
图片加载技术分为:图片预加载和图片延时加载. javascript图片预加载和延时加载的区别主要体现在图片传输到客户端的时机上,都是为了提升用户体验的,延时加载又叫懒加载.两种技术的本质:两者的行为是 ...
- 推荐几款jquery图片切换插件
一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...
随机推荐
- 创建并追加img元素(jquery!)
有几种方法 但都需要你指定一个节点 根据这个节点进行添加 如现有一节点Id为pr:一,向该节点内部后方添加:1 $("#pr").append("<img src= ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- yarn container启动失败
在yarn资源管理的集群上运行spark程序,无法读取的数据多与少,都会报这个错误,但是其他程序在集群上能够正常运行. 16/11/14 00:13:44 WARN cluster.YarnSched ...
- ZOJ 3705 Applications 模拟
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include< ...
- Nodejs Express下引入本地文件的方法
Express的结构如下: |---node_modules------用于安装本地模块. |---public------------用于存放用户可以下载到的文件,比如图片.脚本文件.样式表 ...
- [erlang]supervisor(监控树)的重启策略
1. init函数 init() -> {ok, {SupFlags, [ChildSpec,...]}} | ignore. [ChildSpec,...] 是在init之后默认要启动的子进程 ...
- ios 的touch事件分析
IOS之触摸事件和手势 13.1 事件概述 13.2 触摸事件 13.3 手势 13.1 事件概述 事件是当用户手指触击屏幕及在屏幕上移动时,系统不断发送给应用程序的对象. 系统将事件按照特定的路 ...
- android六大框架
-LinearLayout线性布局 垂直排序,每行仅包含一个界面元素 水平排序,每列仅包含一个界面元素 orientation,Layout-weight,Layout-margin(外边距,与屏幕) ...
- javascript 日期转换为中文
function CNDateString(date) { var cn = ["〇","一","二","三",&quo ...
- 封装ios静态库碰到的一些问题(三)
静态库封装好以后,就存在一个问题,静态库,模拟器的静态库何真机的静态库是分开的,那么能够合并,答案是肯定的,但是必须我们手工在终端工具下执行命令合并 lipo -create Release-ipho ...