Home Web Front-end JS Tutorial Automatic scaling and cropping of images based on JQuery_jquery

Automatic scaling and cropping of images based on JQuery_jquery

May 16, 2016 pm 05:01 PM
picture Zoom Crop

In fact, I have wanted to write an effect like this for a long time. As for the reason? Come in with this note, I believe you understand.
General portal websites are indispensable for displaying a large number of pictures. In order to make the website beautiful, the pictures come in various sizes. Professional website editors will process the pictures into equal proportions and then upload them, making the website very beautiful. It looks good, but unfortunately, I would say that 90% of the website editors I encounter are unprofessional.
In order to prevent the website editors from ruining my hard work, I decided to do something like this.

1. First, define the size of the image in CSS. If JS is not executed, you can see the stretched image, which is the most normal performance;
2. For each CSS that defines the image size Define an additional container that is not commonly used. Here I choose the italic tag <cite></cite>, and define its CSS to be exactly the same as the CSS of the same root img, and define the CSS style of the img in the container to return margin:0; padding:0;
This is what I did:

Copy code The code is as follows:

/*Public*/
cite{display: block;overflow:hidden;overflow:hidden !important;}

/*a container*/
#BigPic img{display:block;padding:2px;width:240px;height:160px;border: 1px solid #cccccc;}
#BigPic cite{display:block;padding:2px;width:240px;height:160px;border:1px solid #cccccc;}
#BigPic cite img{display:block;margin :0px;padding:0px;border:none;}

3. Define the image processing function, refer to the defined size and original size of the image, fill the position while maintaining the proportion, and then put it into the cropping container;
My code:

Copy code The code is as follows:

//Image size judgment and processing, surrounded by cropping container - By COoL
function cutImgz(obj){
var image=new Image();
image.src=obj.src;

$this=$(obj);
var iwidth=$this.width();//Get the image display width fixed in CSS
var iheight=$this.height();//Get the image display height fixed in CSS
if (1*image.width*iheight!=1*iwidth*image.height){
                                                                              using using using use using                                                           ‐                           out out out out out out out through out through out through through off     ’ s ’ through ’ s right through through out through out through out's' through out through out ‐ through‐to‐‐‐‐‐‐ light, to 1*image.width*iheight!=1*iwidth*image.height_to/ image.height>=iwidth/iheight){
                                                                                                                                                                                                              >                                                                                                                                                                                                                                                                     $this.width(iwidth 'px');           }

//Use cite to create a cropping effect
$this.wrap('<cite />');
}
}



4. When loading the page, traverse all the images to determine whether they are in the cache. If they are in the cache, they will be processed directly. If they are not in the cache, they will be processed by onload.
(
Because the cached images are processed within seconds load, it has been loaded before the onload event is defined, causing the onload event not to be triggered; if the image is not in the cache, if it is processed directly, the image is not loaded, and the original size will be regarded as an empty image by the Image object, and the width and height are both 0
)
My code:


Copy code

The code is as follows:

$('img').each(function() { var image=new Image(); image.src=this.src; if(image.complete){ //Exist in cache and process immediately
cutImgz(this ; });


Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area?

6 Ways to Make Pictures Sharper on iPhone 6 Ways to Make Pictures Sharper on iPhone Mar 04, 2024 pm 06:25 PM

6 Ways to Make Pictures Sharper on iPhone

Safari zoom issue on iPhone: Here's the fix Safari zoom issue on iPhone: Here's the fix Apr 20, 2024 am 08:08 AM

Safari zoom issue on iPhone: Here's the fix

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

How to make ppt pictures appear one by one

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader Mar 04, 2024 pm 05:49 PM

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader

How to use JavaScript to implement the drag and zoom function of images? How to use JavaScript to implement the drag and zoom function of images? Oct 27, 2023 am 09:39 AM

How to use JavaScript to implement the drag and zoom function of images?

How to zoom pages side by side in word How to zoom pages side by side in word Mar 19, 2024 pm 07:19 PM

How to zoom pages side by side in word

See all articles