


Implementation principle and js code of proportionally displaying pictures on web pages_javascript skills
On dynamic websites, you often need to upload your own pictures, and the size of these pictures is unknown. When displayed as thumbnails, they must be scaled proportionally to display them beautifully. Take the recent golf website (http://www.changligolfsales.com) as an example.
This website needs to upload golf product images and display them as thumbnails on the list. The site server supports Asp, but does not support thumbnail generation components such as aspjpeg, so the uploaded images are directly displayed as thumbnails. , you need to scale it proportionally. The premise is to get the length and width of the picture. The first method that comes to mind is to read the length and width information of the picture through the ADODB.STREAM object when uploading, save it in the database, and read it when the page is generated. Take it out and calculate the ratio. The obvious disadvantage of this method is that each picture needs to be read and calculated on the server, which consumes more resources and increases the page opening delay.
The second method uses Javascript to transfer the calculation to the client.
The principle is to use Javascript on the client to read the size of each image and scale it after the page is loaded (onload trigger).
//Scale the imageDest image proportionally to fit Displayed in the area of width W and height H
function ResizeImage(imageDest, W, H)
{
//Display box width W, height H
var image = new Image();
image.src = imageDest.src;
if(image.width>0 && image.height>0)
{
//Compare aspect ratio
if(image.width/image. height >= W/H)//Relative display frame: width > height
{
if(image.width > W) //The width is greater than the display frame width W, the height should be compressed
{
imageDest.width = W;
imageDest.height = (image.height*W)/image.width;
}
else //The width is less than or equal to the display box width W, and the picture is completely Display
{
imageDest.width = image.width;
imageDest.height = image.height;
}
}
else//Similarly
{
if(image.height > H)
{
imageDest.height = H;
imageDest.width = (image.width*H)/image.height;
}
else
{
imageDest.width = image.width;
imageDest.height = image.height;
}
}
}
}
The above function scales the image.
The id of each thumbnail on the golf website is set to imgProductItem, such as:
Add a batch operation function:
// Scale all images with specified ids in the page proportionally
function RsizeAllImageById(id, W, H)
{
var imgs = document.getElementsByTagName("img");
for(var i= 0; i
if(imgs[i].id == id)
{
ResizeImage(imgs[i], W, H);
}
}
}
In this way, add
; add in the head area:
will display all images as thumbnails.

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



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 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? 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

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

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