spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / diner / bgresize
DHTML Diner Logo

This is a DHTML cross-browser technique. The in-page examples will only work in Navigator 4 and Explorer 4, all platforms.

This technique was developed in response to a query from Mathieu in the DHTML Lab Forum.
Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

Background Image Resize

Click the form button in the left column to see this technique in a new window.

Resize the window, and the background image will scale to fit the new window size. Well, not the background image, actually, but an image that seems to be a background image.

Scaling images and related browser features and/or limitations have been discussed in great detail in our Dynamic Images column. Here, we will concern ourselves with the mechanics of this particular adaptation.

The Logic

1. In order for an image to appear to be "in the background," it must be placed in a positioned element that resides outside the regular page flow. This element must be positioned at the top-left of the page. An image is then placed in the element that has a WIDTH equal to the window's inner width and a HEIGHT equal to the window's inner height. This HTML must be written dynamically, since we have no way of knowing the final user's window dimensions.

2. We set the onresize handler for the window to call a function when the window is resized

3. For Explorer, we simply resize the image to the new window dimensions. For Navigator, we reload the page, resulting in the positioned element and the image being re-created, giving the illusion of image scaling.

Browser Quirks

In developing this short script, we ran into an unbelievable amount of small problems related to browser quirks. For example:

  • Navigator knows the inner dimensions of a window immediately, since it stores them in properties of window. Explorer waits until the BODY tag appears, since the dimensions are stored in properties of document.body.

  • Explorer will place an element below the page-proper in z-order if the element's z-index is set to -1. Navigator requires the element to be created first on the page.

  • Navigator usually prefers an element to be declared in the STYLE tag and not with the STYLE= attribute. In this case, however, a dynamic setting of the STYLE= attribute was to only way to get the desired effect.

  • Even though the positioned element was given values of 0 for top and left, Navigator ignored them, requiring the BODY tag attributes of MARGINWIDTH and MARGINHEIGHT to be set.

  • and many, many, more!

Advice

Use this technique sparingly when dealing with Navigator. Preferably, keep the image's positioned element as the only positioned element on your page. For Explorer-only pages, the technique has no problems.

Use this technique in short or windowed pages, like our example. Long pages, like the one you are reading, are, of course, not suited for this effect.

With that said, let's look at the code.



Produced by Peter Belesis and



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.

All Rights Reserved. Legal Notices.
Created: May 07, 1998
Revised: May 07, 1998

URL: http://www.webreference.com/dhtml/diner/bgresize/