I recently mastered the theoretical foundations of JS and decided to consolidate knowledge by making a slider.
var slide_left = document.getElementById('left'), slide_right = document.getElementById('right'), images = document.getElementsByTagName('img'), idx = 1; slide_right.addEventListener('click', moveLeft); function moveLeft() { slide_left.style.display = 'block'; images[idx].style.display = 'none'; images[++idx].style.display = 'block'; if (idx === images.length - 1) { slide_right.style.display = 'none'; } } slide_left.addEventListener('click', moveRight); function moveRight() { slide_right.style.display = 'block'; images[idx].style.display = 'none'; images[--idx].style.display = 'none'; if (idx === 0) { slide_left.style.display = 'none'; } } .slides { position: relative; height: 300px; padding: 0; margin: 0; list-style-type: none; } .slide { position: absolute; left: 0; top: 0; width: 100%; height: 100%; opacity: 10; z-index: 1; font-size: 40px; padding: 40px; box-sizing: border-box; background: #333; color: #fff; } .showing { opacity: 1; z-index: 2; } img { width: 100%; height: 100%; } .cont_button { display: flex; justify-content: center; } #left, #right { font-size: 50px; border: none; margin-top: 20px; margin-left: 12px; margin-right: 12px; border-radius: 14px; background-color: #d6b511; color: black; transition: .1s all; } #left:hover, #right:hover { background-color: #ccff00; } #first_wallpaper, #second_wallpaper, #three_wallpaper {} <ul class="slides"> <li class="slide"> <img id="first_wallpaper" src="http://via.placeholder.com/250x250/000000/red" alt=""> </li> <li class="slide"> <img id="second_wallpaper" src="http://via.placeholder.com/250x250/000000/green" alt=""> </li> <li class="slide"> <img id="three_wallpaper" src="http://via.placeholder.com/250x250/000000/blue" alt=""> </li> </ul> <div class="cont_button"> <button id="left">left</button> <button id="right">right</button> </div> The code is very simple, a part was taken from the same site, and a part was written by myself. The problem is that, as I understand it, it is necessary to attach the index clearly to each picture, nothing comes to mind, I understand the logic, but it does not work out.