• Resolved Peter

    (@peterkaagman)


    Hi there forum,

    I’m trying to use javascript to set the style property backgroundImage. But I can’t seem to get it working… and worst… a bit clueless at the moment as for the reason

    The Div is declare in header.php:

    <Div id="site-logo"></Div>

    Style.css has some settings for that div:

    #site-logo{
            position: absolute;
            right: 0;
            z-index: 10;
            width: 300px;
            height:300px;
            background-size: 100% 100%;
            background: red;
    }

    The red background is for debugging purposes… sticks out to identify visibility and positioning.

    and I’m using the following javascript:

    var howOften = 5; //number often in seconds to rotate
    var current = 0; //start the counter at 0
    
    // place your images, text, etc in the array elements here
    var items = new Array();
        items[0]="url=('/wp-content/uploads/images/site-logo-dame1.png')";
        items[1]="url=('/wp-content/uploads/images/site-logo-there-is-only-2.png')";
    
      function rotater() {
        document.getElementById("site-logo").style.backgroundImage = items[current];
        current = (current==items.length-1) ? 0 : current + 1;
        setTimeout("rotater()",howOften*1000);
      }
    window.onload=rotater;

    Using a javascript alert() I made sure the code gets executed and checked the value assigned to items[current].
    This same script did work when I used the innerHTML property.

Viewing 1 replies (of 1 total)
  • Thread Starter Peter

    (@peterkaagman)

    this

    // place your images, text, etc in the array elements here
    var items = new Array();
        items[0]="url=('/wp-content/uploads/images/site-logo-dame1.png')";
        items[1]="url=('/wp-content/uploads/images/site-logo-there-is-only-2.png')";

    should have been

    // place your images, text, etc in the array elements here
    var items = new Array();
        items[0]="url('/wp-content/uploads/images/site-logo-dame1.png')";
        items[1]="url('/wp-content/uploads/images/site-logo-there-is-only-2.png')";

    the equel sign should not have been there

Viewing 1 replies (of 1 total)
  • The topic ‘Using javascript to set .style.backgroundImage’ is closed to new replies.