Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click and Drag to scroll similar to tap and scroll functionality? #150

Open
liamcharmer opened this issue Feb 26, 2023 · 1 comment
Open

Comments

@liamcharmer
Copy link

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?

@liamcharmer
Copy link
Author

liamcharmer commented Feb 26, 2023

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);
      });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant