Image-related operations (re-painting, obtaining sizes, resizing, and scaling) based on jquery _ jquery

Source: Internet
Author: User
This article describes how to perform image operations based on jquery, including image re-painting, image size acquisition, image resizing, and image scaling, interested friends can refer to this article to share with you four common operations on jquery images for your reference. the specific content is as follows:

1. you can redraw the image size on the server or on the client using JQuery.

$(window).bind("load", function() {   // IMAGE RESIZE   $('#product_cat_list img').each(function() {     var maxWidth = 120;     var maxHeight = 120;     var ratio = 0;     var width = $(this).width();     var height = $(this).height();      if(width > maxWidth){      ratio = maxWidth / width;      $(this).css("width", maxWidth);      $(this).css("height", height * ratio);      height = height * ratio;     }     var width = $(this).width();     var height = $(this).height();     if(height > maxHeight){      ratio = maxHeight / height;      $(this).css("height", maxHeight);      $(this).css("width", width * ratio);      width = width * ratio;     }   });   //$("#contentpage img").show();   // IMAGE RESIZE});

2. jQuery's method of obtaining the actual image size

$ (Function () {var imgSrc = $ ("# image "). attr ("src"); getImageWidth (imgSrc, function (w, h) {console. log ({width: w, height: h}) ;}); function getImageWidth (url, callback) {var img = new Image (); img. src = url; // if the image is cached, the cached data if (img. complete) {callback (img. width, img. height);} else {// Fully Loaded event img. onload = function () {callback (img. width, img. height );}}}

3. jquery automatically adjusts the image size.

$ (Document ). ready (function () {$ ('IMG '). each (function () {var maxWidth = 500; // maximum image width var maxHeight = 500; // maximum image height var ratio = 0; // zoom ratio var width = $ (this ). width (); // The actual width of the image var height = $ (this ). height (); // actual image height // check whether the image is too wide if (width> maxWidth) {ratio = maxWidth/width; // calculate the zoom ratio (this).css ("width ", maxWidth); // set the actual display width height = height * ratio; // calculate the scaled height (this).css ("height", height ); // set the height after proportional scaling} // check whether the image is super high if (height> maxHeight) {ratio = maxHeight/height; // calculate the zoom ratio (this%.css ("height ", maxHeight); // set the actual display height width = width * ratio; // calculate the scaled height (this).css ("width", width ); // set the height after proportional scaling }});});

4. use jQuery to resize the image

$(window).bind("load", function() {  // IMAGE RESIZE  $('#product_cat_list img').each(function() {    var maxWidth = 120;    var maxHeight = 120;    var ratio = 0;    var width = $(this).width();    var height = $(this).height();     if(width > maxWidth){      ratio = maxWidth / width;      $(this).css("width", maxWidth);      $(this).css("height", height * ratio);      height = height * ratio;    }    var width = $(this).width();    var height = $(this).height();    if(height > maxHeight){      ratio = maxHeight / height;      $(this).css("height", maxHeight);      $(this).css("width", width * ratio);      width = width * ratio;    }  });  //$("#contentpage img").show();  // IMAGE RESIZE});

The above is all the content in this article. it helps you achieve image re-painting, image size acquisition, image resizing, and image scaling.

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.