第一次自己写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之后做成图片集,而我选择利 ...
随机推荐
- Java Calendar 注意事项
Java JDK 提供了java.util.Calendar来处理日期和时间.Calendar是一个抽象类,是所有日历的模板,因此我们可以继承Calendar来实现其他的历法(比如阴历). Java提 ...
- SharePreference 工具类封装
import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...
- ueditor不自动加P解决方法
百度的Ueditor编辑器出于安全考虑; 用户在html模式下粘贴进去的html文档会自动被去除样式和转义. 虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all. ...
- 推荐大家使用的CSS书写规范、顺序
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...
- OA及权限系统
一直想找一款适合自己的权限管理后台,始终都没找到合适的,决定自己写一个 开发环境:vs2012 ,sql2008 语言:C# 前端:ligurui,jquery ORM框架:EF6.0 先来晒下我的数 ...
- 域名dns查询_查询域名dns ip地址
最近有部分用户反应管理的天气网站打开偏慢,决定从每一个可以出现的问题点查起!首先就是dns! 通过360dns监控对比发现,同一组域名,15tianqi.cn的dns响应时间比较长,在300-700间 ...
- 在本地创建angular-ui/bootstrap项目
在本地创建完整的angular-ui/Bootstrap项目 git clone the repo, then switch to the tag you want,then use grunt bu ...
- 第三章 EnumUtil根据值获取枚举对象
项目中使用枚举类的好处这里不再赘述,在使用枚举值时,通常需要根据值来获取枚举对象,下面介绍两种实现方案: 1.在枚举类中定义方法实现 首先给出如下性别枚举类: public enum SexEnum ...
- JAVA可移植性广泛应用
一.JAVA作为一种编程语言:源代码可移植性 作为一种编程语言,JAVA提供了一种最简单同时也是人们最熟悉的可移植性–源代码移植.这意味着任意一个JAVA程序,不论它运行在何种CPU.操作系统或JAV ...
- 原子操作--ARM架构
说明:内核版本号为3.10.101 一.ARM架构中的原子操作实现 在原子操作(一)中我们已经提到,各个架构组织为“复仇者”联盟,统一了基本的原子变量操作,这里我们就拿atomic_dec(v)来看看 ...