JavaScript Learning notes One of the jquery writing pictures equal scaling and preload _jquery

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.