Download changing background images immediately - javascript

I created site with changing background images.
HTML:
<div id="templatemo_header">
</div>
CSS:
#templatemo_header {
background: url(images/templatemo_header.jpg) no-repeat;
}
And JS code which changes the BG image each 3 seconds
function displayImage(image) {
document.getElementById("templatemo_header").style.backgroundImage = image;
}
function displayNextImage() {
x = (x == images.length - 1) ? 0 : x + 1;
displayImage(images[x]);
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
displayImage(images[x]);
}
function startTimer() {
setInterval(displayNextImage, 3000);
}
var images = [], x = -1;
images[0] = 'url("images/templatemo_header1.jpg")';
images[1] = 'url("images/templatemo_header2.jpg")';
images[2] = 'url("images/templatemo_header3.jpg")';
images[3] = 'url("images/templatemo_header.jpg")';
The problem is when each of BG images changed only first time, image dissapeares and after some moment new image appears.
I guess, it's because new BG image is downloaded.
Is it some way to force download the images immediately, say in function startTimer?
Thanks in advance.

There are a few ways to pre-load images using javascript here. By pre-loading the images you may have better luck when displaying them. In your case I would use Method 3 if possible, if not Method 2. As described in the article, you would use the window.onload event to kick off the downloading of the images once the page has loaded.
I would just setup your image array as follows:
window.onload = preLoadImages();
function preLoadImages(){
preLoadImage = new Image();
images[0] = "images/templatemo_header1.jpg";
images[1] = "images/templatemo_header2.jpg";
images[2] = "images/templatemo_header3.jpg";
images[3] = "images/templatemo_header.jpg";
for(i=0, i<=images.length, i++){
preLoadImage.src=images[i];
}
}

Related

How do I add external links to the images in this piece of code?

I am working on a slideshow/carousel for my website. I found this code online and would like to know how to add links to the images presented in the slideshow. I am open to any other suggestions for slideshows.
<!-- Showcase/Banner Image -->
<section id="banner">
<script>
var i = 0; // Start point
var images = [];
var time = 2500;
// Image List
images[0] = 'simpleslideshow/slide1.jpg';
images[1] = 'simpleslideshow/slide2.jpg';
images[2] = 'simpleslideshow/slide3.jpg';
images[3] = 'simpleslideshow/slide4.jpg';
// Change Image
function changeImg(){
document.slide.src = images[i];
if(i < images.length - 1){
i++;
} else {
i = 0;
}
setTimeout("changeImg()", time);
}
window.onload = changeImg;

Caption Based on changing Image?

I have this code:
<script type="text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
function startTimer() { //this is started when the body is loaded
setInterval(displayNextImage, 10000);
}
var images = [], x = -1;
images[0] = "image0.jpg";
images[1] = "image1.jpg";
images[2] = "image2.jpg";
images[3] = "image3.jpg";
</script>
Basically every 10 seconds (defined by the setInterval) it changes to the next image, then cycles back to the first image again. What I need, is when it displays Image 0 it shows a caption bellow the image "This images was taken by etc.", but when it changes to the second image (Image1) it changes it to the caption for the second image and so on?
Preferably using Javascript or maybe JQuery if necessary (I'm better at JavaScript)
I know this could be done by editing the photo in Photoshop by adding some space on the bottom and adding some text that way, but I think it wouldn't look nice.
You can use your displayNextImage(), displayPreviousImage() functions to place prepared captions into a DIV element:
<script type="text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
document.getElementById("cap").innerHTML = captions[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
document.getElementById("cap").innerHTML = captions[x];
}
function startTimer() { //this is started when the body is loaded
setInterval(displayNextImage, 10000);
}
var images = [], captions = [], x = -1;
images[0] = "image0.jpg";
images[1] = "image1.jpg";
images[2] = "image2.jpg";
images[3] = "image3.jpg";
captions[0] = "foo";
captions[1] = "bar";
captions[2] = "foobar";
captions[3] = "foobarfoo";
</script>
In your HTML, add <div id="cap"></div>

Retina.js swapping out images in an array

I'm using Retina.js to swap out images on my site for retina screens. It's working fine however I have an issue when I am swapping images from an array. I have one image in the middle of the screen and every 3 seconds this changes to a different image stored in the array. Like so:
<script type = "text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
function startTimer() {
setInterval(displayNextImage, 1600);
}
var images = [], x = -1;
images[0] = "smallslide2.png";
images[1] = "smallslide3.png";
images[2] = "smallslide4.png";
images[3] = "smallslide5.png";
images[4] = "smallslide1.png";
</script>
In my html I have:
<p class="centeredImage">
<img id ="img" src="smallslide1.png">
</p>
The first slide works and shows the retina image, I guess this because it's using the standard image detection i.e replacing image src = " ", for the rest of the images it doesn't work though. Is there a way to get it to work for the images in the array? Any pointers on this would be really appreciated. Thanks!
edit:
if (document.getElementById("img").src===smallslide1#2x.png){
images[0] = "smallslide2#2x.png";
images[1] = "smallslide3#2x.png";
images[2] = "smallslide4#2x.png";
images[3] = "smallslide5#2x.png";
images[4] = "smallslide1#2x.png";
}
else {
images[0] = "smallslide2.png";
images[1] = "smallslide3.png";
images[2] = "smallslide4.png";
images[3] = "smallslide5.png";
images[4] = "smallslide1.png";
}

how to implement automatic sliding images through array in javascript

I want to implement sliding images as soon as the website loads.
Here's my html code
<html>
<title>Wold of Guitars</title>
<body onload="images()">
</body>
</html>
And here's my javascript
var imgArray = new Array();
imgArray[0] = new Image();
imgArray[0].src = '../img/n/h1.jpg';
imgArray[1] = new Image();
imgArray[1].src = '../img/n/h2.jpg';
imgArray[2] = new Image();
imgArray[2].src = '../img/n/home.jpg';
imgArray[3] = new Image();
imgArray[3].src = '../img/n/h3.jpg';
imgArray[4] = new Image();
imgArray[4].src = '../img/n/h4.jpg';
x=-1;
function images()
{ x=x+1
if(x>4)
x=1
document.write("<li><img src='" + imgArray[x] + "' width="1350" height="630"/><span>" + imgArray[] + "</span></li>");
var t=setInterval("images()",3000);
}
Help will be appreciated.
Although you mentioned "sliding" your initial code looked liked you wanted to swap the images over time.
I admit you may struggle to find good resources on JavaScript for something so simple, which Is why I can relate to your question.
Ok so here's a quick & dirty but more modern-ish image changing slide requestAnimationFrame:
https://jsfiddle.net/julienetienne/95tqftnk/4/
The same principals apply for making images slide except you are pre-loading all images and shifting the position by the slide show width.
var speed = 3;
// Your image array
var images = [
'http://www.gettyimages.co.uk/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg',
'http://www.hdwallpapersimages.com/wp-content/uploads/images/Child-Girl-with-Sunflowers-Images.jpg',
'http://www.gettyimages.co.uk/CMS/StaticContent/1391099215267_hero2.jpg'];
// Get the slideShow tag
var slideShow = document.getElementById('slide-show');
// Create the image tag //You could also just make one in HTML and get the tag
var img = document.createElement('img');
// Append the image to the slideshow container
slideShow.appendChild(img);
// Basic request animation polyfill
var rAF = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
return setTimeout(callback, 1000 / 60);
};
// declare var count outside of rAF loop
var count;
function changeImage(timeStamp) {
count = Math.floor(timeStamp / 1000 / speed);
while (count > images.length -1) {
count -= images.length;
}
img.src = images[count];
rAF(changeImage);
}
rAF(changeImage);
img {
width: 600px;
height auto;
}
<div id="slide-show"></div>

Automatic slideshow is not showing properly

I have an automatic slideshow on the main page of some site. Images are rotating every 10 seconds, but when they appear for the first time they aren't already loadead (I guess), so a blank is show.
I think that preloading these images they will show fine, but I don't know how to preload them. I use the following code to preload all the images of the page, but it doesn't seems to work:
function preloadAllImages() {
for (i = 0; i < document.images; i++) {
var img = new Image();
img.src = document.images[i];
}
}
So, how can I make this work?
Try
function preloadAllImages() {
for ( var i = 0; i < document.images.length; i++) {
var img = new Image();
img.src = document.images[i];
}
}
I believe you are missing the length attribute.

Categories