@twreporter/core 1.4.1 → 1.4.2-rc.0
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/CHANGELOG.md +8 -0
- package/lib/utils/smooth-scroll.js +69 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.4.2-rc.0](https://github.com/twreporter/twreporter-npm-packages/compare/@twreporter/core@1.4.1...@twreporter/core@1.4.2-rc.0) (2022-07-27)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @twreporter/core
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [1.4.1](https://github.com/twreporter/twreporter-npm-packages/compare/@twreporter/core@1.4.1-rc.0...@twreporter/core@1.4.1) (2022-06-07)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @twreporter/core
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = _default;
|
|
7
|
+
// NOTE:
|
|
8
|
+
// 1. behavior: 'smooth' of window.scroll() is not supported by Safari, so we need a custom smooth scroll.
|
|
9
|
+
// 2. The underline anchors in TOC is actually not <a> but <H1>, so we can't use 'smooth-scroll' lib
|
|
10
|
+
// which is developed specifically for <a>. Here is a workaround to copy the smooth scroll behavior
|
|
11
|
+
// of 'smoothScroll' lib. (ref: https://github.com/alicelieutier/smoothScroll/blob/master/smoothscroll.js)
|
|
12
|
+
// P.S. We don't use 'smoothScroll' directly due to it will apply smooth scroll behavior to ALL anchors
|
|
13
|
+
// in the website automatically.
|
|
14
|
+
var defaultDuration = 500;
|
|
15
|
+
|
|
16
|
+
function _default(el, duration, callback, context) {
|
|
17
|
+
var position = function position(start, end, elapsed, duration) {
|
|
18
|
+
if (elapsed > duration) return end;
|
|
19
|
+
return start + (end - start) * easeInOutQuint(elapsed / duration); // <-- you can change the easing funtion there
|
|
20
|
+
// return start + (end - start) * (elapsed / duration) // <-- this would give a linear scroll
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
var getTop = function getTop(element, start) {
|
|
24
|
+
// return value of html.getBoundingClientRect().top ... IE : 0, other browsers : -pageYOffset
|
|
25
|
+
if (element.nodeName === 'HTML') return -start;
|
|
26
|
+
return element.getBoundingClientRect().top + start;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
var easeInOutQuint = function easeInOutQuint(t) {
|
|
30
|
+
return t < 0.5 ? 16 * t * t * t * t * t : 1 - Math.pow(-2 * t + 2, 5) / 2;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
duration = duration || defaultDuration;
|
|
34
|
+
context = context || window;
|
|
35
|
+
var start = context.scrollTop || window.pageYOffset;
|
|
36
|
+
var end;
|
|
37
|
+
|
|
38
|
+
if (typeof el === 'number') {
|
|
39
|
+
end = parseInt(el);
|
|
40
|
+
} else {
|
|
41
|
+
end = getTop(el, start);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var clock = Date.now();
|
|
45
|
+
|
|
46
|
+
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || function (fn) {
|
|
47
|
+
window.setTimeout(fn, 15);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
var step = function step() {
|
|
51
|
+
var elapsed = Date.now() - clock;
|
|
52
|
+
|
|
53
|
+
if (context !== window) {
|
|
54
|
+
context.scrollTop = position(start, end, elapsed, duration);
|
|
55
|
+
} else {
|
|
56
|
+
window.scroll(0, position(start, end, elapsed, duration));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (elapsed > duration) {
|
|
60
|
+
if (typeof callback === 'function') {
|
|
61
|
+
callback(el);
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
requestAnimationFrame(step);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
step();
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twreporter/core",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2-rc.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"repository": "https://github.com/twreporter/twreporter-npm-packages.git",
|
|
6
6
|
"author": "twreporter <developer@twreporter.org>",
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"lib"
|
|
21
21
|
],
|
|
22
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "adbbd91639cb9a74438108b97083294acf57d8cf"
|
|
23
23
|
}
|