I wanted to make a fading of different background images but it affect the sentences on it too.
Here is a simple html to test :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="css/style.css" rel="stylesheet" />
<title>Title</title>
<script type="text/javascript" src="js/main.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<p id="myID">This sentence have a background who should <b> change ! </b><br><br><br><br><br><br><br><br>Yea...</p>
</body>
</html>
I have done a class, with array of images, id and index and when its fading in/out its fading the text too. Js :
class changingImageObject {
constructor(img1, img2, img3, id) {
this.image = [img1, img2, img3];
this.id = id;
this.x = 0;
this.eraseBg = this.eraseBg.bind(this);
this.newBg = this.newBg.bind(this);
this.startTimer = this.startTimer.bind(this);
};
newBg() {
this.x = (this.x === 2) ? 0 : this.x + 1;
$(this.id).css('background-image', 'url(' + this.image[this.x] + ')');
$(this.id).fadeIn(1000); //this is new, will fade in smoothly
};
eraseBg() {
$(this.id).fadeOut(1000, this.newBg); //this is new, will fade out smoothly
};
startTimer() { //Change image after 3sec (looping)
setInterval(this.eraseBg, 3000);
};
}
const test = new changingImageObject("assets/img1.jpeg", "assets/img2.jpeg", "assets/img3.jpeg", '#myID');
test.startTimer();
Thx for reading.
Thansk to melancia i know that i need to use the rgba to make a pretty fading in/out without interfering with the childrens. Here is the code :