Skip to content

Commit 56366f7

Browse files
authored
Fix issue #17 (#18)
* fix: delete content in the dist folder * fix: delete content in the dist folder * fix: not ignore the dist folder * feat: generate build
1 parent e523254 commit 56366f7

File tree

6 files changed

+4100
-2
lines changed

6 files changed

+4100
-2
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pnpm-debug.log*
88
lerna-debug.log*
99

1010
node_modules
11-
dist
1211
dist-ssr
1312
*.local
1413

dist/vanillajs-scrollspy.cjs.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
class VanillaScrollspy {
4+
constructor({ menu, speed = 2000, easing = 'easeOutSine' }) {
5+
this.$menu = menu;
6+
this.speed = speed;
7+
this.easing = easing;
8+
}
9+
10+
scrollToY(targetY = 0) {
11+
let currentTime = 0;
12+
const scrollTargetY = targetY;
13+
const scrollY = window.scrollY || document.documentElement.scrollTop;
14+
const time = Math.max(
15+
0.1,
16+
Math.min(Math.abs(scrollY - scrollTargetY) / this.speed, 0.8)
17+
);
18+
19+
const easingEquations = {
20+
easeOutSine(pos) {
21+
return Math.sin(pos * (Math.PI / 2))
22+
},
23+
24+
easeInOutSine(pos) {
25+
return -0.5 * (Math.cos(Math.PI * pos) - 1)
26+
},
27+
28+
easeInOutQuint(pos) {
29+
if ((pos /= 0.5) < 1) return 0.5 * pos ** 5
30+
return 0.5 * ((pos - 2) ** 5 + 2)
31+
}
32+
};
33+
34+
const tick = () => {
35+
currentTime += 1 / 60;
36+
37+
const p = currentTime / time;
38+
const t = easingEquations[this.easing](p);
39+
40+
if (p < 1) {
41+
window.requestAnimationFrame(tick);
42+
window.scrollTo(0, scrollY + (scrollTargetY - scrollY) * t);
43+
return
44+
}
45+
46+
window.scrollTo(0, scrollTargetY);
47+
};
48+
49+
tick();
50+
}
51+
52+
menuControl() {
53+
const $links = this.$menu.querySelectorAll('a[href^="#"]');
54+
const scrollPos = window.scrollY || document.documentElement.scrollTop;
55+
56+
$links.forEach((link) => {
57+
const $elem = document.querySelector(link.getAttribute('href'));
58+
59+
return $elem.offsetTop <= scrollPos &&
60+
$elem.offsetTop + $elem.clientHeight > scrollPos
61+
? link.classList.add('active')
62+
: link.classList.remove('active')
63+
});
64+
}
65+
66+
animated() {
67+
const $links = this.$menu.querySelectorAll('a[href^="#"]');
68+
const self = this;
69+
70+
function control(e) {
71+
e.preventDefault();
72+
const $target = document.querySelector(this.hash);
73+
self.scrollToY($target.offsetTop);
74+
}
75+
76+
$links.forEach((link) => link.addEventListener('click', control));
77+
}
78+
79+
init() {
80+
this.animated();
81+
document.addEventListener('scroll', () => this.menuControl());
82+
}
83+
}
84+
85+
function vanillaScrollspy(...args) {
86+
return new VanillaScrollspy(...args)
87+
}
88+
89+
module.exports = vanillaScrollspy;

dist/vanillajs-scrollspy.es.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class VanillaScrollspy {
2+
constructor({ menu, speed = 2000, easing = 'easeOutSine' }) {
3+
this.$menu = menu;
4+
this.speed = speed;
5+
this.easing = easing;
6+
}
7+
8+
scrollToY(targetY = 0) {
9+
let currentTime = 0;
10+
const scrollTargetY = targetY;
11+
const scrollY = window.scrollY || document.documentElement.scrollTop;
12+
const time = Math.max(
13+
0.1,
14+
Math.min(Math.abs(scrollY - scrollTargetY) / this.speed, 0.8)
15+
);
16+
17+
const easingEquations = {
18+
easeOutSine(pos) {
19+
return Math.sin(pos * (Math.PI / 2))
20+
},
21+
22+
easeInOutSine(pos) {
23+
return -0.5 * (Math.cos(Math.PI * pos) - 1)
24+
},
25+
26+
easeInOutQuint(pos) {
27+
if ((pos /= 0.5) < 1) return 0.5 * pos ** 5
28+
return 0.5 * ((pos - 2) ** 5 + 2)
29+
}
30+
};
31+
32+
const tick = () => {
33+
currentTime += 1 / 60;
34+
35+
const p = currentTime / time;
36+
const t = easingEquations[this.easing](p);
37+
38+
if (p < 1) {
39+
window.requestAnimationFrame(tick);
40+
window.scrollTo(0, scrollY + (scrollTargetY - scrollY) * t);
41+
return
42+
}
43+
44+
window.scrollTo(0, scrollTargetY);
45+
};
46+
47+
tick();
48+
}
49+
50+
menuControl() {
51+
const $links = this.$menu.querySelectorAll('a[href^="#"]');
52+
const scrollPos = window.scrollY || document.documentElement.scrollTop;
53+
54+
$links.forEach((link) => {
55+
const $elem = document.querySelector(link.getAttribute('href'));
56+
57+
return $elem.offsetTop <= scrollPos &&
58+
$elem.offsetTop + $elem.clientHeight > scrollPos
59+
? link.classList.add('active')
60+
: link.classList.remove('active')
61+
});
62+
}
63+
64+
animated() {
65+
const $links = this.$menu.querySelectorAll('a[href^="#"]');
66+
const self = this;
67+
68+
function control(e) {
69+
e.preventDefault();
70+
const $target = document.querySelector(this.hash);
71+
self.scrollToY($target.offsetTop);
72+
}
73+
74+
$links.forEach((link) => link.addEventListener('click', control));
75+
}
76+
77+
init() {
78+
this.animated();
79+
document.addEventListener('scroll', () => this.menuControl());
80+
}
81+
}
82+
83+
function vanillaScrollspy(...args) {
84+
return new VanillaScrollspy(...args)
85+
}
86+
87+
export { vanillaScrollspy as default };

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vanillajs-scrollspy",
3-
"version": "3.0.7",
3+
"version": "3.0.8",
44
"description": "ScrollSpy in pure JavaScript",
55
"main": "dist/vanillajs-scrollspy.cjs.js",
66
"module": "dist/vanillajs-scrollspy.es.js",
@@ -45,6 +45,7 @@
4545
"jest-environment-jsdom": "^29.0.3",
4646
"prettier": "^2.7.1",
4747
"rollup": "^2.79.1",
48+
"rollup-plugin-delete": "^2.0.0",
4849
"rollup-plugin-terser": "^7.0.2"
4950
}
5051
}

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { terser } from 'rollup-plugin-terser'
2+
import del from 'rollup-plugin-delete'
23

34
export default {
5+
plugins: [del({ targets: 'dist/*' })],
46
input: 'src/VanillaScrollspy/index.js',
57
output: [
68
{

0 commit comments

Comments
 (0)