Home >Web Front-end >JS Tutorial >Resize a Popup to Fit an Image's Size
This article demonstrates a clever JavaScript solution for dynamically resizing popup windows to perfectly fit the images they display. The technique works across various browsers including Netscape Navigator 4/5/6/7 and Internet Explorer 4/5/6.
The solution involves two files: a main HTML page containing links to images and a popup HTML file (popup.htm
).
The main HTML page includes links that, when clicked, call a JavaScript function PopupPic()
. This function opens popup.htm
in a new window, passing the image URL via the query string:
function PopupPic(sPicURL) { window.open("popup.htm?" + sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200"); }
popup.htm
uses JavaScript to extract the image URL from the query string, then dynamically inserts the <img alt="Resize a Popup to Fit an Image's Size" >
tag into the page using document.write()
. Crucially, it uses the onload
event to call the FitPic()
function after the image has loaded:
var arrTemp = self.location.href.split("?"); var picUrl = (arrTemp.length > 1) ? arrTemp[1] : ""; var NS = (navigator.appName == "Netscape") ? true : false; function FitPic() { var iWidth = (NS) ? window.innerWidth : document.body.clientWidth; var iHeight = (NS) ? window.innerHeight : document.body.clientHeight; iWidth = document.images[0].width - iWidth; iHeight = document.images[0].height - iHeight; window.resizeBy(iWidth, iHeight); self.focus(); }; document.write("<img src="%22%20+%20picUrl%20+%20%22" border="0" alt="Resize a Popup to Fit an Image's Size" >");
The FitPic()
function calculates the difference between the image dimensions and the browser window's dimensions. It then uses window.resizeBy()
to adjust the popup window size accordingly, ensuring a perfect fit.
This method elegantly solves the common problem of mismatched popup and image sizes, providing a user-friendly image viewing experience. The script's efficiency and cross-browser compatibility make it a valuable tool for webmasters managing image galleries.
Frequently Asked Questions (FAQ): (Summarized for brevity)
drawImage
with smoothingQuality = 'high'
.This revised response provides a more concise and readable explanation while retaining the core information and maintaining the image.
The above is the detailed content of Resize a Popup to Fit an Image's Size. For more information, please follow other related articles on the PHP Chinese website!