@daysnap/utils 0.0.53 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/mousewheel.d.ts +14 -0
- package/es/mousewheel.js +61 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/mousewheel.d.ts +14 -0
- package/lib/mousewheel.js +65 -0
- package/package.json +1 -1
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MousewheelScrolling {
|
|
2
|
+
(result: {
|
|
3
|
+
direction: 'down' | 'up';
|
|
4
|
+
delta: number;
|
|
5
|
+
}): void;
|
|
6
|
+
}
|
|
7
|
+
interface MousewheelOptions {
|
|
8
|
+
scrollingSpeed?: number;
|
|
9
|
+
scrollDelay?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function mousewheel(scrolling: MousewheelScrolling, options?: MousewheelOptions): {
|
|
12
|
+
listener: (e: any) => false | undefined;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
package/es/mousewheel.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
function getAverage(elements, number) {
|
|
2
|
+
let sum = 0;
|
|
3
|
+
//taking `number` elements from the end to make the average, if there are not enought, 1
|
|
4
|
+
const lastElements = elements.slice(Math.max(elements.length - number, 1));
|
|
5
|
+
for (let i = 0; i < lastElements.length; i++) {
|
|
6
|
+
sum = sum + lastElements[i];
|
|
7
|
+
}
|
|
8
|
+
return Math.ceil(sum / number);
|
|
9
|
+
}
|
|
10
|
+
export function mousewheel(scrolling, options) {
|
|
11
|
+
const { scrollingSpeed = 700, scrollDelay = 50 } = Object.assign({}, options);
|
|
12
|
+
let scrollings = [];
|
|
13
|
+
let prevTime = Date.now();
|
|
14
|
+
let lastAnimation = 0;
|
|
15
|
+
function isMoving() {
|
|
16
|
+
const timeNow = new Date().getTime();
|
|
17
|
+
// Cancel scroll if currently animating or within quiet period
|
|
18
|
+
if (timeNow - lastAnimation < scrollDelay + scrollingSpeed) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const listener = (e) => {
|
|
24
|
+
const curTime = Date.now();
|
|
25
|
+
const delta = e.wheelDelta || -e.deltaY || -e.detail;
|
|
26
|
+
// const delta = Math.max(-1, Math.min(1, value))
|
|
27
|
+
const horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
|
|
28
|
+
const isScrollingVertically = Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta) ||
|
|
29
|
+
Math.abs(e.deltaX) < Math.abs(e.deltaY) ||
|
|
30
|
+
!horizontalDetection;
|
|
31
|
+
//Limiting the array to 150 (lets not waste memory!)
|
|
32
|
+
if (scrollings.length > 149) {
|
|
33
|
+
scrollings.shift();
|
|
34
|
+
}
|
|
35
|
+
//keeping record of the previous scrollings
|
|
36
|
+
scrollings.push(Math.abs(delta));
|
|
37
|
+
//time difference between the last scroll and the current one
|
|
38
|
+
const timeDiff = curTime - prevTime;
|
|
39
|
+
prevTime = curTime;
|
|
40
|
+
//haven't they scrolled in a while?
|
|
41
|
+
//(enough to be consider a different scrolling action to scroll another section)
|
|
42
|
+
if (timeDiff > 200) {
|
|
43
|
+
//emptying the array, we dont care about old scrollings for our averages
|
|
44
|
+
scrollings = [];
|
|
45
|
+
}
|
|
46
|
+
if (!isMoving()) {
|
|
47
|
+
const averageEnd = getAverage(scrollings, 10);
|
|
48
|
+
const averageMiddle = getAverage(scrollings, 70);
|
|
49
|
+
const isAccelerating = averageEnd >= averageMiddle;
|
|
50
|
+
if (isAccelerating && isScrollingVertically) {
|
|
51
|
+
scrolling({
|
|
52
|
+
direction: delta < 0 ? 'up' : 'down',
|
|
53
|
+
delta,
|
|
54
|
+
});
|
|
55
|
+
lastAnimation = Date.now();
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
return { listener };
|
|
61
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -75,6 +75,7 @@ __exportStar(require("./isUndefined"), exports);
|
|
|
75
75
|
__exportStar(require("./isWeixin"), exports);
|
|
76
76
|
__exportStar(require("./isWindow"), exports);
|
|
77
77
|
__exportStar(require("./kebabCase"), exports);
|
|
78
|
+
__exportStar(require("./mousewheel"), exports);
|
|
78
79
|
__exportStar(require("./normalizePath"), exports);
|
|
79
80
|
__exportStar(require("./omit"), exports);
|
|
80
81
|
__exportStar(require("./padding"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MousewheelScrolling {
|
|
2
|
+
(result: {
|
|
3
|
+
direction: 'down' | 'up';
|
|
4
|
+
delta: number;
|
|
5
|
+
}): void;
|
|
6
|
+
}
|
|
7
|
+
interface MousewheelOptions {
|
|
8
|
+
scrollingSpeed?: number;
|
|
9
|
+
scrollDelay?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function mousewheel(scrolling: MousewheelScrolling, options?: MousewheelOptions): {
|
|
12
|
+
listener: (e: any) => false | undefined;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mousewheel = void 0;
|
|
4
|
+
function getAverage(elements, number) {
|
|
5
|
+
let sum = 0;
|
|
6
|
+
//taking `number` elements from the end to make the average, if there are not enought, 1
|
|
7
|
+
const lastElements = elements.slice(Math.max(elements.length - number, 1));
|
|
8
|
+
for (let i = 0; i < lastElements.length; i++) {
|
|
9
|
+
sum = sum + lastElements[i];
|
|
10
|
+
}
|
|
11
|
+
return Math.ceil(sum / number);
|
|
12
|
+
}
|
|
13
|
+
function mousewheel(scrolling, options) {
|
|
14
|
+
const { scrollingSpeed = 700, scrollDelay = 50 } = Object.assign({}, options);
|
|
15
|
+
let scrollings = [];
|
|
16
|
+
let prevTime = Date.now();
|
|
17
|
+
let lastAnimation = 0;
|
|
18
|
+
function isMoving() {
|
|
19
|
+
const timeNow = new Date().getTime();
|
|
20
|
+
// Cancel scroll if currently animating or within quiet period
|
|
21
|
+
if (timeNow - lastAnimation < scrollDelay + scrollingSpeed) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const listener = (e) => {
|
|
27
|
+
const curTime = Date.now();
|
|
28
|
+
const delta = e.wheelDelta || -e.deltaY || -e.detail;
|
|
29
|
+
// const delta = Math.max(-1, Math.min(1, value))
|
|
30
|
+
const horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
|
|
31
|
+
const isScrollingVertically = Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta) ||
|
|
32
|
+
Math.abs(e.deltaX) < Math.abs(e.deltaY) ||
|
|
33
|
+
!horizontalDetection;
|
|
34
|
+
//Limiting the array to 150 (lets not waste memory!)
|
|
35
|
+
if (scrollings.length > 149) {
|
|
36
|
+
scrollings.shift();
|
|
37
|
+
}
|
|
38
|
+
//keeping record of the previous scrollings
|
|
39
|
+
scrollings.push(Math.abs(delta));
|
|
40
|
+
//time difference between the last scroll and the current one
|
|
41
|
+
const timeDiff = curTime - prevTime;
|
|
42
|
+
prevTime = curTime;
|
|
43
|
+
//haven't they scrolled in a while?
|
|
44
|
+
//(enough to be consider a different scrolling action to scroll another section)
|
|
45
|
+
if (timeDiff > 200) {
|
|
46
|
+
//emptying the array, we dont care about old scrollings for our averages
|
|
47
|
+
scrollings = [];
|
|
48
|
+
}
|
|
49
|
+
if (!isMoving()) {
|
|
50
|
+
const averageEnd = getAverage(scrollings, 10);
|
|
51
|
+
const averageMiddle = getAverage(scrollings, 70);
|
|
52
|
+
const isAccelerating = averageEnd >= averageMiddle;
|
|
53
|
+
if (isAccelerating && isScrollingVertically) {
|
|
54
|
+
scrolling({
|
|
55
|
+
direction: delta < 0 ? 'up' : 'down',
|
|
56
|
+
delta,
|
|
57
|
+
});
|
|
58
|
+
lastAnimation = Date.now();
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return { listener };
|
|
64
|
+
}
|
|
65
|
+
exports.mousewheel = mousewheel;
|