Skip to content

Ability to specify region for Drag Scroll #96

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ polyfill works:
- **pressHoldThresholdPixels** is the drift in pixels that determines whether
a long press actually starts a long press, or starts a touch-drag instead.
This value is 0 by default.

- **scrolledRegion** is the region that is scrolled if allowDragScroll is true.
Defaults to globalThis (window)

## Thanks

- Thanks to Eric Bidelman for the great tutorial on HTML5 drag and drop:
Expand Down
38 changes: 33 additions & 5 deletions dist/drag-drop-touch.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ var DefaultConfiguration = {
isPressHoldMode: false,
pressHoldDelayMS: 400,
pressHoldMargin: 25,
pressHoldThresholdPixels: 0
pressHoldThresholdPixels: 0,
scrolledRegion: globalThis
};
var DragDropTouch = class {
_dragRoot;
Expand Down Expand Up @@ -318,7 +319,8 @@ var DragDropTouch = class {
this._isDropZone = this._dispatchEvent(e, `dragover`, target);
if (this.configuration.allowDragScroll) {
const delta = this._getHotRegionDelta(e);
globalThis.scrollBy(delta.x, delta.y);
let scrolledRegion = this.configuration.scrolledRegion || globalThis;
scrolledRegion.scrollBy(delta.x, delta.y);
}
}
}
Expand Down Expand Up @@ -421,18 +423,44 @@ var DragDropTouch = class {
const p = pointFrom(e);
return ((p.x - x) ** 2 + (p.y - y) ** 2) ** 0.5;
}
/**
* ...docs go here...
*/
_getScrolledRegionDimensions() {
let x = 0;
let y = 0;
let { innerWidth: w, innerHeight: h } = globalThis;
const scrolledRegion = this.configuration.scrolledRegion;
if (scrolledRegion && scrolledRegion !== globalThis) {
const bounds = scrolledRegion.getBoundingClientRect();
w = bounds.width;
h = bounds.height;
x = bounds.x;
y = bounds.y;
}
return { x, y, w, h };
}
/**
* ...docs go here...
* @param e
*/
_getHotRegionDelta(e) {
const { clientX: x, clientY: y } = e.touches[0];
const { innerWidth: w, innerHeight: h } = globalThis;
const {
x: scrolledRegionX,
y: scrolledRegionY,
w,
h
} = this._getScrolledRegionDimensions();
const { dragScrollPercentage, dragScrollSpeed } = this.configuration;
const rightEdge = scrolledRegionX + w;
const leftEdge = scrolledRegionX;
const topEdge = scrolledRegionY;
const bottomEdge = scrolledRegionY + h;
const v1 = dragScrollPercentage / 100;
const v2 = 1 - v1;
const dx = x < w * v1 ? -dragScrollSpeed : x > w * v2 ? +dragScrollSpeed : 0;
const dy = y < h * v1 ? -dragScrollSpeed : y > h * v2 ? +dragScrollSpeed : 0;
const dx = x < leftEdge + w * v1 ? -dragScrollSpeed : x > rightEdge * v2 ? +dragScrollSpeed : 0;
const dy = y < topEdge + h * v1 ? -dragScrollSpeed : y > bottomEdge * v2 ? +dragScrollSpeed : 0;
return { x: dx, y: dy };
}
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/drag-drop-touch.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading