Home  >  Article  >  Web Front-end  >  JQuery image lazy loading and proportional scaling plug-in_jquery

JQuery image lazy loading and proportional scaling plug-in_jquery

WBOY
WBOYOriginal
2016-05-16 18:42:10841browse

I’ve been learning JS OOP recently, so I wrote something like this
How to use:
$(".viewArea img").zoom({height:74,width:103});
Effect demonstration:
http://demo.jb51.net/html/jquery_img/jquery_img.htm
Code:

Copy code The code is as follows:

(function($){
$.fn.zoom = function(settings){
//Some default configurations;
settings = $.extend({
height:0,
width:0,
loading:"lightbox-ico-loading.gif"
},settings);
var images = this ;
$(images).hide();
var loading = new Image();
loading.className="loading"
loading.src = settings.loading;
$( images).after(loading);
//Preloading
var preLoad = function($this){
var img = new Image();
img.src = $this.src;
if (img.complete) {
processImg.call($this);
return;
}
//$this.src = loading.src;//will cause an error The size of
img.onload = function(){
//$this.src = this.src; //will cause the wrong size to be obtained
processImg.call($this);
img .onload=function(){};
}
}
//Calculate image size;
function processImg(){
//if(settings.height===0|| settings.width ===0) return;
var m = this.height-settings.height;
var n = this.width - settings.width;
if(m>n)
this.height = this.height>settings.height ? settings.height :
this.height;
else
this.width = this.width >settings.width ? settings.width :
this.width;
$(this).next(".loadding").remove()
$(this).show();
}
return $(images).each( function(){
preLoad(this);
});
}
})(jQuery);

The effect is like this:
JQuery image lazy loading and proportional scaling plug-in_jquery
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn