MCPcopy Create free account
hub / github.com/Fiddle-Digital/string-scroll / StringScrollbar

Class StringScrollbar

src/Scrollbar/StringScrollbar.ts:1–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1export class StringScrollbar {
2 private scrollbar: any;
3 private thumb: any;
4 private scrollTimeout: any;
5
6 private isScrollbarWillShow = true
7
8 constructor() {
9 this.createScrollbar();
10 this.updateThumb();
11 this.addCustomStyles()
12 }
13
14 private addCustomStyles() {
15 const style = document.createElement('style');
16 style.textContent = `
17 ::-webkit-scrollbar {
18 width: 0px;
19 }
20 body {
21 -ms-overflow-style: none; /* IE and Edge */
22 scrollbar-width: none; /* Firefox */
23 }
24 `;
25 document.head.appendChild(style);
26 }
27
28 private createScrollbar() {
29 this.scrollbar = document.createElement('div');
30 this.scrollbar.classList.add('scrollbar')
31 this.thumb = document.createElement('div');
32 this.thumb.classList.add('thumb')
33
34 this.scrollbar.appendChild(this.thumb);
35 document.body.appendChild(this.scrollbar);
36
37 this.addDragFunctionality();
38 this.addScrollListener();
39 }
40
41 public resize(){
42 const contentHeight = document.documentElement.scrollHeight;
43 const visibleHeight = window.innerHeight;
44 const thumbHeight = visibleHeight / contentHeight * visibleHeight;
45 this.thumb.style.setProperty('--height', thumbHeight + 'px');
46
47 if (contentHeight <= visibleHeight) {
48 this.scrollbar.classList.add('-hide')
49 } else {
50 this.scrollbar.classList.remove('-hide')
51 }
52 }
53
54 private updateThumb() {
55 const contentHeight = document.documentElement.scrollHeight;
56 const visibleHeight = window.innerHeight;
57 this.thumb.style.setProperty('--position', `${(document.documentElement.scrollTop / contentHeight * visibleHeight) + 'px'}`)
58 //this.thumb.style.transform = `translateY(${(document.documentElement.scrollTop / contentHeight * visibleHeight) + 'px'})`;
59 }
60

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected