2016-04-01 19 views
0

Ich versuche, das Hintergrundbild zu wechseln. Es hat vorher funktioniert, aber jetzt ist etwas aus. Im Moment ist der Hintergrund der gleiche wie der von CSS, ich möchte das ändern, um es zu alternieren.JavaScript Ich versuche zu wechseln backgroundImage

var widthOfBrowser = window.innerWidth; 
 
var heightOfBrowser = window.innerHeight; 
 
var hideImage; 
 
var timeInterval = 20000; 
 
var time = 5000; 
 
var changeNavBar = document.getElementsByTagName("body")[0]; 
 
var T1; 
 
var T2; 
 
var T3; 
 
var intervalMain; 
 
var text; 
 
var parent; 
 
var child1; 
 
var child2; 
 
var valueHidePadding; 
 
var mobileTwo; 
 
var desktopView; 
 
var obj3; 
 
var obj1; 
 
var obj2; 
 

 
if (widthOfBrowser < 500) { 
 
    hideImage = document.getElementsByTagName("body")[0]; 
 
    hideImage.style.backgroundImage = "url('')"; 
 
    parent = document.getElementById("parent"); 
 
    child1 = document.getElementById("removeMobile1"); 
 
    child2 = document.getElementById("removeMobile2"); 
 
    parent.removeChild(child1); 
 
    parent.removeChild(child2); 
 
} else { 
 
    intervalMain = setInterval(function() { 
 
    putTogether() 
 
    }, timeInterval); 
 

 
    function putTogether() { 
 
    T1 = setTimeout(function() { 
 
     image1() 
 
    }, time); //after 5 seconds the function alternatingImages1to2 will be executed 
 
    time += 5000; 
 
    T2 = setTimeout(function() { 
 
     image2() 
 
    }, time); //after 10 seconds the function alternatingImages2to3 will be executed 
 
    time += 10000; 
 
    T3 = setTimeout(function() { 
 
     image3() 
 
    }, time); 
 
    time = 5000; 
 

 
    } 
 

 
} 
 

 
function image1() { 
 
    obj1 = document.getElementsByTagName("body")[0]; 
 
    obj1 = obj1.style.backgroundImage = "url('')"; 
 
} 
 

 
function image2() { 
 

 
    obj2 = document.getElementsByTagName("body")[0]; 
 
    obj2 = obj2.style.backgroundImage = "url('')"; //placeholder img 
 

 

 
} 
 

 
function image3() { 
 
    obj3 = document.getElementsByTagName("body")[0]; 
 
    obj3 = obj3.style.backgroundImage = "url('')"; //placeholder image 
 
} 
 

 
// Related css
body { 
 
    background: url("https://c1.staticflickr.com/1/126/387606063_408c203f6c_b.jpg") no-repeat center center fixed; 
 
    -webkit-background-size: cover; 
 
    -moz-background-size: cover; 
 
    -o-background-size: cover; 
 
    background-size: cover; 
 
    /* This is to set the background */ 
 
}

+1

Was ist das '\' 'tun nach' var intervalMain ; \ "? – blex

+0

danke wow, wie habe ich das nicht bemerkt, danke – johnbumble

+1

Wenn ich dein Beispiel versuche, verschwindet das Bild nach 20 Sekunden, wie es sollte. Da Sie in 'image1()', 'image2()' und 'image3()' keine URLs haben, findet nichts statt. Was erwartest du stattdessen? – Barmar

Antwort

0

Sieht aus wie Sie die Bild-URLs hinzufügen vergessen:

obj1.style.backgroundImage = "url('')" 

werden sollten:

obj1.style.backgroundImage = "url('path/to/image.jpg')"