I am in the process of developing a custom jQuery plugin that will enable me to easily change the "src" attribute of an image on my website based on the width of the browser.
However, I've encountered an issue where when the new image is loaded, the img element momentarily collapses to 0 height and width before suddenly jumping to its correct dimensions. Preloading the image didn't solve the problem, possibly due to its large size causing delays even after being pre-downloaded.
To address this, I have come up with the following solution:
- First, set the current src as a CSS background-image attribute.
- Forcefully set the size of the image to match the background image's dimensions.
- Then update the src to the new image.
- Once the new src has fully loaded, revert the forced dimensions back to their original state, allowing the new src or any other rules set by the programmer to determine the image size.
I want to ensure that I do not disrupt the programmer's (the individual using my library) CSS styles and HTML attributes.
One challenge I'm facing is that image sizes can be defined in various ways, such as:
- Using
<img width="xx" height="yy">
- Setting dimensions with CSS:
img { width: xx, height: yy }
- Allowing the loaded file to dictate its own dimensions
Therefore, the desired behavior is:
- The programmer should be able to define image dimensions in any preferred manner
- My plugin should temporarily maintain those dimensions during src changes
- Upon restoration, the image dimensions should go back to adhering to the programmer's original specifications
My main question now is: How can I remove the enforced dimensions in a way that restores the previous rules, even if it's solely the image file determining them?