js、jquery 图像载入效果 Image Loading Effect

Demo下载地址:

https://github.com/SunnyWoo/ImageLoadEffect

代码片段:

var imgList = [
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575926968307712/598c24ea85c265245.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575928688230400/598c24ea85f8e8217.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575930797707264/598c24ea8617e2486.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575933506277376/598c24ea863c51554.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575936371122176/598c24ea865279731.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575937634938880/598c24ea866901074.jpg",
				"http://56lvmbzh.droibaascdn.com/droi/56lvmbzhOSd5VOT1TNyu3OBXp8oVaHBnlQCN3ToB/895575939165601792/598c24ea867f08783.jpg"
			];
			
			function loadImage(src, callback) {
				var img = document.createElement("img");
				img.src = src;
				img.addEventListener("load", function() {
					if(this.complete) {
						callback();
					}
				});
			}
			
			var bindHtml = function() {
				var html = '';
				for(var i = 0; i < imgList.length; i++) {
					html += '';
				}
				$("#rowList").html(html);

				//循环判断所有的img
				$("#rowList img").each(function() {
					var _this = $(this);
					if(_this.attr("src") == undefined) {
						var _src = _this.attr("data-src");
						loadImage(_src, function() {
							//载入成功后执行 添加延迟 使用中可删除
							setTimeout(function() {
								_this.attr("src", _src);
								_this.parent().css("background-image", "none");
							}, 400);
						});
					}
				});
			};

			bindHtml();

你可能感兴趣的:(jQuery)