Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/node_modules
.eslintcache
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Slider

- Had problem while making the image disappear
- Added animation now and now this slider is as per the description
Binary file removed img-1.jpeg
Binary file not shown.
Binary file removed img-2.jpeg
Binary file not shown.
2 changes: 0 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<!-- <header><h1>Slider</h1></header> -->
<main>
<section class="slider-container">
<div class="image-container"></div>
Expand All @@ -18,7 +17,6 @@
</div>
</section>
</main>

<!-- javascript -->
<script src="./script.js"></script>
</body>
Expand Down
Binary file removed person-1.jpeg
Binary file not shown.
34 changes: 28 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,50 @@ const nextBtn = document.querySelector('.next-btn')

// global variables
let count = 0
let opacity = 0

// let imgElement = ni

// functions

// decrement function
function decrement() {
count -= 1
if (count < 0) count = images.length
if (count < 0) count = images.length - 1
// console.log(count)
}
// increment
function increment() {
count += 1
if (count >= images.length) count = 0
if (count === images.length) count = 0
// console.log(count)
}
function changeImage(src, alt, dataId) {
const imgElement = document.querySelector('.responsive-img')
// imgElement.classList.add('fade')
imgElement.style.opacity = 0.4
const fadeInterval = setInterval(() => {
// console.log('on interval')
opacity += 0.2
imgElement.style.opacity = opacity
if (opacity === 1) {
opacity = 0
clearInterval(fadeInterval)
}
}, 200)
imgElement.setAttribute('src', src)
imgElement.setAttribute('alt', alt)
imgElement.setAttribute('dataId', dataId)
}
// create image
function createImage(src, alt, dataId) {
// remove image
imgContainer.innerHTML = ''
// creation of image
const image = document.createElement('img')
// added class that makes image responsive
image.classList.add('responsive-img')
// added class that helps in animation
image.classList.add('fade')

image.setAttribute('src', src)
image.setAttribute('alt', alt)
image.setAttribute('data-id', dataId)
Expand All @@ -56,14 +78,14 @@ function createImage(src, alt, dataId) {
prevBtn.addEventListener('click', () => {
decrement()
const { name, src } = images[count]
createImage(src, name, count)
changeImage(src, name, count)
// console.log(count)
})
// next button
nextBtn.addEventListener('click', () => {
increment()
const { name, src } = images[count]
createImage(src, name, count)
changeImage(src, name, count)
})

// Execution
Expand Down
28 changes: 25 additions & 3 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
display: inline-block;
max-width: 100%;
}
.hidden-img {
opacity: 0;
/* .translucent-img {
opacity: 0.2;
}
.visible-image {
opacity: 1;
} */
.slider-container {
position: relative;
width: 100%;
Expand All @@ -23,7 +26,8 @@
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* this might eat the part of image if the images dont respect the aspect ratio of 1:1*/
/* this might eat the part of image
if the images don't respect the aspect ratio */
height: 50%;
width: 50%;
/* background-color: red; */
Expand Down Expand Up @@ -52,3 +56,21 @@
cursor: pointer;
color: #fff;
}
/* animation */
/* inspired from w3school*/

.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
0% {
opacity: 0.2;
}
50% {
opacity: 0.6;
}
100% {
opacity: 1;
}
}