不断修改完善中……

  1. /*!
  2. * jquery.lazyoading.js
  3. *自定义的页面图片延迟加载插件,比网上的jquery.lazyload简单,也更适合自己的网站
  4. *使用方法:
  5. 把img 的class加上 lazyloading
  6. 然后先引用jquery,再引用jquery.lazyoading.js,再调用:$("img.lazyloading").lazyloading({loadfirst:true});
  7. * by pukuimin
  8. * 2013-11-01
  9. *2013-11-08 解决了图片没有指定高度的问题
  10. *2013-11-14 解决了没有指定高度加载图片之后有间隔的问题
  11. */
  12. /// <reference path="jquery-1.8.2.min.js" />
  13. (function ($) {
  14. $.fn.lazyloading = function (options) {
  15. var defaults = {
  16. preyimg: "/Content/images/Imgpreview/grey.gif",
  17. picpath: "data-original",
  18. container: $(window),
  19. loadfirst: false, //进入页面后是否加载当前页面的图片
  20. defaultHeightID: "lazyloadingHeight"//页面上默认高度的input标签id
  21. //imgPaddingID: "lazyloadingPadding"//img的padding值
  22. };
  23. var params = $.extend({}, defaults, options || {});
  24. params.cache = [];
  25. $(this).each(function () {
  26. var node = this.nodeName.toLowerCase(), url = $(this).attr(params["picpath"]), preyimg = params["preyimg"];
  27. var defaultheight = $("#" + params["defaultHeightID"]).val(); //, padding = $("#" + params["imgPaddingID"]).val(); //
  28. //重组
  29. var data = {
  30. obj: $(this),
  31. tag: node,
  32. url: url,
  33. preyimg: preyimg,
  34. defaultheight: defaultheight
  35. };
  36. params.cache.push(data);
  37. });
  38.  
  39. var init = function () {
  40. $.each(params.cache, function (i, data) {
  41. var thisImg = data.obj, tag = data.tag, url = data.url, preyimg = data.preyimg;
  42. if (typeof (url) != "undefined")// 判断是否需要延迟加载
  43. {
  44. var parent1 = thisImg.parent(); //a
  45. var Inner = null; //
  46. if (parent1.is("a") == true) {//img wrap by a
  47. Inner = parent1;
  48. }
  49. else {
  50. Inner = thisImg;
  51. }
  52. var width = thisImg.attr("width") || thisImg.css("width");
  53. var height = data.defaultheight || thisImg.css("height");
  54. //if (i == 0) alert(data.defaultheight);
  55. var attrheight = thisImg.attr("height");
  56. if (attrheight != null) height = attrheight;
  57. if (width != null && width.indexOf("px") > -1) width.replace("px", "");
  58. if (height != null && height.indexOf("px") > -1) height.replace("px", "");
  59. var divstr = "<div class='.loading' style='text-align: left;position:relative;float:left;width:" + width + "px;";
  60. var HasHeight = true; //图片是否指定了高度
  61. divstr = divstr + "height:" + height + "px;";
  62. if (attrheight == null || attrheight == "") {
  63. HasHeight = false;
  64. }
  65.  
  66. thisImg.css("position", "relative");
  67.  
  68. divstr = divstr + "' ></div>"
  69. //修正外层div:text-align的影响
  70. Inner.wrap(divstr);
  71. //修正img外面不是a标签时parent()已经改变的问题
  72. parent1 = thisImg.parent();
  73. if (HasHeight == true) { parent1.attr("lazyloading_hasheight", "1"); } //是否指定了高度
  74. else { { parent1.attr("lazyloading_hasheight", "0"); } }
  75.  
  76. parent1.append("<img class='loadhiddenimg' width='0' height='0' src='' />");
  77. thisImg.attr("src", preyimg);
  78. thisImg.removeAttr("width").removeAttr("height");
  79. thisImg.attr("width1", width).attr("height1", attrheight);
  80.  
  81. ////thisImg.attr("width", "50px").attr("height", "50px"); //loading图大小
  82. //thisImg.css("margin", "0 auto");
  83. thisImg.css("margin", ((height / 2) - 25) + "px auto auto " + ((width / 2) - 25) + "px");
  84. $(".lazyloading").css("display", "table"); //.css("position", "relative");
  85. }
  86. });
  87. }
  88. //动态显示数据
  89. var loading = function () {
  90. //窗口的高度+看不见的顶部的高度=屏幕低部距离最顶部的高度
  91. var thisButtomTop = parseInt($(window).height()) + parseInt($(window).scrollTop());
  92. var thisTop = parseInt($(window).scrollTop()); //屏幕顶部距离最顶部的高度
  93.  
  94. $.each(params.cache, function (i, data) {
  95. var thisImg = data.obj, tag = data.tag, url = data.url, post, posb;
  96.  
  97. if (thisImg) {//对象不为空
  98. if (typeof (url) != "undefined") {// 判断是否需要延迟加载
  99. var PictureTop = parseInt(thisImg.offset().top);
  100. //如果处理可见范围内,并且原图地址data-original不等于src,则加载图片
  101. if (PictureTop >= thisTop && PictureTop <= thisButtomTop && thisImg.attr("data-original") != thisImg.attr("src")) {
  102. var hiddenImg = thisImg.siblings("img.loadhiddenimg");
  103.  
  104. hiddenImg.load(function () { //隐藏图片加载完之后的回调函数
  105. var width = thisImg.attr("width1");
  106. var height = thisImg.attr("height1");
  107. thisImg.attr("width", width).attr("height", height);
  108. thisImg.removeAttr("width1").removeAttr("height1");
  109. thisImg.css("margin", "0 auto");
  110. if (thisImg.parent().attr("lazyloading_hasheight") == "0") {//没有指定高度时,加载图片后去掉div高度自适应
  111. if (thisImg.parent().is("a") == true) {
  112. thisImg.parent().parent().css("height", "");
  113. }
  114. else {
  115. thisImg.parent().css("height", "");
  116. }
  117. }
  118. thisImg.load(function () {
  119. if (thisImg.parent().is("a") == true) {
  120. thisImg.parent().parent().css("height", thisImg.height());
  121. }
  122. else {
  123. thisImg.parent().css("height", thisImg.height());
  124. }
  125. });
  126. thisImg.attr("src", hiddenImg.attr("src"));
  127. }).error(function () {
  128. thisImg.attr("src", hiddenImg.attr("src")); //alert("error");
  129. });
  130. hiddenImg.attr("src", url);
  131. }
  132. }
  133. }
  134. });
  135. };
  136. //初始化
  137. init();
  138. //事件触发
  139. //加载完毕即执行
  140. if (params["loadfirst"] == true) loading();
  141. //滚动执行
  142. params.container.bind("scroll", loading).bind("resize", loading);
  143. };
  144. })(jQuery);

查看效果:http://architecture.kinpan.com/

第一次自己写jquery图片延迟加载插件,不通用,但修改一下还是可以使用到很多页面上的的更多相关文章

  1. jQuery图片延迟加载插件jQuery.lazyload

      插件描述:jQuery图片延迟加载插件jQuery.lazyload,使用延迟加载在可提高网页下载速度.在某些情况下,它也能帮助减轻服务器负载. 使用方法 引用jquery和jquery.lazy ...

  2. JQuery图片延迟加载插件,动态获取图片长宽尺寸

    以前的网站带宽小,没有特别多的大图,现在不同了,各种图片网站如同雨后春笋层出不穷.服务器是抗住了,但是客户端就有意见了,太多的图片必然导致页面加载缓慢,特别是有些table结构的站点更是如此.能否让图 ...

  3. jQuery图片延迟加载插件

    在一些图片较多的页面上,如果图片都一起加载网页的速度会比较慢,而且也浪费流量. 使用图片延时加载插件就解决这些问题. 方法: 引入jquery和插件文件 <script src="jq ...

  4. jQuery图片延迟加载插件jQuery.lazyload 的使用

    使用方法 引用jquery和jquery.lazyload.js到你的页面 1 2 <script src="jquery-1.11.0.min.js"></sc ...

  5. jQuery图片延迟加载插件jquery.lazyload.js

    在实际的项目开发中,我们通常会遇见这样的场景:一个页面有很多图片,而首屏出现的图片大概就一两张,那么我们还要一次性把所有图片都加载出来吗?显然这是愚蠢的,不仅影响页面渲染速度,还浪费带宽.这也就是们通 ...

  6. jQuery图片延迟加载插件:jquery.lazyload

    ----------------------------------------------------------------------------------------------- clas ...

  7. jQuery图片延迟加载插件jQuery.lazyload使用方法(转)

    使用方法 1.引用jquery和jquery.lazyload.js到你的页面 <script src="jquery-1.11.0.min.js"></scri ...

  8. 图片延时加载原理 和 使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)

    图片加载技术分为:图片预加载和图片延时加载. javascript图片预加载和延时加载的区别主要体现在图片传输到客户端的时机上,都是为了提升用户体验的,延时加载又叫懒加载.两种技术的本质:两者的行为是 ...

  9. 推荐几款jquery图片切换插件

    一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...

随机推荐

  1. Java Calendar 注意事项

    Java JDK 提供了java.util.Calendar来处理日期和时间.Calendar是一个抽象类,是所有日历的模板,因此我们可以继承Calendar来实现其他的历法(比如阴历). Java提 ...

  2. SharePreference 工具类封装

    import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...

  3. ueditor不自动加P解决方法

    百度的Ueditor编辑器出于安全考虑; 用户在html模式下粘贴进去的html文档会自动被去除样式和转义. 虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all. ...

  4. 推荐大家使用的CSS书写规范、顺序

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...

  5. OA及权限系统

    一直想找一款适合自己的权限管理后台,始终都没找到合适的,决定自己写一个 开发环境:vs2012 ,sql2008 语言:C# 前端:ligurui,jquery ORM框架:EF6.0 先来晒下我的数 ...

  6. 域名dns查询_查询域名dns ip地址

    最近有部分用户反应管理的天气网站打开偏慢,决定从每一个可以出现的问题点查起!首先就是dns! 通过360dns监控对比发现,同一组域名,15tianqi.cn的dns响应时间比较长,在300-700间 ...

  7. 在本地创建angular-ui/bootstrap项目

    在本地创建完整的angular-ui/Bootstrap项目 git clone the repo, then switch to the tag you want,then use grunt bu ...

  8. 第三章 EnumUtil根据值获取枚举对象

    项目中使用枚举类的好处这里不再赘述,在使用枚举值时,通常需要根据值来获取枚举对象,下面介绍两种实现方案: 1.在枚举类中定义方法实现 首先给出如下性别枚举类: public enum SexEnum ...

  9. JAVA可移植性广泛应用

    一.JAVA作为一种编程语言:源代码可移植性 作为一种编程语言,JAVA提供了一种最简单同时也是人们最熟悉的可移植性–源代码移植.这意味着任意一个JAVA程序,不论它运行在何种CPU.操作系统或JAV ...

  10. 原子操作--ARM架构

    说明:内核版本号为3.10.101 一.ARM架构中的原子操作实现 在原子操作(一)中我们已经提到,各个架构组织为“复仇者”联盟,统一了基本的原子变量操作,这里我们就拿atomic_dec(v)来看看 ...