Preload background images
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am cycling through background images in javascript/jQuery like this...
var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>Problem is, I get flashes of no image occasionally, I think this is down to the images not being loaded in time. Is there a way to preload them?
javascript jquery
add a comment |
I am cycling through background images in javascript/jQuery like this...
var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>Problem is, I get flashes of no image occasionally, I think this is down to the images not being loaded in time. Is there a way to preload them?
javascript jquery
Why so complicated? Simple create<img>tags inside<div style="display:none">.
– Martin Heralecký
Nov 22 '18 at 13:19
add a comment |
I am cycling through background images in javascript/jQuery like this...
var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>Problem is, I get flashes of no image occasionally, I think this is down to the images not being loaded in time. Is there a way to preload them?
javascript jquery
I am cycling through background images in javascript/jQuery like this...
var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>Problem is, I get flashes of no image occasionally, I think this is down to the images not being loaded in time. Is there a way to preload them?
var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>var duration = 2500;
var delay = 500;
var i = 0;
setInterval(function() {
if (i == 0) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1480/any')");
}
if (i == 1) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1481/any')");
}
if (i == 2) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1482/any')");
}
if (i == 3) {
$(".myimage").css("background-image", "url('https://placeimg.com/1640/1483/any')");
}
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>javascript jquery
javascript jquery
edited Nov 22 '18 at 13:26
Zakaria Acharki
57.3k134471
57.3k134471
asked Nov 22 '18 at 13:17
fightstarr20fightstarr20
2,9561157120
2,9561157120
Why so complicated? Simple create<img>tags inside<div style="display:none">.
– Martin Heralecký
Nov 22 '18 at 13:19
add a comment |
Why so complicated? Simple create<img>tags inside<div style="display:none">.
– Martin Heralecký
Nov 22 '18 at 13:19
Why so complicated? Simple create
<img> tags inside <div style="display:none">.– Martin Heralecký
Nov 22 '18 at 13:19
Why so complicated? Simple create
<img> tags inside <div style="display:none">.– Martin Heralecký
Nov 22 '18 at 13:19
add a comment |
4 Answers
4
active
oldest
votes
You could preload them using CSS like:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}
NOTE: You could use an array of images in the JS code and change them based on the index i.
var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>add a comment |
For the first time, browser needs to download all the images, once downloaded, it'll load from cache.
So, flashes of no image will come every time whenever browser download file. you have two options
the approach you are following (which is ok)
Use CSS trick to load all at once by using following code
.myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1480/any');
background-image: url('https://placeimg.com/1648/1481/any');
background-image: url('https://placeimg.com/1648/1482/any');
background-image: url('https://placeimg.com/1648/1483/any');
background-size: cover;
}
add a comment |
Once browser loads it, it wont be reloaded all the time. the second time the same image is rendered there wont be such issue,
make the image cycle using i= i==3?0:++i.
Rather than just i++
Fiddle
add a comment |
You can preload the images as followed
function preloadImages(){
var images = ["https://placeimg.com/1640/1480/any","https://placeimg.com/1640/1481/any","https://placeimg.com/1640/1482/any","https://placeimg.com/1640/1483/any"];
var prelaodedImages = ;
for(i=0;i<images.length;i++){
prelaodedImages[i] = new Image();
prelaodedImages[i].src = images[i];
console.log(prelaodedImages[i].src)
}
}
preloadImages();
After the images are preloaded you can use your code and instead of
"url('https://placeimg.com/1640/1480/any')")
You use
"url("+prelaodedImages1.src+")")
This should normally result in your images already being loaded and displaying correctly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431876%2fpreload-background-images%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could preload them using CSS like:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}
NOTE: You could use an array of images in the JS code and change them based on the index i.
var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>add a comment |
You could preload them using CSS like:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}
NOTE: You could use an array of images in the JS code and change them based on the index i.
var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>add a comment |
You could preload them using CSS like:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}
NOTE: You could use an array of images in the JS code and change them based on the index i.
var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>You could preload them using CSS like:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}
NOTE: You could use an array of images in the JS code and change them based on the index i.
var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>var duration = 2500;
var delay = 500;
var i = 0;
var images = ['https://placeimg.com/1640/1480/any', 'https://placeimg.com/1640/1481/any', 'https://placeimg.com/1640/1482/any', 'https://placeimg.com/1640/1483/any'];
setInterval(function() {
$(".myimage").css("background-image", "url(" + images[i] + ")");
i++;
}, duration + delay).myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1488/any');
background-size: cover;
}
body::after {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
z-index: -1;
content: url(https://placeimg.com/1640/1480/any) url(https://placeimg.com/1640/1481/any) url(https://placeimg.com/1640/1482/any) url(https://placeimg.com/1640/1483/any);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myimage">
</div>edited Nov 22 '18 at 13:54
answered Nov 22 '18 at 13:24
Zakaria AcharkiZakaria Acharki
57.3k134471
57.3k134471
add a comment |
add a comment |
For the first time, browser needs to download all the images, once downloaded, it'll load from cache.
So, flashes of no image will come every time whenever browser download file. you have two options
the approach you are following (which is ok)
Use CSS trick to load all at once by using following code
.myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1480/any');
background-image: url('https://placeimg.com/1648/1481/any');
background-image: url('https://placeimg.com/1648/1482/any');
background-image: url('https://placeimg.com/1648/1483/any');
background-size: cover;
}
add a comment |
For the first time, browser needs to download all the images, once downloaded, it'll load from cache.
So, flashes of no image will come every time whenever browser download file. you have two options
the approach you are following (which is ok)
Use CSS trick to load all at once by using following code
.myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1480/any');
background-image: url('https://placeimg.com/1648/1481/any');
background-image: url('https://placeimg.com/1648/1482/any');
background-image: url('https://placeimg.com/1648/1483/any');
background-size: cover;
}
add a comment |
For the first time, browser needs to download all the images, once downloaded, it'll load from cache.
So, flashes of no image will come every time whenever browser download file. you have two options
the approach you are following (which is ok)
Use CSS trick to load all at once by using following code
.myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1480/any');
background-image: url('https://placeimg.com/1648/1481/any');
background-image: url('https://placeimg.com/1648/1482/any');
background-image: url('https://placeimg.com/1648/1483/any');
background-size: cover;
}
For the first time, browser needs to download all the images, once downloaded, it'll load from cache.
So, flashes of no image will come every time whenever browser download file. you have two options
the approach you are following (which is ok)
Use CSS trick to load all at once by using following code
.myimage {
height: 500px;
width: 500px;
transition: background-image 1s linear;
background-image: url('https://placeimg.com/1648/1480/any');
background-image: url('https://placeimg.com/1648/1481/any');
background-image: url('https://placeimg.com/1648/1482/any');
background-image: url('https://placeimg.com/1648/1483/any');
background-size: cover;
}
answered Nov 22 '18 at 13:31
Harsh ChaurasiaHarsh Chaurasia
9615
9615
add a comment |
add a comment |
Once browser loads it, it wont be reloaded all the time. the second time the same image is rendered there wont be such issue,
make the image cycle using i= i==3?0:++i.
Rather than just i++
Fiddle
add a comment |
Once browser loads it, it wont be reloaded all the time. the second time the same image is rendered there wont be such issue,
make the image cycle using i= i==3?0:++i.
Rather than just i++
Fiddle
add a comment |
Once browser loads it, it wont be reloaded all the time. the second time the same image is rendered there wont be such issue,
make the image cycle using i= i==3?0:++i.
Rather than just i++
Fiddle
Once browser loads it, it wont be reloaded all the time. the second time the same image is rendered there wont be such issue,
make the image cycle using i= i==3?0:++i.
Rather than just i++
Fiddle
edited Nov 22 '18 at 13:36
answered Nov 22 '18 at 13:25
Jins PeterJins Peter
1,293621
1,293621
add a comment |
add a comment |
You can preload the images as followed
function preloadImages(){
var images = ["https://placeimg.com/1640/1480/any","https://placeimg.com/1640/1481/any","https://placeimg.com/1640/1482/any","https://placeimg.com/1640/1483/any"];
var prelaodedImages = ;
for(i=0;i<images.length;i++){
prelaodedImages[i] = new Image();
prelaodedImages[i].src = images[i];
console.log(prelaodedImages[i].src)
}
}
preloadImages();
After the images are preloaded you can use your code and instead of
"url('https://placeimg.com/1640/1480/any')")
You use
"url("+prelaodedImages1.src+")")
This should normally result in your images already being loaded and displaying correctly.
add a comment |
You can preload the images as followed
function preloadImages(){
var images = ["https://placeimg.com/1640/1480/any","https://placeimg.com/1640/1481/any","https://placeimg.com/1640/1482/any","https://placeimg.com/1640/1483/any"];
var prelaodedImages = ;
for(i=0;i<images.length;i++){
prelaodedImages[i] = new Image();
prelaodedImages[i].src = images[i];
console.log(prelaodedImages[i].src)
}
}
preloadImages();
After the images are preloaded you can use your code and instead of
"url('https://placeimg.com/1640/1480/any')")
You use
"url("+prelaodedImages1.src+")")
This should normally result in your images already being loaded and displaying correctly.
add a comment |
You can preload the images as followed
function preloadImages(){
var images = ["https://placeimg.com/1640/1480/any","https://placeimg.com/1640/1481/any","https://placeimg.com/1640/1482/any","https://placeimg.com/1640/1483/any"];
var prelaodedImages = ;
for(i=0;i<images.length;i++){
prelaodedImages[i] = new Image();
prelaodedImages[i].src = images[i];
console.log(prelaodedImages[i].src)
}
}
preloadImages();
After the images are preloaded you can use your code and instead of
"url('https://placeimg.com/1640/1480/any')")
You use
"url("+prelaodedImages1.src+")")
This should normally result in your images already being loaded and displaying correctly.
You can preload the images as followed
function preloadImages(){
var images = ["https://placeimg.com/1640/1480/any","https://placeimg.com/1640/1481/any","https://placeimg.com/1640/1482/any","https://placeimg.com/1640/1483/any"];
var prelaodedImages = ;
for(i=0;i<images.length;i++){
prelaodedImages[i] = new Image();
prelaodedImages[i].src = images[i];
console.log(prelaodedImages[i].src)
}
}
preloadImages();
After the images are preloaded you can use your code and instead of
"url('https://placeimg.com/1640/1480/any')")
You use
"url("+prelaodedImages1.src+")")
This should normally result in your images already being loaded and displaying correctly.
answered Nov 22 '18 at 14:02
mrdeadsvenmrdeadsven
458216
458216
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431876%2fpreload-background-images%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why so complicated? Simple create
<img>tags inside<div style="display:none">.– Martin Heralecký
Nov 22 '18 at 13:19