I am trying to build a jQuery plugin that implements some functionality I already have in my site (but in an easy to reuse way): changing the "src" attribute of an image based on the width of the browser.
The problem is that when loading the new image, the img element collapses to 0 height and width for a moment, and then jumps to the new height. Even preloading the image is not enough (I think that it's because of the huge dimensions that makes it take its time loading even after being pre-downloaded).
I decided to do this:
- Set the current src as a CSS background-image attibute.
- Force the size of the image to match the one that's now the background.
- Then setting the new src.
- When the new src loads, revert the forced size to let the new src (or whatever other rules or properties the programmer used before) define it again.
I don't want to mess up the user's (by user I mean the programmer using my library) CSS rules and HTML attributes.
The problem is that the size can be defined in different ways, por example:
- using
<img width="xx" height="yy">
- using CSS:
img { width: xx, height: yy }
- letting the loaded file impose it's own dimensions
So, the behavior I am looking for is:
- the programmer defines the img dimensions any way he wants
- my plugin momentarily force-freezes those dimensions during src change
- when it unfreezes them, the dimensions are again determined by the programmers original rules
The question is: How can I unset the forced dimensions in a way that restores previous rules, even when it's just the image file the one ruling them?