Showing posts with label dhtml. Show all posts
Showing posts with label dhtml. Show all posts

Friday, 22 February 2008

Cheat's way to show an indicator while images are loading

There are several cool, interesting ways to use Javascript to show progress indicators while images are downloading to your page. The method shown in this post is not one of them.

I have a simple ASP.NET page that has two dynamically generated images. The images are charts streamed from another ASPX page (e.g. <img src="MakeChart.aspx?someParamsHere" />), and they can sometimes take a couple of seconds to render on the page. This delay means people temporarily see a very bare page that looks like it has finished loading, and might start clicking around or navigating away before they get to see the pretty pictures. What I wanted was to give people some indication that stuff was happening, and that they should stick around for a few seconds.

Rather than whipping out ye olde JavaScript, I chose to keep things really simple. I created a CSS class to display the progress indicator as a background on the element holding the dynamic image. While the image is loading the background shows through. Once it is done, the background is covered by the final image.

<style>
.ChartPlaceholder {
  background-image: url("Images/loadingPlaceholder.png");
  background-position: center;
  background-repeat: no-repeat;
  height: <%= ChartHeight %>px;
  width: <%= ChartWidth %>px;
}
</style>
...
<div class="ChartPlaceholder">
  <asp:Image runat="server" ID="SomeDynamicChart"  />
</div>

In this case I have embedded the DIV's height and width defined in the ASPX straight into the CSS declaration, as it seemed reasonable in the context in which I am currently using it, but you can obviously use other methods to make the DIV render sensibly prior to the image coming through.

A bit hacky, but very simple and worked nicely in FireFox and IE7.

Thursday, 4 January 2007

Covering drop down elements in IE

Internet Explorer (as of version 6) renders select form elements (drop downs) and some other objects like iframes above all other layers. This is awful when you want to have an div element pop-up in an HTML screen, only to have half the pop-up obscured by a drop down box.

One solution floating around (I used this article) is to use this bug against itself. Divs can't cover selects, but iframes can. So you create an iframe to cover the elements, then put your div over the iframe. You could put the content itself in the iframe, but this can lead to headaches with script references.

Here is the code I used:

<div id="CalendarContainer" style="position: absolute; width: 200px; height: 190px; visibility:hidden;">
  <iframe id="CalendarIframe" frameborder="0" style="width: 100%; height: 100%; position: absolute; z-index: 1;">
  </iframe>
  <div id="Calendar" style="width:100%; height:100%; position:absolute; z-index:2;">
  </div>
</div>

You can then toggle the CalendarContainer and it will render in front of overlapping select elements. Firefox and other Gecko-based browsers don't suffer from this problem, and will render the div properly without these iframe gymnastics.

Thursday, 14 December 2006

Quirksmode.org: Reference site for JS and CSS

QuirksMode.org has lots of great Javascript and CSS resources.