Automatic scaling and cropping of images based on JQuery_jquery
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 , 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:
/*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:
//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('');
}
}
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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Overview of advanced functions of how to use HTML, CSS and jQuery to implement image merge display: In web design, image display is an important link, and image merge display is one of the common techniques to improve page loading speed and enhance user experience. This article will introduce how to use HTML, CSS and jQuery to implement advanced functions of image merging and display, and provide specific code examples. 1. HTML layout: First, we need to create a container in HTML to display the merged images. You can use di

Apple's recent iPhones capture memories with crisp detail, saturation and brightness. But sometimes, you may encounter some issues that may cause the image to look less clear. While autofocus on iPhone cameras has come a long way, allowing you to take photos quickly, the camera can mistakenly focus on the wrong subject in certain situations, making the photo blurry in unwanted areas. If your photos on your iPhone look out of focus or lack sharpness overall, the following post should help you make them sharper. How to Make Pictures Clearer on iPhone [6 Methods] You can try using the native Photos app to clean up your photos. If you want more features and options

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

When we use word documents to edit files, sometimes there are many pages. We want to display them side by side and check the overall effect. However, because we don’t know how to operate, we often need to scroll for a long time to view page by page. I don’t know if you have ever encountered a similar situation. In fact, we can easily solve it at this time as long as we learn how to set the word zoom pages side by side. Below, let’s take a look and learn together. First, we create and open a new page in the Word document, and then enter some simple content to make it easier to distinguish. 2. For example, if we want to realize word zoom and side-by-side display, we need to find [View] in the menu bar, and then select [Multiple Pages] in the view tool options, as shown in the figure below: 3. Find [Multiple Pages] and click,

If you don't have control over the zoom level in Safari, getting things done can be tricky. So if Safari looks zoomed out, that might be a problem for you. Here are a few ways you can fix this minor zoom issue in Safari. 1. Cursor magnification: Select "Display" > "Cursor magnification" in the Safari menu bar. This will make the cursor more visible on the screen, making it easier to control. 2. Move the mouse: This may sound simple, but sometimes just moving the mouse to another location on the screen may automatically return it to normal size. 3. Use Keyboard Shortcuts Fix 1 – Reset Zoom Level You can control the zoom level directly from the Safari browser. Step 1 – When you are in Safari
