Skip to content

Commit

Permalink
Scroll sensitivity control in each direction
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-prudhomme committed Sep 24, 2020
1 parent 6e3ac78 commit 1380ac3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 11 additions & 0 deletions packages/plugins/auto-scroll/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ This function should return `'continue'` if it wishes to allow Sortable's native
#### `scrollSensitivity` option
Defines how near the mouse must be to an edge to start scrolling.

Instead of a number, you can use an object to configure `top`, `bottom`, `left` and `right` scroll sensitivity.

```js
var scrollSensitivity = {
top: 130, // take a fixed header height into account
bottom: 30,
left: 30,
right: 30
};
```


---

Expand Down
13 changes: 8 additions & 5 deletions packages/plugins/auto-scroll/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ const autoScroll = throttle(function (evt, options, rootEl, isFallback) {
if (!options.scroll) return;
const x = (evt.touches ? evt.touches[0] : evt).clientX,
y = (evt.touches ? evt.touches[0] : evt).clientY,
sens = options.scrollSensitivity,
sensTop = typeof options.scrollSensitivity === 'object' ? options.scrollSensitivity.top : options.scrollSensitivity,
sensBottom = typeof options.scrollSensitivity === 'object' ? options.scrollSensitivity.bottom : options.scrollSensitivity,
sensLeft = typeof options.scrollSensitivity === 'object' ? options.scrollSensitivity.left : options.scrollSensitivity,
sensRight = typeof options.scrollSensitivity === 'object' ? options.scrollSensitivity.right : options.scrollSensitivity,
speed = options.scrollSpeed,
winScroller = getWindowScrollingElement();

Expand Down Expand Up @@ -235,15 +238,15 @@ const autoScroll = throttle(function (evt, options, rootEl, isFallback) {
let vx =
canScrollX &&
//@ts-ignore
(Math.abs(right - x) <= sens && scrollPosX + width < scrollWidth) -
(Math.abs(right - x) <= sensRight && scrollPosX + width < scrollWidth) -
//@ts-ignore
(Math.abs(left - x) <= sens && !!scrollPosX);
(Math.abs(left - x) <= sensLeft && !!scrollPosX);
let vy =
canScrollY &&
//@ts-ignore
(Math.abs(bottom - y) <= sens && scrollPosY + height < scrollHeight) -
(Math.abs(bottom - y) <= sensBottom && scrollPosY + height < scrollHeight) -
//@ts-ignore
(Math.abs(top - y) <= sens && !!scrollPosY);
(Math.abs(top - y) <= sensTop && !!scrollPosY);

if (!autoScrolls[layersOut]) {
for (let i = 0; i <= layersOut; i++) {
Expand Down

0 comments on commit 1380ac3

Please sign in to comment.