
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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

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
CakePHP Tutorial
1385
52



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

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.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.
