A Pen by Hyperplexed on CodePen.
Created
March 29, 2025 21:30
Sliding Image Track
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="image-track" data-mouse-down-at="0" data-prev-percentage="0"> | |
<img class="image" src="https://images.unsplash.com/photo-1524781289445-ddf8f5695861?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1610194352361-4c81a6a8967e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1674&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1618202133208-2907bebba9e1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1495805442109-bf1cf975750b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1548021682-1720ed403a5b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1496753480864-3e588e0269b3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2134&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1613346945084-35cccc812dd5?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1759&q=80" draggable="false" /> | |
<img class="image" src="https://images.unsplash.com/photo-1516681100942-77d8e7f9dd97?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80" draggable="false" /> | |
</div> | |
<a id="source-link" class="meta-link" href="https://camillemormal.com" target="_blank"> | |
<i class="fa-solid fa-link"></i> | |
<span>Source</span> | |
</a> | |
<a id="yt-link" class="meta-link" href="https://youtu.be/PkADl0HubMY" target="_blank"> | |
<i class="fa-brands fa-youtube"></i> | |
<span>7 min tutorial</span> | |
</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const track = document.getElementById("image-track"); | |
const handleOnDown = e => track.dataset.mouseDownAt = e.clientX; | |
const handleOnUp = () => { | |
track.dataset.mouseDownAt = "0"; | |
track.dataset.prevPercentage = track.dataset.percentage; | |
} | |
const handleOnMove = e => { | |
if(track.dataset.mouseDownAt === "0") return; | |
const mouseDelta = parseFloat(track.dataset.mouseDownAt) - e.clientX, | |
maxDelta = window.innerWidth / 2; | |
const percentage = (mouseDelta / maxDelta) * -100, | |
nextPercentageUnconstrained = parseFloat(track.dataset.prevPercentage) + percentage, | |
nextPercentage = Math.max(Math.min(nextPercentageUnconstrained, 0), -100); | |
track.dataset.percentage = nextPercentage; | |
track.animate({ | |
transform: `translate(${nextPercentage}%, -50%)` | |
}, { duration: 1200, fill: "forwards" }); | |
for(const image of track.getElementsByClassName("image")) { | |
image.animate({ | |
objectPosition: `${100 + nextPercentage}% center` | |
}, { duration: 1200, fill: "forwards" }); | |
} | |
} | |
/* -- Had to add extra lines for touch events -- */ | |
window.onmousedown = e => handleOnDown(e); | |
window.ontouchstart = e => handleOnDown(e.touches[0]); | |
window.onmouseup = e => handleOnUp(e); | |
window.ontouchend = e => handleOnUp(e.touches[0]); | |
window.onmousemove = e => handleOnMove(e); | |
window.ontouchmove = e => handleOnMove(e.touches[0]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://codepen.io/Hyperplexed/pen/xxYJYjM/54407644e24173ad6019b766443bf2a6.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
height: 100vh; | |
width: 100vw; | |
background-color: black; | |
margin: 0rem; | |
overflow: hidden; | |
} | |
#image-track { | |
display: flex; | |
gap: 4vmin; | |
position: absolute; | |
left: 50%; | |
top: 50%; | |
transform: translate(0%, -50%); | |
user-select: none; /* -- Prevent image highlighting -- */ | |
} | |
#image-track > .image { | |
width: 40vmin; | |
height: 56vmin; | |
object-fit: cover; | |
object-position: 100% center; | |
} | |
/* -- YouTube Link Styles -- */ | |
body.menu-toggled > .meta-link > span { | |
color: rgb(30, 30, 30); | |
} | |
#source-link { | |
bottom: 60px; | |
} | |
#source-link > i { | |
color: rgb(94, 106, 210); | |
} | |
#yt-link > i { | |
color: rgb(239, 83, 80); | |
} | |
.meta-link { | |
align-items: center; | |
backdrop-filter: blur(3px); | |
background-color: rgba(255, 255, 255, 0.05); | |
border: 1px solid rgba(255, 255, 255, 0.1); | |
border-radius: 6px; | |
bottom: 10px; | |
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.1); | |
cursor: pointer; | |
display: inline-flex; | |
gap: 5px; | |
left: 10px; | |
padding: 10px 20px; | |
position: fixed; | |
text-decoration: none; | |
transition: background-color 400ms, border-color 400ms; | |
z-index: 10000; | |
} | |
.meta-link:hover { | |
background-color: rgba(255, 255, 255, 0.1); | |
border: 1px solid rgba(255, 255, 255, 0.2); | |
} | |
.meta-link > i, .meta-link > span { | |
height: 20px; | |
line-height: 20px; | |
} | |
.meta-link > span { | |
color: white; | |
font-family: "Rubik", sans-serif; | |
font-weight: 500; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment