<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Slider</title>
<style>
@font-face {
font-family: "firacode";
src: url(./FiraCode-Medium.ttf);
}
* {
user-select: none;
box-sizing: border-box;
margin: auto;
font-family: "firacode";
text-align: center;
color: #dbdbdb;
transition: all 0.4s ease;
}
body {
background-color: #101010;
}
header {
width: 70%;
max-width: 550px;
margin: 20px auto;
padding: 17px;
background-color: #202020;
border: 7px solid #313131;
border-radius: 15px;
}
#slider {
position: relative;
width: 90%;
max-width: 620px;
margin: 20px auto;
padding: 15px;
background-color: #202020;
border: 7px solid #313131;
border-radius: 15px;
}
button {
all: unset;
display: inline;
padding: 15px;
margin: 5px auto;
width: 30%;
height: auto;
color: white;
background-color: #303030;
border: 2px solid rgb(68, 68, 68);
border-radius: 4px;
transition: all 0.4s ease-in-out;
font-size: 20px;
}
button:hover {
cursor: pointer;
background-color: #404040;
transform: translateY(-2px);
}
img {
width: 80%;
max-width: 370px;
height: auto;
background-color: #252525;
border: 5px solid #505050;
border-radius: 50%;
transition: opacity 0.5s ease-in-out;
}
</style>
</head>
<body>
<header><h1>Slider</h1></header>
<section id="slider">
<img src="1.png">
<button onclick="change(false)"> ← </button>
<button onclick="change(true)"> → </button>
</section>
<script>
let img = document.querySelector("#slider img")
let items = ["1.png","2.png","3.png","4.png","5.png"]
let index = 0
let changeSrc = ()=>{
img.style.opacity = 0
setTimeout(()=>{
img.src = items[index]
},500)
img.onload = ()=>{
img.style.opacity = 1
}
}
let change = (isNext)=>{
if (isNext) {
index++
if (index >= items.length){index = 0}
changeSrc(img, items[index])
} else {
index--
if (index == -1){index = items.length-1}
changeSrc(img, items[index])
}
}
</script>
</body>
</html>