Home Web Front-end JS Tutorial Use javascript to solve image scaling and optimization code_image special effects

Use javascript to solve image scaling and optimization code_image special effects

May 16, 2016 pm 05:53 PM
Image zoom

A client contacted me and told me that the website he had just built was not displaying properly. I immediately became nervous. This was the first project I had completed independently, so I quickly opened his website and looked at it. I didn’t see anything abnormal. Come. I asked him again why it was abnormal, and he said it was different from the effect when handing over the project. Haha, if it was abnormal during the handover, the project would definitely not be handed over. I might as well ask him to take a screenshot. Sure enough, it was abnormal. It was a picture he had just uploaded that opened the window displaying the content. Looking at the code, it is clear that max-width has been written, so why does this situation still occur? Suddenly I found that the browser in the picture he sent looked unpleasant, like the ancient and sacred IE6! After confirmation, it was indeed it, and it was it again! I was really negligent and handed over the work without testing it under IE6.

Speaking of IE6, those of us who are doing front-end work are really annoyed and helpless because many attributes are not supported. But many users are still using it, and we cannot ignore it. For the sake of compatibility, I had to ask my colleagues for advice, and then used js scaling to display images for IE6 and made some detailed optimizations.

Although today’s client website is relatively small and does not have high requirements for website performance, I consider that these things may be used in the future. Optimization is a long-term learning process. I checked some articles and said If you use the onload method of the img tag to call a function to modify the size, it will have a greater impact on performance. In addition, using -expression behavior seems not advisable (will continue to verify it later), so in the end it is triggered when the page is loaded.

Copy code The code is as follows:

function resizeImage(img,width){
var image=new Image();
image.src=img.src;
var temp = image.width;
img.width = temp = (temp>width)?width:temp;
img.style.display = "inline";
}
function doResize(){
if($.browser.version==6&&$.browser.msie) $("img").each( function(){resizeImage(this,100)});
}
window.onload = doResize;

This code uses the resizeImage function to determine and modify the img.width attribute. The principle is relatively simple. In front of this code, I also added a judgment for the IE6 browser. In addition to it, I still let CSS determine the size of the image. I did not define the height and width of the image in the code, so when scaling, I only need to modify one of the values ​​and let the other value adapt, instead of letting js perform equal-ratio scaling. This is also a little bit of optimization. Bar. I quoted Jquery in the code (because I'm used to it). You don't need to do this. The reason why I wrote it this way is that after the DOM element is returned, the function can be processed uniformly. Considering that customers often use images from external links, at this time, the display speed of the web page is affected by the image source, so when the page is loaded, I first use CSS to hide the images that need to be displayed, and then use the js method after the image is scaled. To display images, I made another hack for IE6:
Copy the code The code is as follows:

img{
display:inline !important;
display:none;
max-width:180px;
}
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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 implement image thumbnail function in JavaScript? How to implement image thumbnail function in JavaScript? Oct 25, 2023 am 08:56 AM

How to implement image thumbnail function in JavaScript? When we display images on a web page, we sometimes need to reduce the original large image to meet the layout requirements of the page, which requires the use of the image thumbnail function. In JavaScript, we can implement the thumbnail function of the image through the following methods: Use HTML to directly set the width and height of the image. The simplest way is to directly set the width and height attributes of the image in HTML to achieve the thumbnail effect. For example:&l

How to use CSS to achieve the zoom effect of images How to use CSS to achieve the zoom effect of images Nov 21, 2023 pm 04:17 PM

How to use CSS to achieve the zoom effect of images In web design, the zoom effect of images is one of the common requirements. Through the related properties and techniques of CSS, we can easily achieve the zoom effect of images. Below, we will introduce in detail how to use CSS to achieve the zoom effect of images, and give specific code examples. Use the transform attribute to implement matrix scaling of images. The transform attribute allows us to transform elements by rotating, scaling, tilting or translating them. Among them, the scaling transformation is to realize the picture

How to implement image scaling and magnifying glass effects in Vue? How to implement image scaling and magnifying glass effects in Vue? Jun 25, 2023 pm 07:32 PM

How to implement image scaling and magnifying glass effects in Vue? With the continuous development of Web technology, users have increasingly higher requirements for the display effects of images on websites. Among them, image zooming and magnifying glass effects are relatively common requirements. It is relatively simple to implement image scaling and magnifying glass effects in Vue. Next, I will introduce the specific implementation method in detail. 1. Basic method First, let us take a look at how to achieve the basic image scaling effect. The implementation method is simple, just use Vue’s built-in instructions

Best way to achieve image scaling using PHP and GD library Best way to achieve image scaling using PHP and GD library Jul 12, 2023 pm 08:07 PM

The best way to achieve image scaling using PHP and GD libraries. In recent years, with the popularity of the Internet, image processing has become one of the necessary functions for many sites. As one of the most common requirements in image processing, image scaling needs to be able to scale the image size proportionally without losing image quality to adapt to different display needs. As a common server-side programming language, PHP has a wealth of image processing libraries, the most commonly used of which is the GD library. The GD library provides a simple yet powerful interface that can be used to handle various image operations.

How to use PHP to develop simple image scaling and cropping functions How to use PHP to develop simple image scaling and cropping functions Sep 21, 2023 am 10:28 AM

How to use PHP to develop simple image scaling and cropping functions Summary: Image processing is a common requirement in web development. This article will introduce how to use PHP to develop simple image scaling and cropping functions and provide specific code examples. By studying this article, readers can understand how to use PHP to implement basic image processing functions in Web applications. 1. Background introduction In web development, sometimes we need to scale or crop images to adapt to different page layouts or meet specific needs. PHP as

How to use HTML, CSS and jQuery to implement advanced image zoom functions How to use HTML, CSS and jQuery to implement advanced image zoom functions Oct 25, 2023 am 09:07 AM

How to use HTML, CSS and jQuery to implement the advanced function of image scaling Introduction: In modern web design, the beauty and adaptability of images are very important. However, conventional picture display often cannot meet our needs. In this article, we will introduce how to use HTML, CSS and jQuery to implement some advanced image zoom functions. Through these technologies, we can achieve customized image scaling effects and add more interactivity to our web pages. Step 1: HTML Markup First, we need a

Tips for implementing image scaling using CSS Positions layout Tips for implementing image scaling using CSS Positions layout Sep 26, 2023 pm 02:17 PM

CSSPositions layout techniques for image scaling In web design, image scaling is one of the common requirements. Through CSSPositions layout, we can achieve the zoom effect of images and add a better visual experience to the web page. This article will introduce some techniques and give specific code examples. Use the position attribute to set the position of an image: In CSS, you can use the position attribute to define how an element is positioned. By setting the position attribute to "re

Tips and methods to use CSS to achieve image bubble effects Tips and methods to use CSS to achieve image bubble effects Oct 18, 2023 pm 12:24 PM

Tips and methods for using CSS to achieve image bubble effects In web design, adding special effects to images is one of the important means to improve user experience. Among them, picture bubble effects can add interest and interactivity to pictures, making web content more attractive. This article will share some tips and methods for using CSS to achieve image bubble effects, with specific code examples. Use pseudo-class elements to create bubble effects By using CSS pseudo-class elements, we can add a bubble effect above the image. The specific method is to set pseudo classifiers

See all articles