A Pen by Alex Gallegos on CodePen.
Created
June 26, 2019 17:34
-
-
Save AlexGh12/2a098a8f5e2fac5482b01776383125c4 to your computer and use it in GitHub Desktop.
Horizontal Click and Drag Scrolling with JS
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="list-x"> | |
<div class="item item1"></div> | |
<div class="item item2"></div> | |
<div class="item item3"></div> | |
<div class="item item4"></div> | |
<div class="item item5"></div> | |
<div class="item item6"></div> | |
<div class="item item7"></div> | |
<div class="item item8"></div> | |
<div class="item item9"></div> | |
<div class="item item10"></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('.list-x'); | |
let isDown = false; | |
let startX; | |
let scrollLeft; | |
slider.addEventListener('mousedown', (e) => { | |
isDown = true; | |
slider.classList.add('active'); | |
startX = e.pageX - slider.offsetLeft; | |
scrollLeft = slider.scrollLeft; | |
}); | |
slider.addEventListener('mouseleave', () => { | |
isDown = false; | |
slider.classList.remove('active'); | |
}); | |
slider.addEventListener('mouseup', () => { | |
isDown = false; | |
slider.classList.remove('active'); | |
}); | |
slider.addEventListener('mousemove', (e) => { | |
if(!isDown) return; | |
e.preventDefault(); | |
const x = e.pageX - slider.offsetLeft; | |
const walk = (x - startX) * 3; //scroll-fast | |
slider.scrollLeft = scrollLeft - walk; | |
console.log(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
//GET THE GOOGLE FONT SPECIMEN | |
@import url(https://fonts.googleapis.com/css?family=Rubik); | |
body, | |
html { | |
color: #fff; | |
text-align: center; | |
background: #FFF; | |
font-family: Helvetica, sans-serif; | |
margin: 0; | |
} | |
.list-x { | |
position: relative; | |
width: 100%; | |
height: 100vh; | |
overflow-x: scroll; | |
overflow-y: hidden; | |
white-space: nowrap; | |
transition: all 0.2s; | |
/*transform: scale(0.98);*/ | |
will-change: transform; | |
user-select: none; | |
cursor: pointer; | |
} | |
.list-x.active { | |
background: rgba(255, 255, 255, 0.3); | |
cursor: grabbing; | |
cursor: -webkit-grabbing; | |
/*transform: scale(0.99);*/ | |
} | |
.item { | |
display: inline-block; | |
background: skyblue; | |
min-height: 150px; | |
min-width: 200px; | |
margin: 2px 5px; | |
border-radius: 10px; | |
-webkit-box-shadow: inset 0px 0px 6px -2px rgba(0, 0, 0, 0.75); | |
-moz-box-shadow: inset 0px 0px 6px -2px rgba(0, 0, 0, 0.75); | |
box-shadow: inset 0px 0px 6px -2px rgba(0, 0, 0, 0.75); | |
-webkit-transition: 0.2s ease-in-out 0s; | |
-moz-transition : 0.2s ease-in-out 0s; | |
-o-transition : 0.2s ease-in-out 0s; | |
} | |
.list-x.active .item { | |
-webkit-box-shadow: inset 0px 0px 12px -2px rgba(0, 0, 0, 0.75); | |
-moz-box-shadow: inset 0px 0px 12px -2px rgba(0, 0, 0, 0.75); | |
box-shadow: inset 0px 0px 12px -2px rgba(0, 0, 0, 0.75); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To prevent accidental clicking when dragging, Here is the updated code: