Everyone knows that colorful web pages are inseparable from the support of the picture, the image has increased the vividness of the webpage, and on the other hand, the web page is increased, so that the download speed slows down. How to make a picture can be displayed on the page at an appropriate size and become a mystery that I can't solve.
Not long ago, I developed a teaching research website for the unit. In the news management system, in the newsletter, you need to call the image file from the database as a picture in the picture news, thereby forming a text winding form. Since the entire system includes news upload, news editing, system announcement, etc. is dynamically realized. In the development of the process, I consider the following factors: (1) as the maintainer of the website, the operation of the computer is not very skilled; (2) The website is reasonable, and the content is enriched. Therefore, for the design, the website page, the image news has a certain demand, the width of the picture, the width of this website cannot exceed 200px, even though I understand, we can use photoshop and other image processing software, you can handle the picture in advance Ok, upload it to the server, reach the same effect of the home page call, but as a user, the more hope is the simpler, so I am determined to break this problem. After a few days of fierce battle, I will constantly find the way to solve the problem, and continue to test until success. At this point, I wrote some of my experiences, and I have been engaged in the script preparation.
To make the picture display in an appropriate size, the essence is a problem with a large picture. Of course, the small picture cannot be enlarged in the web page, otherwise the image distortion will appear. How to get the size of the image through the image of the image (width, height) is the key to the problem, I will go to the online search to use JavaScript to write image equivalents, etc., and I have not worried, I finally found "adaptive picture size. The pop-up window "One article, the page effect is, click the text hyperlink, the page pops up a new window, showing pictures, where the form size is equivalent to the picture, the article is dynamically loaded with the JavaScript: Image () object, get pictures The height and width, according to the height of the source picture, the width setting pop-up window is high and width of the width of the pop-up window, and open the window, the main code is as follows:
Test one:
hEAD>
Var imgobj;
Function Checkimg (Turl, WinName) {
// Whether the object has been created
IF (IMGOBJ) == Object) {
/ / Whether the height and width of the image have been obtained
IF (IMGOBJ.WIDTH! = 0) && (imgobj.height! = 0)))
/ / The height and width of the pop-up window is set according to the acquired image height and the width, and open the window //, the increments 20 and 30 are spaced between the settings of the window border and the picture.
OpenFullsizeWindow (Turl, WinName, Width = (IMGOBJ.WIDTH 20) , Height = (IMGOBJ.HEIGHT 30);
Else
// Because the image is dynamically loaded by the image object, it is impossible to get the width and height of the picture immediately, so repeat the call check every 100 milliseconds.
SetTimeout (Checkimg (' theurl ",' Winname '), 100)
}
}
Function OpenFullsizeWindow (Turl, WinName, Features) {
Var anewwin, sbasecmd;
// pop up the window appearance parameters
SBasecmd = Toolbar = no, location = no, status = no, menubar = no, scrollbars = no, resizable = no ,;
/ / Call from Checkimg
IF (Features == Null || features ==) {
// Create an image object
IMGOBJ = new image ();
/ / Set the image source
IMGOBJ.SRC = theurl;
/ / Start getting image size
Checkimg (THEURL, WINNAME)
}
Else {
// Open the window
Anewwin = window.open (Theurl, WinName, Sbasecmd Features);
/ / Focus window
Anewwin.focus ();
}
}
// ->
script>
body>
html>
With image () objects to get the width, Height, I know how to solve how to achieve the proportional zoom problem that implements image. So, I use K = Width / Height to represent the proportional value of the image, when K> = 1, indicates width> heght, as long as Width does not exceed 200px, Height must <= 200px; opposite K <1, as long as height does not exceed 150px, width must <= 150px (Width / height = 4: 3). Therefore, as long as K> = 1 defines width, K <1 defines Height. With the help of the test one, I will soon have the following code (test 2):
Test two
Var imgobj;
Function checkimg (theurl) {
Var Width;
VAR K;
IMGOBJ = new image ();
IMGOBJ.SRC = theurl;
IF (IMGOBJ) == Object) {
IF (IMGOBJ.WIDTH! = 0) && (imgobj.height! = 0)) {width = imgobj.width;
Height = imgobj.height;
K = width / height;
Document.write (k);
IF (k> = 1) {
IF (width> = 200) {
Width = 200;
Height = width / k;
}
Else
{IF (Height> = 200) {
HEIGHT = 200;
Width = k * height;
}
}
Showimg (Turl, Width, Height);
}
Else
SetTimeout (Checkimg (' theurl '), 100)
}
}
Function showimg (theurl, x, y)
{
Document.write (
}
// ->
script>
Checkimg (bt0085.jpg);
script>
Test passed! I am happy, immediately transplant the code into the home file (Default.asp), then test through the server, the result When I entered the URL, the browser has handled the post-moving picture, I suddenly Dumbent, obviously I run the home file, but the result appears pictures. In addition, I found that the browser toolbar "Back" tool is available, I click "Back", and this time the page content appears. Test again, the result is normal. After repeated test, I found the same phenomenon when I updated the picture, and the same problem occurred while I opened a computer that did not have visited this website. I am more confused, I can't understand. Why is this phenomenon? I thought a lot of programs, and the results failed during testing.
In anxious, I went to the Internet to check the characteristics of the image () object, mainly to implement picture roll effect, download the picture to the client in advance, so that the switch between the pictures does not have time delay. Like the code below, use the Image object's SRC property to specify the path (URL) of the image, then download the image Pic2.gif in the images directory to the client:
Var myimg = new image ();
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, then when the user moves the mouse to the image, the picture Will change immediately without time delay. So if the first time the picture is displayed, the home file will be displayed normally. This is this reason. Thus, I conclude that IMAGE () objects can be used to obtain an image of the image, and the solution to solve the problem should be changed.
I checked a lot of books about JavaScript, and there was a book to write "the beating" special effects of the "Figure": "The element of the page can define its display range, the height height and width of the figure (Width When handling event triggers, two attributes that dynamically change the map can achieve effect; "According to the prompt, I quickly completed the code of the test three, the main code is as follows: Test three
Function show ()
{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>
VAR x = show ();
// Document.write (x);
script>
In this code, I take into account the ID in , you can define the width and height of the figure, as long as the width of the image can be controlled, the equivalence display can be implemented in the web preview (high degree of width changes) So I will process the width of the post-image as the return value of the show () function. Call the image of the image with an onload event in
. I will test again, pass! Transplant it again to the home code, tested again, then the problem appears again: Home on the picture news is actually no display. I click on the "Go to" button in the address bar, this picture can be displayed normally. Practice tells me another failure! Because the only habit of the Internet is to do it, type the input URL, type the two actions entering these.
On this basis, I did the following attempts:
(1),
head> . Allow 10 seconds to automatically refresh the screen, the result picture can be displayed normally, but constantly brushing the screen Vision is very uncomfortable.(2) Window.Location.Reload () Re-loads the page, but the result is that the page cannot be displayed correctly during the loading page.
(3), 200) this.width = 200; align = left>, test can pass, but if the image width
I have a little bit of talents, I can't find a better way to solve this problem, gradually lose my confidence, I haven't slept for this question, I am going to have the last fight. I carefully analyze my test three. I found that the main reason is that the online event is an event triggered when the page is loaded. OnLoad is used in element, when IE explains here, the page is required to trigger "JavaScript: width = X; therefore to reload the page to display the picture. In addition, I know onload () is usually used in
Test four:
>>>>Function show ()
{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>
this is a test!
Test again, pass it! And the home call is successful! I succeeded!