Skip to content

Instantly share code, notes, and snippets.

@K-Vishwak
Created June 8, 2024 14:59
Show Gist options
  • Save K-Vishwak/c76053d797f97c2615e40746266a8a18 to your computer and use it in GitHub Desktop.
Save K-Vishwak/c76053d797f97c2615e40746266a8a18 to your computer and use it in GitHub Desktop.
image slider
<html>
<head>
<title>Image Slider</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<style>
body {
font-family: 'Courier New', Courier, monospace;
margin: 0;
padding: 0;
}
div {
width: 100%;
height: 40%;
}
img {
width: 100%;
height: 100%;
}
#buttons {
margin: 0 auto;
text-align: center;
}
i {
font-size: 50px;
color: deepskyblue;
}
</style>
</head>
<body>
<div id="list">
<img src="https://fastly.picsum.photos/id/10/2500/1667.jpg?hmac=J04WWC_ebchx3WwzbM-Z4_KC_LeLBWr5LZMaAkWkF68">
</div>
<div id="buttons">
<i onclick="previousClick()" class="bi bi-caret-left-fill"></i>
<i onclick="nextClick()" class="bi bi-caret-right-fill"></i>
</div>
<script>
const imageList = [
'https://fastly.picsum.photos/id/10/2500/1667.jpg?hmac=J04WWC_ebchx3WwzbM-Z4_KC_LeLBWr5LZMaAkWkF68',
'https://fastly.picsum.photos/id/11/2500/1667.jpg?hmac=xxjFJtAPgshYkysU_aqx2sZir-kIOjNR9vx0te7GycQ',
'https://fastly.picsum.photos/id/12/2500/1667.jpg?hmac=Pe3284luVre9ZqNzv1jMFpLihFI6lwq7TPgMSsNXw2w',
'https://fastly.picsum.photos/id/13/2500/1667.jpg?hmac=SoX9UoHhN8HyklRA4A3vcCWJMVtiBXUg0W4ljWTor7s',
'https://fastly.picsum.photos/id/14/2500/1667.jpg?hmac=ssQyTcZRRumHXVbQAVlXTx-MGBxm6NHWD3SryQ48G-o'
];
const divEle = document.getElementById('list');
const imgEle = document.createElement('img');
let count = 1;
let imgInterval;
document.addEventListener('DOMContentLoaded', () => {
onLoad();
});
const onLoad = () => {
imgInterval = setInterval(() => {
if (count >= imageList.length) count = 0;
divEle.innerHTML = '';
imgEle.src = imageList[count];
divEle.appendChild(imgEle);
count++;
}, 2000)
};
const previousClick = () => handleAction(count - 2);
const nextClick = () => handleAction(count + 1);
const handleAction = (actionCount) => {
if (actionCount >= imageList.length) actionCount = 0;
if (actionCount < 0) actionCount = imageList.length - 1;
clearInterval(imgInterval);
divEle.innerHTML = '';
imgEle.src = imageList[actionCount];
divEle.appendChild(imgEle);
count = actionCount + 1;
onLoad();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment