• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Navigation / Drag to scroll

Drag to scroll

drag to scroll

Users often use the mouse to scroll in a scrollable container. In addition to that, some applications also allow users to scroll by dragging the element. You can see that feature implemented in a PDF viewer, Figma and many more.

This post shows you a simple way to archive that feature with vanilla JavaScript.

Assume that we have a scrollable container as below:

<div id="container" class="container">...</div>

The element must have at least two CSS properties:

.container {
    cursor: grab;
    overflow: auto;
}

The cursor: grab indicates that the element can be clicked and dragged.

Scroll to given position

As long as the element is scrollable, we can scroll it to given position by setting the scrollTop or scrollLeft property:

const ele = document.getElementById('container');
ele.scrollTop = 100;
ele.scrollLeft = 150;

Drag to scroll

The implementation is quite straightforward. We start with handling the mousedown event which occurs when user clicks the element:

let pos = { top: 0, left: 0, x: 0, y: 0 };

const mouseDownHandler = function (e) {
    pos = {
        // The current scroll
        left: ele.scrollLeft,
        top: ele.scrollTop,
        // Get the current mouse position
        x: e.clientX,
        y: e.clientY,
    };

    document.addEventListener('mousemove', mouseMoveHandler);
    document.addEventListener('mouseup', mouseUpHandler);
};

pos stores the current scroll and mouse positions. When user moves the mouse, we calculate how far it has been moved, and then scroll to the element to the same position:

const mouseMoveHandler = function (e) {
    // How far the mouse has been moved
    const dx = e.clientX - pos.x;
    const dy = e.clientY - pos.y;

    // Scroll the element
    ele.scrollTop = pos.top - dy;
    ele.scrollLeft = pos.left - dx;
};

Last but not least, we can improve the user experience by setting some CSS properties when user starts moving the mouse:

const mouseDownHandler = function(e) {
    // Change the cursor and prevent user from selecting the text
    ele.style.cursor = 'grabbing';
    ele.style.userSelect = 'none';
    ...
};

These CSS properties are reset when the mouse is released:

const mouseUpHandler = function () {
    document.removeEventListener('mousemove', mouseMoveHandler);
    document.removeEventListener('mouseup', mouseUpHandler);

    ele.style.cursor = 'grab';
    ele.style.removeProperty('user-select');
};

Source

https://htmldom.dev/drag-to-scroll/

Navigation

Related Snippets:

  • Scroll to an element smoothly (smooth scroll)
  • Scroll to an element smoothly
  • Automatic Breadcrumbs
  • Using the Target Attribute

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers