Authored by 姜枫

jquery-lazyload/1.1.5

{
"curly": true,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"noarg": true,
"quotmark": "double",
"trailing": true,
"undef": true,
"unused": true,
"node": true,
"jquery": true,
"browser": true
}
\ No newline at end of file
... ...
# Contributing to Lazy Load
## Only one feature or change per pull request
Make pull requests only one feature or change at the time. For example you have fixed a bug. You also have optimized some code. Optimization is not related to a bug. These should be submitted as separate pull requests. This way I can easily choose what to include. It is also easier to understand the code changes. Commit messages should be descriptive and full sentences.
Do not commit minified versions. Do not touch the version number. Make the pull requests against [1.9.x branch](https://github.com/tuupola/jquery_lazyload/commits/1.9.x).
## Write meaningful commit messages
Proper commit message is full sentence. It starts with capital letter but does not end with period. Headlines do not end with period. The GitHub default `Update filename.js` is not enough. When needed include also longer explanation what the commit does.
```
Capitalized, short (50 chars or less) summary
More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body. The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.
```
When in doubt see Tim Pope's blogpost [A Note About Git Commit Messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
## Follow the existing coding standards
When contributing to open source project it is polite to follow the original authors coding standars. They might be different than yours. It is not a holy war. Just follow then original.
```javascript
var snake_case = "something";
function camelCase(options) {
}
if (true !== false) {
console.log("here be dragons");
}
```
... ...
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
uglify : {
options: {
banner: "/*! Lazy Load <%= pkg.version %> - MIT license - Copyright 2010-2013 Mika Tuupola */\n"
},
target: {
files: {
"jquery.lazyload.min.js" : "jquery.lazyload.js",
"jquery.scrollstop.min.js" : "jquery.scrollstop.js"
}
}
},
watch: {
files: ["*.js", "!*.min.js" ,"test/spec/*Spec.js"],
tasks: ["test"],
},
jshint: {
files: ["*.js", "!*.min.js" ,"test/spec/*Spec.js"],
options: {
jshintrc: ".jshintrc"
}
},
jasmine: {
src: ["jquery.lazyload.js"],
options: {
helpers: "test/spec/*Helper.js",
specs: "test/spec/*Spec.js",
vendor: ["test/vendor/jquery-1.9.0.js", "test/vendor/jasmine-jquery.js"]
}
}
});
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-jasmine");
grunt.loadNpmTasks("grunt-contrib-watch");
//grunt.registerTask("test", ["jshint", "jasmine"]);
grunt.registerTask("test", ["jshint"]);
grunt.registerTask("default", ["test", "uglify"]);
};
\ No newline at end of file
... ...
# History
---
## 1.1.3
增加图片加载失败时的图片显示
USAGE:
lazyLoad({
try_again_css: 'xx'
});
如果提供了try_again_css,则使用自定义样式,否则使用默认样式
\ No newline at end of file
... ...
# yoho.lazyload [![spm version](http://spm.yoho.cn/badge/yoho.lazyload)](http://spm.yoho.cn/package/yoho.lazyload)
---
yoho lazyload
## Install
```
$ spm install yoho.lazyload --save
```
## Usage
lazyload对原代码做了一层封装,默认配置参数:
{
effect : 'fadeIn',
effect_speed: 10,
placeholder: 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==',
skip_invisible: false
}
```js
var Lazyload = require('yoho.lazyload');
// use Lazyload
//接受2个参数,为指定的$imgs添加lazyload并可配置需要的参数(需要的时候覆盖默认参数项);若图片参数不传则为$('img.lazy')添加lazyload效果
lazyLoad([$imgs, configs]);
//如果你想用原来的方法加载lazyload
require('yoho.lazylaod/lib/jquery.lazyload');
```
//点击重新下载图片时,阻止默认行为,防止页面发生跳转。add by liangzhifeng at 2015.11.4
// 超时时间改为5秒。modify by liangzhifeng at 2015.11.6
// 超时图片要加上原来的样式。modify by liangzhifeng at 2015.12.15
\ No newline at end of file
... ...
{
"name": "jquery_lazyload",
"version": "1.9.3",
"homepage": "http://www.appelsiini.net/projects/lazyload",
"authors": [
"Mika Tuupola <tuupola@appelsiini.net>"
],
"description": "jQuery plugin for lazy loading images",
"main": [
"jquery.lazyload.js",
"jquery.scrollstop.js"
],
"license": "MIT",
"ignore": [
"**/.*",
"**/*.min.js",
"**/*.html",
"**/*.textile",
"Gruntfile.js",
"lazyload.jquery.json",
"package.json",
"node_modules",
"bower_components",
"test",
"img"
]
}
... ...
/**
* lazyload
* @author: xuqi(qi.xu@yoho.cn)
* @date: 2015/6/25
*/
var $ = require('jquery');
require('./lib/jquery.lazyload.js');
/**
* 为指定imgs添加lazyload效果,未指定imgs则为所有img.lazy添加lazyload效果
* @params imgs lazyload的图片
* @params options lazyload效果选项
*/
module.exports = function(imgs, options) {
var setting = {
effect : 'fadeIn',
effect_speed: 10,
placeholder: 'data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==',
skip_invisible: false
}, $imgs, argsLength = arguments.length;
//分解参数
(function seperateOptions() {
switch (argsLength) {
case 0:
$imgs = $('img.lazy');
break;
case 1:
if (imgs instanceof $) {
//img
$imgs = imgs;
} else {
$imgs = $('img.lazy');
$.extend(setting, imgs);
}
break;
case 2:
$imgs = imgs;
setting = $.extend(setting, options);
break;
}
}());
$imgs.lazyload(setting);
};
\ No newline at end of file
... ...
(function($, window, document, undefined) {
var $window = $(window);
var webp = false;
function check_webp_feature(feature, callback) {
var kTestImages = {
lossy: "UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA",
lossless: "UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==",
alpha: "UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==",
animation: "UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA"
};
var img = new Image();
img.onload = function () {
var result = (img.width > 0) && (img.height > 0);
callback(feature, result);
};
img.onerror = function () {
callback(feature, false);
};
img.src = "data:image/webp;base64," + kTestImages[feature];
}
setTimeout(function(){
check_webp_feature('lossy', function(feature, result) {
webp = result;
});
}, 50);
function img_url_format(imgUrl, opts) {
if (!imgUrl) {
return imgUrl;
}
var splits = imgUrl.split('?');
var url = splits[0];
var query = splits[1] || '';
if (query.indexOf('imageView2') === 0) {
if (query.indexOf('/q/') < 0 && !!opts.q) {
query += '/q/' + opts.q;
}
if (query.indexOf('/format/') < 0 && !!opts.format) {
query += '/format/' + opts.format;
}
return imgUrl + '?' + query;
} else if (query.indexOf('imageMogr2') === 0) {
if (query.indexOf('/quality/') < 0 && !!opts.q) {
query += '/quality/' + opts.q;
}
if (query.indexOf('/format/') < 0 && !!opts.format) {
query += '/format/' + opts.format;
}
return imgUrl + '?' + query;
} else if (query === '') {
var imageUrl = 'imageView2/2/interlace/1/q/' + opts.q;
if (opts.format) {
imageUrl += '/format/' + opts.format;
}
return url + '?' +imageUrl;
} else {
return imgUrl;
}
}
$.fn.lazyload = function(options) {
var elements = this;
var $container;
var settings = {
threshold : 0,
failure_limit : 0,
event : "scroll",
effect : "show",
container : window,
data_attribute : "original",
skip_invisible : true,
appear : null,
load : null,
yasPath: '/web/',
yasImgDomain: 'http://analytics.yhurl.com/yas.gif',
placeholder : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC",
try_again_css: null
};
function update() {
var counter = 0;
elements.each(function() {
var $this = $(this);
if (settings.skip_invisible && !$this.is(":visible")) {
return;
}
if ($.abovethetop(this, settings) ||
$.leftofbegin(this, settings)) {
/* Nothing. */
} else if (!$.belowthefold(this, settings) &&
!$.rightoffold(this, settings)) {
$this.trigger("appear");
/* if we found an image we'll load, reset the counter */
counter = 0;
} else {
if (++counter > settings.failure_limit) {
return false;
}
}
});
};
//add by jp, 发送图片方式跨域发送消息
function crossSend(param, callback) {
var image = new Image(1, 1);
image.src = settings.yasImgDomain + settings.yasPath + '?' + param;
image.onload = function() {
image.onload = null;
if (callback) {
callback();
}
};
};
if(options) {
/* Maintain BC for a couple of versions. */
if (undefined !== options.failurelimit) {
options.failure_limit = options.failurelimit;
delete options.failurelimit;
}
if (undefined !== options.effectspeed) {
options.effect_speed = options.effectspeed;
delete options.effectspeed;
}
$.extend(settings, options);
}
/* Cache container as jQuery as object. */
$container = (settings.container === undefined ||
settings.container === window) ? $window : $(settings.container);
/* Fire one scroll event per scroll. Not one scroll event per image. */
if (0 === settings.event.indexOf("scroll")) {
$container.bind(settings.event, function() {
return update();
});
}
this.each(function() {
var self = this;
var $self = $(self);
var original = $self.attr("data-" + settings.data_attribute);
original = img_url_format(original, {q: 75, format: (webp ? 'webp': '')});
setTimeout(function(){
var preImg = new Image();
preImg.src = original;
}, 100);
self.loaded = false;
/* If no src attribute given use data:uri. */
if ($self.attr("src") === undefined || $self.attr("src") === false || $self.attr("src") === '') {
if ($self.is("img")) {
$self.attr("src", settings.placeholder);
}
}
/* When appear is triggered load original image. */
$self.one("appear", function() {
//add by jp
if($self.timerid) clearTimeout($self.timerid);
//console.log("img appear, this.loaded:" + this.loaded + ", id:" + $self.attr("id"));
if (!this.loaded) {
if (settings.appear) {
var elements_left = elements.length;
settings.appear.call(self, elements_left, settings);
}
$("<img />")
//add by jp
.bind("error abort", function(){
$self.attr("src",settings.placeholder);
loadfail({info:'error'});
})
.bind("load", function() {
//add by jp, 加载超时后,不再进行加载,通过传统方式设置src,边下载边渲染
if($self.loadtimeout){
return;
}
//add by jp, 清理检测图片下载超时定时器
clearTimeout($self.timerid);
//add by jp, 上报图片加载成功事件
if( $self.attr("pfmrpt") ){
//crossImgSend('success');
}
$self.hide();
if ($self.is("img")) {
$self.attr("src", original);
} else {
$self.css("background-image", "url('" + original + "')");
}
$self[settings.effect](settings.effect_speed);
self.loaded = true;
/* Remove image from array so it is not looped next time. */
var temp = $.grep(elements, function(element) {
return !element.loaded;
});
elements = $(temp);
if (settings.load) {
var elements_left = elements.length;
settings.load.call(self, elements_left, settings);
}
})
.attr("src", $self.attr("data-" + settings.data_attribute));
//add by jp , 临时保存图片url
$self.src = $self.attr("data-" + settings.data_attribute);
//add by jp , 上报图片开始加载事件
if( $self.attr("pfmrpt") && _yas && _yas.add){
//crossImgSend(,'start');
}
//add by jp, 图片加载失败或超时的处理
var loadtimeout = function(options){
//记录标识图片加载超时
$self.loadtimeout = true;
//上报图片加载超时事件
if( $self.attr("pfmrpt") && _yas && _yas.add){
options.info ; //error or timeout
//crossImgSend(settings.yasImgDomain,settings.yasPath,'fail');
}
//清除图片src取值,便于图片边下载边渲染
$self.attr("src","");
setTimeout(function(){
$self.attr("src",$self.src);
},50);
};
//add by jp, 图片加载失败或超时的处理
var loadfail = function(options){
//记录标识图片加载超时
$self.loadtimeout = true;
//上报图片加载失败事件
if( $self.attr("pfmrpt") && _yas && _yas.add){
options.info ; //error or timeout
//crossImgSend(settings.yasImgDomain,settings.yasPath,'fail');
}
var width = $self.width();
var height = $self.height();
var $tryagain = $("<div class="+settings.try_again_css+" style='background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC) ;text-align: center;position:relative;width:"+width+"px;height:"+height+"px; line-height:"+height+"px'>图片打开失败,点击重新加载</div>");
// $tryagain.css("background-image",settings.placeholder);
$self.replaceWith($tryagain);
$tryagain.bind("click", function(e){
e.preventDefault();
$tryagain.replaceWith($self);
//清除图片src取值,便于图片边下载边渲染
$self.attr("src","");
setTimeout(function(){
$self.attr("src",$self.src);
},50);
});
};
//图片下载超时检测
$self.timerid = setTimeout(loadtimeout,5000);
}
});
/* When wanted event is triggered load original image */
/* by triggering appear. */
if (0 !== settings.event.indexOf("scroll")) {
$self.bind(settings.event, function() {
if (!self.loaded) {
$self.trigger("appear");
}
});
}
});
/* Check if something appears when window is resized. */
$window.bind("resize", function() {
update();
});
/* With IOS5 force loading images when navigating with back button. */
/* Non optimal workaround. */
if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) {
$window.bind("pageshow", function(event) {
if (event.originalEvent && event.originalEvent.persisted) {
elements.each(function() {
$(this).trigger("appear");
});
}
});
}
/* Force initial check if images should appear. */
$(document).ready(function() {
update();
});
return this;
};
/* Convenience methods in jQuery namespace. */
/* Use as $.belowthefold(element, {threshold : 100, container : window}) */
$.belowthefold = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
} else {
fold = $(settings.container).offset().top + $(settings.container).height();
}
return fold <= $(element).offset().top - settings.threshold;
};
$.rightoffold = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.width() + $window.scrollLeft();
} else {
fold = $(settings.container).offset().left + $(settings.container).width();
}
return fold <= $(element).offset().left - settings.threshold;
};
$.abovethetop = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.scrollTop();
} else {
fold = $(settings.container).offset().top;
}
return fold >= $(element).offset().top + settings.threshold + $(element).height();
};
$.leftofbegin = function(element, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = $window.scrollLeft();
} else {
fold = $(settings.container).offset().left;
}
return fold >= $(element).offset().left + settings.threshold + $(element).width();
};
$.inviewport = function(element, settings) {
return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
!$.belowthefold(element, settings) && !$.abovethetop(element, settings);
};
})($, window, document);
... ...
{
"name": "yoho.lazyload",
"version": "1.1.5",
"engines": {
"node": ">= 0.8.0"
},
"description": "yoho lazyload, encased with version 1.9.3 of jquery.lazyload with jquery 2.1.4",
"author": "Mika Tuupola <tuupola@appelsiini.net>",
"repository": {
"type": "git",
"url": "git@github.com:YOHO-LAB/jquery_lazyload.git"
},
"licenses": "MIT",
"spm": {
"main": "lazyload.js",
"dependencies": {
"jquery": "2.1.4"
},
"devDependencies": {
"expect.js": "0.3.1"
}
},
"scripts": {
"test": "grunt test",
"test": "spm test",
"build": "spm build"
},
"devDependencies": {
"spm": "3",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-uglify": "~0.2.4",
"grunt-contrib-jasmine": "~0.5.2",
"grunt-contrib-watch": "~0.5.3"
}
}
... ...