JavaScript Learning notes One of the jquery writing pictures equal scaling and preload
In the past for JavaScript is always used in the page to write a few functions, basically do not take into account the encapsulation and reuse of functions, a recent project may be a bit high in this respect, so the study of a similar jquery encapsulation.
Here's a look at the image scaling as well as the preload effect to try and write similar JavaScript code.
Images such as scaling and preload effects are as follows (the preload effect is sometimes not obvious):
JS main code is as follows:
Copy Code code as follows:
(function () {
var yquery = (function () {
var yquery = function () {
return YQuery.fn.init ();
};
Yquery.fn = Yquery.prototype = {
Init:function () {
return this;
},
Picture scaling and preload method declarations but it feels like this (return new Imgresizebox (e)) Very awkward please enlighten me
Imgresize:function (e) {
return new Imgresizebox (e);
}
};
Image Image Processing
var imgresizebox = function (e) {
Image parameter
setting = {
Imgid: "",//the ID of the container of the picture, such as. Viewarea img
height:0,
width:0,
Loading: "Images/lightbox-ico-loading.gif"
};
$.extend (setting, E, setting); Parameter substitution
var images = $ (setting.imgid); Get all pictures
$ (images). Hide (); Hide
var loading = new Image (); Pre-loading pictures
Loading.classname = "Loading";
LOADING.SRC = setting.loading;
$ (images). After (loading);
Pre-loading functions
var perloading = function ($this) {
var img = new Image ();
IMG.SRC = $this. src;
if (img.complete) {
Computeimg.call ($this);
Return
};
Img.onload = function () {
Computeimg.call ($this);
Img.onload = function () {};
};
};
Picture zoom processing, and picture display function
var computeimg = function () {
var m = this.height-setting.height;
var n = this.width-setting.width;
if (M > N)
This.height = this.height > setting.height? Setting.height:this.height;
Else
This.width = this.width > setting.width? Setting.width:this.width;
$ (this). Next (". Loading"). Remove ();
$ (this). Show ();
};
Loop call preload function
return $ (Images). each (function () {
Perloading (this);
});
}
return yquery;
})();
Window.yquery = window.$$ = Yquery ();
})();
The calling code is as follows:
Copy Code code as follows:
$ (document). Ready (function ()
{
$$.imgresize ({imgid: ". Viewarea img", height:160, width:270, loading: "http://www.jb51.net/images/2012/155618/ 2012062710243954.gif "});
});
Finally attach a simple source code: Jsdemo_jb51.rar