search
HomeWeb Front-endHTML TutorialjQ handles images that are too large in the page_html/css_WEB-ITnose

This is a very practical function. It is inevitable that some oversized pictures will appear on the web page, which will stretch the page or partially hide the pictures. We usually use the max-width of CSS To control it, but ie6 doesn't like this. I encountered this kind of confusion when I was building a website. Because I was learning jQuery recently, I thought of using jq to deal with this problem. After some thinking, I feel that this problem is actually not difficult. Let’s explain it in detail below:



1. Idea:

To solve the size problem, we must first obtain The width and height of the picture, and then define a maximum width and make a judgment. If the actual width is greater than the set maximum width, then make the actual width equal to the maximum width, and the height can be reduced proportionally according to the aspect ratio. I organized my thoughts and listed the Chinese sentences:

1) Set the maximum width
2) Get the image width
3) Get the image height
4) Define the proportional relationship of the height ( New height = height/width * set width)
5) Determine, if width > set maximum width
6) then width = maximum width; height = new height
7) End



2. Code:

Write the above Chinese statement into a complete jQ statement:

$(function() {
$(' .content img').each(function() {
var maxWidth = 600;
var width = $(this).width();
var height = $(this).height();
var newHeight = height / width * maxWidth;
if(width > maxWidth) {
$(this).width(maxWidth);
$(this).height(newHeight);
}
});
});



The .content img here is the selector, which should be modified according to the actual web page structure, such as the following structure:


jQ handles images that are too large in the page_html/css_WEB-ITnose




3. Compatibility:



Doing compatibility testing is an essential step. After all, the browser varieties are too complex. I have tested it here under ie6, ie9, firefox, and chrome, and it is very good and successful!



But the thinking problem cannot be too limited, because the width and height of the image must first be obtained in the code , if the height and width are not defined, will it still be normal?



Chairman Mao taught us that true knowledge comes from practice! Of course, the doubts can only be known by actual testing. I added alert(width); in jq to see what the browser will tell us.



1) Height and width are empty: jQ handles images that are too large in the page_html/css_WEB-ITnose



In this case, the actual width obtained by ie6 and ie9 is 1, firefox obtains 800, and chrome obtains 0, which means that only firefox can run normally.



2) No height and width attributes: jQ handles images that are too large in the page_html/css_WEB-ITnose



In this case, The values ​​obtained by ie6, ie9 and firefox are all 800, and the values ​​obtained by chrome are 0, which means that ie9 and firefox are normal, but chrome has failed. Chrome, which has always been admired by others, is a bit disappointing.



From this point of view, when designing the website, the height and width attributes of the image should be added correctly before the previous jq can be used to control the size of the image.







Four. Function expansion:



Okay, the previous code can already Pictures that are too large are controlled, and then some expansions are made on this basis.

1), if the size is too large, add css class to the picture:

$(this).addClass("bigpic");

2), if the size is too large If the size is too large, set the css of the image:

$(this).css("cursor","pointer");

3), if the size is too large, set the attributes of the image:

$(this).attr("title","Click to enlarge");

4), if the size is too large, click it to open the original image in a new window:

$(this).click(function(){window.open(this.src)});

And so on...add more as needed.

Statement
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
How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment