
Home
Web Front-end
HTML Tutorial
jQ handles images that are too large in the page_html/css_WEB-ITnose



jQ 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:

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:

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:

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 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 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
What's New in Windows 11 KB5054979 & How to Fix Update Issues
4 weeks ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
3 weeks ago
By DDD
InZoi: How To Apply To School And University
4 weeks ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
3 weeks ago
By DDD
Where to find the Site Office Key in Atomfall
4 weeks ago
By DDD

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

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...
