1

I need to make the background image in div tag and it has to change automatically, I already put the array of images inside the javascript, but the images is not showing when i'm run the site.The background should behind the menu header.

This is the div

<div style="min-height:1000px;position:relative;" id="home">

below of the div is containing the logo, menu and nav part.

         <div class="container">
          <div class="fixed-header">
              <!--logo-->
               <div class="logo" >
                  <a href="index.html">
                      <img src="images/logo.png" alt="logo mazmida" height="142" width="242">
                      </a>
               </div>
                <!--//logo-->

This is the javascript

 <script>
   var imgArray = [
    'images/1.jpg',
    'images/2.jpg',
    'images/3.jpg'],       

curIndex = 0;
    imgDuration = 2000;        



function slideShow() {
    document.getElementID('home').src = imgArray[curIndex];
    curIndex++;
    if (curIndex == imgArray.length) { curIndex = 0; }
    setTimeout("slideShow()", imgDuration);
}
slideShow();

Ghost
  • 21
  • 1
  • 7

5 Answers5

1

You have a few issues with your script. I've made a live JSbin example here:

https://jsbin.com/welifusomi/edit?html,output

  <script>
   var imgArray = [
    'https://upload.wikimedia.org/wikipedia/en/thumb/0/02/Homer_Simpson_2006.png/220px-Homer_Simpson_2006.png',
    'https://upload.wikimedia.org/wikipedia/en/thumb/0/0b/Marge_Simpson.png/220px-Marge_Simpson.png',
    'https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png'
   ]; 

   var curIndex = 0;
   var imgDuration = 1000;        

  var el = document.getElementById('home'); 

function slideShow() {
    el.style.backgroundImage = 'url(' + imgArray[curIndex % 3] + ')';
    curIndex++;
    setTimeout("slideShow()", imgDuration);
}
slideShow();
  </script>

There are a few issues with your script:

Optimizations

  • And you can use mod % trick to avoid zero reset
  • Use setInterval instead of setTimeout

Further optimzations

  • Use requestAnimationFrame instead of setTimeout/setInterval

I suggest getting familiar with your browser debugging tools which would help identify many of the issues you face.

Mâtt Frëëman
  • 1,361
  • 9
  • 20
0

document.getElementID('home').src = imgArray[curIndex]

You are targeting a div with an ID of home, but this is not an Image element (ie ,

But since you want to alter the background colour of the DIV, then you use querySelector using javascript and store it in a variable, then you can target the background property of this div (ie Background colour).

I hope this helps.

C Williams
  • 850
  • 12
  • 19
0

You are trying to change the src property of a div, but divs do not have such property.

Try this:

document.getElementById('home').style.backgroundImage = "url('" + imgArray[curIndex] + "')"

This changes the style of the target div, more precisely the image to be used as background.

R. Schifini
  • 9,085
  • 2
  • 26
  • 32
0

As you want to change the background image of the div, instead of document.getElementID('home').src = imgArray[curIndex] use document.getElementById("#home").style.backgroundImage = "url('imageArray[curIndex]')";

in JavaScript or

$('#home').css('background-image', 'url("' + imageArray[curIndex] + '")'); in jquery.

Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33
Ekta Tiwari
  • 11
  • 1
  • 3
0

To achieve expected result, use below option of using setInterval

Please correct below syntax errors

  1. document.getElementID to document.getElementById
  2. .src attribute is not available on div tags
  3. Create img element and add src to it
  4. Finally use setInterval instead of setTimeout outside slideShow function

 var imgArray = [
    'http://www.w3schools.com/w3css/img_avatar3.png',
    'https://tse2.mm.bing.net/th?id=OIP.ySEgAgJIlDQsIQTu_MeoLwHaHa&pid=15.1&P=0&w=300&h=300',
    'https://tse4.mm.bing.net/th?id=OIP.wBAPnR04OfXaHuFI9Ny2bgHaE8&pid=15.1&P=0&w=243&h=163'],       
curIndex = 0;
    imgDuration = 2000;        

var home = document.getElementById('home')
var image = document.createElement('img')

function slideShow() {
    if(curIndex != imgArray.length-1) { 
      image.src = imgArray[curIndex];
      home.appendChild(image)
      curIndex++;
    }else{
      curIndex = 0;
    } 
}
setInterval(slideShow,2000)
<div style="position:relative;" id="home"></div>
 

code sample - https://codepen.io/nagasai/pen/JLKvME

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40