Sprawdź np. taki zapis
<style>
img {
display: block;
object-fit: cover;
}
button {
width: 2rem;
font-size: 1rem;
}
button + button {
margin-left: .5rem;
}
</style>
<div id="background" >
<img src="https://picsum.photos/360/400?random=1" class="bg-image" id="slide-show">
<button>⤌</button>
<button>⤍</button>
</div>
<script>
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ CHANGE BACKGROUND ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
const images = [
'https://picsum.photos/360/400?random=1',
'https://picsum.photos/360/400?random=2',
'https://picsum.photos/360/400?random=3',
'https://picsum.photos/360/400?random=4',
'https://picsum.photos/360/400?random=5',
'https://picsum.photos/360/400?random=6',
'https://picsum.photos/360/400?random=7',
'https://picsum.photos/360/400?random=8',
'https://picsum.photos/360/400?random=9',
'https://picsum.photos/360/400?random=10',
'https://picsum.photos/360/400?random=11',
'https://picsum.photos/360/400?random=12'
];
let index = 0;
const preLoad = [];
for (let i=0; i<images.length; i++) {
preLoad[i] = new Image();
preLoad[i].src = images[i];
}
window.addEventListener('DOMContentLoaded', DOMload);
function DOMload() {
const buttons = document.querySelectorAll('#background button');
buttons[0].addEventListener('click', () => { changeSlide(-1); });
buttons[1].addEventListener('click', () => { changeSlide(+1); });
}
function changeSlide(direction) {
index = index + direction;
if (index > images.length - 1) index = 0;
if (index < 0) index = images.length - 1;
document.querySelector('#slide-show').src = preLoad[index].src;
}
</script>