Made a simple slider on js. While I am learning and I don’t know much, so the implementation leaves much to be desired) how best to complete it?
var btn1 = document.querySelector(".rad1"); var btn2 = document.querySelector(".rad2"); var btn3 = document.querySelector(".rad3"); var one = document.querySelector(".yellow"); var two = document.querySelector(".green"); var three = document.querySelector(".red"); btn1.addEventListener("click", function(event) { one.classList.add("block-item-show"); two.classList.remove("block-item-show"); three.classList.remove("block-item-show"); }) btn2.addEventListener("click", function(event) { two.classList.add("block-item-show"); one.classList.remove("block-item-show"); three.classList.remove("block-item-show"); }) btn3.addEventListener("click", function(event) { three.classList.add("block-item-show"); one.classList.remove("block-item-show"); two.classList.remove("block-item-show"); }) html, body { margin: 0; padding: 0; } .block { position: relative; width: 600px; height: 300px; margin: 0 auto; background-color: skyblue; border: 2px solid #eee; } .block-item { display: none; position: absolute; top: 0; left: 0; width: 600px; height: 300px; } .block-item-show { display: block; } .yellow { background-color: yellow; } .green { background-color: green; } .red { background-color: red; } .rad1, .rad2, .rad3 { position: absolute; left: 50%; bottom: 10px; z-index: 2; margin-left: -50px; } .rad2 { margin-left: -30px; } .rad3 { margin-left: -10px; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="block"> <div class="block-item yellow"></div> <div class="block-item green"></div> <div class="block-item red"></div> <input class="rad1" type="radio" name="group"> <input class="rad2" type="radio" name="group"> <input class="rad3" type="radio" name="group"> </div> <script src="main.js"></script> </body> </html>