mdbootstrap / perfect-scrollbar

Minimalistic but perfect custom scrollbar plugin. Get more free components with Material Design for Bootstrap UI Kit (link below)
https://perfectscrollbar.com/
MIT License
346 stars 66 forks source link

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

Open liamcharmer opened 1 year ago

liamcharmer commented 1 year ago

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