Skip to content

Commit

Permalink
update the transition of image
Browse files Browse the repository at this point in the history
  • Loading branch information
siyi0211 committed Jul 28, 2021
1 parent 0cb9fd3 commit 8ae0561
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 7 additions & 0 deletions image-carousel/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<body>
<div class="carousel">
<div class="image-container" id="imgs">
<img
src="https://images.unsplash.com/photo-1599561046251-bfb9465b4c44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1492&q=80"
alt="fourth-image"
/>
<img src="https://images.unsplash.com/photo-1599394022918-6c2776530abb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1458&q=80"
alt="first-image"
/>
Expand All @@ -24,6 +28,9 @@
src="https://images.unsplash.com/photo-1599561046251-bfb9465b4c44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1492&q=80"
alt="fourth-image"
/>
<img src="https://images.unsplash.com/photo-1599394022918-6c2776530abb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1458&q=80"
alt="first-image"
/>
</div>

<div class="buttons-container">
Expand Down
32 changes: 27 additions & 5 deletions image-carousel/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rightBtn = document.getElementById('right')

const img = document.querySelectorAll('#imgs img')

let idx = 0
let idx = 1

let interval = setInterval(run, 2000)

Expand All @@ -15,12 +15,34 @@ function run() {

function changeImage() {
if(idx > img.length - 1) {
idx = 0
} else if(idx < 0) {
idx = img.length - 1
}

imgs.style.transform = `translateX(${-idx * 500}px)`
imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`

setTimeout(function() {
idx = 1;
imgs.style.transition = '';
imgs.style.transform = `translateX(${-500}px)`
},0)
}
else if(idx < 0){
idx = 0

imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`

setTimeout(function() {
idx = img.length - 2;
imgs.style.transition = '';
imgs.style.transform = `translateX(${-idx * 500}px)`
},0)
}
else{
imgs.style.transition = 'all, linear, 1s';
imgs.style.transform = `translateX(${-idx * 500}px)`
}

}

function resetInterval() {
Expand Down
3 changes: 1 addition & 2 deletions image-carousel/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ img {

.image-container {
display: flex;
transform: translateX(0);
transition: transform 0.5s ease-in-out;
transform: translateX(-500px);
}

.buttons-container {
Expand Down

0 comments on commit 8ae0561

Please sign in to comment.