How can I preload large background images before showing them to be used with the css background-image: url(image);
format. I can use jQuery to make it easier.
Asked
Active
Viewed 83 times
0

mattdevio
- 2,319
- 1
- 14
- 28
1 Answers
0
var img = document.createElement('img')
img.src = 'http://yourhref.com/myimg.jpg'
would load the image. With Css:
var div = document.createElement('div')
div.style.backgroundImage = 'url("myurl.jpg")'
Then, when you want to add it,
document.body.appendChild(div)

Zane Hitchcox
- 936
- 1
- 9
- 23
-
The 'with CSS method', does this cache the image? – mattdevio Mar 30 '16 at 01:09
-
Yes, usually. It depends on the browser, browser version, the expiration of your cache headers, whether or not you're using ssl, etc....but, yes. – Zane Hitchcox Mar 30 '16 at 01:49