A Pen by Chase Harrison on CodePen.
Created
February 26, 2019 12:51
-
-
Save ChaseH88/f3362528d93c9b054691220949b50f76 to your computer and use it in GitHub Desktop.
JavaScript - Click and Drag
This file contains 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 class="items"> | |
<div class="item item1">1</div> | |
<div class="item item2">2</div> | |
<div class="item item3">3</div> | |
<div class="item item4">4</div> | |
<div class="item item5">5</div> | |
<div class="item item6">6</div> | |
<div class="item item7">7</div> | |
<div class="item item8">8</div> | |
<div class="item item9">9</div> | |
<div class="item item10">10</div> | |
<div class="item ite115">11</div> | |
</div> |
This file contains 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 slider = document.querySelector(".items"); | |
let isDown = false; | |
let startX; | |
let scrollLeft; | |
// EVENTS | |
slider.addEventListener("mousedown", (e) => { | |
isDown = true; | |
slider.classList.add("active"); | |
startX = e.pageX - slider.offsetLeft; | |
scrollLeft = slider.scrollLeft; | |
console.log(scrollLeft) | |
}); | |
slider.addEventListener("mouseleave", () => { | |
isDown = false; | |
slider.classList.remove("active"); | |
}); | |
slider.addEventListener("mouseup", () => { | |
isDown = false; | |
slider.classList.remove("active"); | |
}); | |
slider.addEventListener("mousemove", (e) => { | |
e.preventDefault(); | |
// return if false | |
if(!isDown) return; | |
const x = e.pageX - slider.offsetLeft | |
const walk = x - startX; | |
slider.scrollLeft = scrollLeft - walk; | |
}); |
This file contains 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
html { | |
box-sizing: border-box; | |
background: url('https://source.unsplash.com/NFs6dRTBgaM/2000x2000') fixed; | |
background-size: cover; | |
} | |
*, *:before, *:after { | |
box-sizing: inherit; | |
} | |
body { | |
min-height: 100vh; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
font-family: sans-serif; | |
font-size: 20px; | |
margin: 0; | |
} | |
.items { | |
height: 800px; | |
padding: 100px; | |
width: 100%; | |
border: 1px solid white; | |
overflow-x: scroll; | |
overflow-y: hidden; | |
white-space: nowrap; | |
user-select: none; | |
cursor: pointer; | |
transition: all 0.2s; | |
transform: scale(0.98); | |
will-change: transform; | |
position: relative; | |
background: rgba(255,255,255,0.1); | |
font-size: 0; | |
perspective: 500px; | |
} | |
.items.active { | |
background: rgba(255,255,255,0.3); | |
cursor: grabbing; | |
cursor: -webkit-grabbing; | |
transform: scale(1); | |
} | |
.item { | |
width: 200px; | |
height: calc(100% - 40px); | |
display: inline-flex; | |
align-items: center; | |
justify-content: center; | |
font-size: 80px; | |
font-weight: 100; | |
color: rgba(0,0,0,0.15); | |
box-shadow: inset 0 0 0 10px rgba(0,0,0,0.15); | |
} | |
.item:nth-child(9n+1) { background: dodgerblue;} | |
.item:nth-child(9n+2) { background: goldenrod;} | |
.item:nth-child(9n+3) { background: paleturquoise;} | |
.item:nth-child(9n+4) { background: gold;} | |
.item:nth-child(9n+5) { background: cadetblue;} | |
.item:nth-child(9n+6) { background: tomato;} | |
.item:nth-child(9n+7) { background: lightcoral;} | |
.item:nth-child(9n+8) { background: darkslateblue;} | |
.item:nth-child(9n+9) { background: rebeccapurple;} | |
.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); } | |
.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment