Open liamcharmer opened 1 year ago
Ah incase anyone wanted to implement a vanilla JS way.
Found at https://codepen.io/thenutz/pen/VwYeYEE
const slider = document.querySelector(".ps");
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);
});
Is it possible to make the scrollable content area draggable?
I know you can drag the scroll bar itself to drag across. But is it possible similar to how you would drag and scroll via touch events on mobile. To implement this on desktop via mouse?