


Detailed explanation of how JavaScript uses three methods to modify image size
As we all know, colorful Web network courseware is inseparable from the support of images. While images increase the vividness of web pages, they also increase the size of web pages and slow down the download speed. How to make a picture display on the page in an appropriate size on a website is a question worth studying. To this end, the author has written down some of his own experiences for the reference of script writers, so as to encourage each other.
Requirements analysis: This website uses a news management system. In the news column on the homepage, image files need to be called from the database as pictures in the picture news, thus forming a text wrapping form. According to layout needs, the width of the image cannot exceed 200px.
The problem of allowing images to be displayed at appropriate sizes is essentially a problem of scaling down large images in equal proportions. How to obtain the size (width, height) of the image through the URL of the image is the key to the problem. Assume that k=Width/Height represents the proportion value of the image. When K>=1, as long as the width does not exceed 200px, the height must be <= 200px; on the contrary, K<1, as long as the Height does not exceed 150Px, the width must be <=150px (under normal circumstances, width:height=4:3). So as long as K>=1 limits the width, K<1 limits the height.
Option 1: Use the Image() object of javascript to dynamically load the image and obtain the height and width of the image.
<scriptlanguage="JavaScript" type="text/javascript"> <!--varimgObj;function checkImg(theURL){var width,height;var k;imgObj =new Image(); //创建对象; imgObj.src = theURL; if (typeof(imgObj) =="object"){if ((imgObj.width != 0) && (imgObj.height != 0)){width=imgObj.width; //是否已取得了图像的宽度; height=imgObj.height; //是否已取得了图像的高度; k=width/height; //获取图像比例值; if(k>=1){ if (width>=200){ width=200; height=width/k; }} else {if (height>=200){ height=200; width=k?height; } } showimg(theURL,width,height); } else setTimeout("checkImg('" + theURL + "')", 100) //通过Image对象动态装载图片,不可能立即得到图片的宽度和高度,所以每隔100毫秒重复调用检查。 } } functionshowimg(theURL,x,y){document.write("<imgsrc="+theURL+""+"width="+x+""+"height="+y+"border='0'"+""+"align='left'>");} //--> </script> <scriptlanguage="javascript">checkImg("Bt0085.jpg"); </script>
Result: The code was transplanted to the home page file (default.asp), and then a problem occurred when it passed the server test. When entering the URL, the processed image first appears in the browser. Click "Back" on the toolbar to display the homepage content. Tested again and the result was normal. The same phenomenon will occur every time the picture is updated, or the same problem will occur every time a computer that has not visited this website is opened.
Cause analysis: The characteristics of the Image() object are mainly used to achieve the image rolling effect. The images can be downloaded to the client in advance, so that there is no time delay in switching between images. As in the code below, use the src attribute of the Image object to specify the path (URL) of the image, so that the image pic2.gif in the images directory is downloaded to the client. var myImg = newImage();myImg.src = "pic.gif";
This code will force the browser to start downloading the specified image from the server, if there is this image in the client's cache (Cache) , the browser will automatically overwrite it. That way, when the user moves the mouse over the picture, the picture will change immediately without any time delay. So if the picture is displayed for the first time, the homepage file can be displayed normally the second time. This is the reason. From this, I came to the conclusion that the Image() object cannot be used to obtain the properties of the image, and the solution to the problem should be changed.
Option 2: Use the ID in to define the width and height of the image
The element of the page can define its display range, that is, the height and width of the image (Width). When the processing event is triggered, the effect can be achieved by dynamically changing the two properties of the graph.
<script> functionshow() {var w,h; var k; var con; w=smallslot.width; h=smallslot.height; k=w/h; if(k>=1){ if (w>=200){ w=200; h=w/k; }} else {if(h>=150){ h=150; w=k?h; } } return w; } </script> <imgborder=0 ID="smallslot" src="4.jpg" onload="javascript:width=x;"align="left"> <script languge="javascript"> varx=show(); </script>
In this code, the width of the processed image is used as the return value of the show() function. Use the onload event to call the Width of the image in .
Problem: The pictures in the picture news on the homepage are not displayed. Click the "Go" button in the address bar to display them normally. Practice tells me another failure! Because the only thing Internet users are used to doing is entering the URL and typing Enter. On this basis, the author made the following attempts: (1) Add "
Option 3: Put the Onload() event in into
After carefully analyzing the option 2, the author found that the main The reason is that the onload event is an event triggered when the page is loaded. Onload is used in the element. When IE interprets it here, the page needs to be loaded to trigger "javascript:width=x;", so the page needs to be reloaded to display the image. In addition, Onload() is generally used in the
<body onload="window.smallslot.width=show();"> <script> function show() {….. //show() } </script> <img border=0 ID="smallslot"src="1.jpg" align="left"> this is atest!
再次测试,通过了,而且首页调用成功!我成功了! 经过这次经历,让笔者感受到了从事程序开发工作的艰辛和乐趣!有时为了一个小小的问题折腾得茶饭不思,但成功的喜悦又让人溢于言表。任何事情只要自己能刻苦钻研、持之以恒,相信问题总会得到解决的。
The above is the detailed content of Detailed explanation of how JavaScript uses three methods to modify image size. For more information, please follow other related articles on the PHP Chinese website!

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

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 dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
