- Dapatkan pautan
- X
- E-mel
- Apl Lain
Demo
HTML view ▼
<section class="beforeAfter">
<div class="beforeAfter__img beforeAfter__before">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgugUJGzhMCjgqgL6Y_XhHcn0T1N2Jym4roLF08M4PvRl1bQXLIabnupun2t7HNLg2-NdvRvsZeXaPtgVc0-xFpu6Gh3dvM2j3RA_J7KQ4lpn5siq7OL9mOqcyvWIR2VxdqlRk5qV5XWymz0pEyyEmCo925VhV2s2R8LfGTlvhR20BZwrkIs8Khb3Z1Lb8/s600/DS%20pantai%20klebang%201.JPG" alt="before">
</div>
<div class="beforeAfter__img beforeAfter__after">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ2lSkCFjmoJ1E-ZUV_9t410y3sdsHmpigpFYb0DFVq9T0qIL0suBgbdC3ij0okO9g4E8WWsshphm_uKjpDYdrTbwWwqM2-WwfGldrTHwk-MJWSBk8BiTNgFzbeT7f0brvbxV6kIBVLYmYitQH7o-5rrrdN6dKwsKjo3lqU8daOEZrtFUdIgd-4GZS4fE/s600/DS%20pantai%20klebang%202.JPG" alt="after">
</div>
</section>
<style>
.beforeAfter {
max-width: 800px;
width: 100%;
aspect-ratio: 3/2;
position: relative;
z-index: 1;
margin: 0 auto;
border-radius: 10px;
overflow: hidden;
}
.beforeAfter__img {
width: 100%;
height: 100%;
position: absolute;
top: 0;
}
.beforeAfter__img img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.beforeAfter__after {
position: absolute;
overflow: hidden;
top: 0;
transform: translateX(50%);
transition: transform 0.2s ease-out;
}
.beforeAfter__after:before {
content: "";
position: absolute;
z-index: 2;
top: 0;
left: 0;
width: 1px;
height: 100%;
background: white;
}
.beforeAfter__after img {
transform: translateX(-50%);
transition: transform 0.2s ease-out;
}
</style>
<script>
const beforeAfter = document.querySelector('.beforeAfter')
const after = beforeAfter.querySelector('.beforeAfter__after')
const afterImg = after.querySelector('img')
const handleMouseMove = (e) => {
e.preventDefault()
e.stopPropagation()
const { left, width } = beforeAfter.getBoundingClientRect()
const x = e.touches ? e.touches[0].clientX : e.clientX
const progress = ((x - left) / width) * 100
after.style.transform = `translateX(${progress}%)`
afterImg.style.transform = `translateX(${-progress}%)`
}
['mousemove', 'touchmove', 'touchstart'].forEach((e) => {
beforeAfter.addEventListener(e, handleMouseMove)
})
</script>
<div class="beforeAfter__img beforeAfter__before">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgugUJGzhMCjgqgL6Y_XhHcn0T1N2Jym4roLF08M4PvRl1bQXLIabnupun2t7HNLg2-NdvRvsZeXaPtgVc0-xFpu6Gh3dvM2j3RA_J7KQ4lpn5siq7OL9mOqcyvWIR2VxdqlRk5qV5XWymz0pEyyEmCo925VhV2s2R8LfGTlvhR20BZwrkIs8Khb3Z1Lb8/s600/DS%20pantai%20klebang%201.JPG" alt="before">
</div>
<div class="beforeAfter__img beforeAfter__after">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ2lSkCFjmoJ1E-ZUV_9t410y3sdsHmpigpFYb0DFVq9T0qIL0suBgbdC3ij0okO9g4E8WWsshphm_uKjpDYdrTbwWwqM2-WwfGldrTHwk-MJWSBk8BiTNgFzbeT7f0brvbxV6kIBVLYmYitQH7o-5rrrdN6dKwsKjo3lqU8daOEZrtFUdIgd-4GZS4fE/s600/DS%20pantai%20klebang%202.JPG" alt="after">
</div>
</section>
<style>
.beforeAfter {
max-width: 800px;
width: 100%;
aspect-ratio: 3/2;
position: relative;
z-index: 1;
margin: 0 auto;
border-radius: 10px;
overflow: hidden;
}
.beforeAfter__img {
width: 100%;
height: 100%;
position: absolute;
top: 0;
}
.beforeAfter__img img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.beforeAfter__after {
position: absolute;
overflow: hidden;
top: 0;
transform: translateX(50%);
transition: transform 0.2s ease-out;
}
.beforeAfter__after:before {
content: "";
position: absolute;
z-index: 2;
top: 0;
left: 0;
width: 1px;
height: 100%;
background: white;
}
.beforeAfter__after img {
transform: translateX(-50%);
transition: transform 0.2s ease-out;
}
</style>
<script>
const beforeAfter = document.querySelector('.beforeAfter')
const after = beforeAfter.querySelector('.beforeAfter__after')
const afterImg = after.querySelector('img')
const handleMouseMove = (e) => {
e.preventDefault()
e.stopPropagation()
const { left, width } = beforeAfter.getBoundingClientRect()
const x = e.touches ? e.touches[0].clientX : e.clientX
const progress = ((x - left) / width) * 100
after.style.transform = `translateX(${progress}%)`
afterImg.style.transform = `translateX(${-progress}%)`
}
['mousemove', 'touchmove', 'touchstart'].forEach((e) => {
beforeAfter.addEventListener(e, handleMouseMove)
})
</script>
Rujukan CSS & JS di codepen.io/supah/pen/mdvbqqP
Catatan oleh
Orandableg
- Dapatkan pautan
- X
- E-mel
- Apl Lain
Ulasan