-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19738b9
commit b12604d
Showing
4 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Ref } from 'vue' | ||
|
||
export default function useSyncScroll(scrollableClass: Ref<string>) { | ||
// Keep scroll containers in sync | ||
const handleScroll = (e: Event) => { | ||
const syncScroll = (scrolledEle: Element, ele: Element) => { | ||
const scrolledPercent = scrolledEle.scrollTop / (scrolledEle.scrollHeight - scrolledEle.clientHeight) | ||
const top = scrolledPercent * (ele.scrollHeight - ele.clientHeight) | ||
|
||
const scrolledWidthPercent = scrolledEle.scrollLeft / (scrolledEle.scrollWidth - scrolledEle.clientWidth) | ||
const left = scrolledWidthPercent * (ele.scrollWidth - ele.clientWidth) | ||
|
||
ele.scrollTo({ | ||
behavior: 'instant', // must be instant | ||
top, | ||
left, | ||
}) | ||
} | ||
|
||
const scrolledEle = e.target | ||
|
||
Array.from([...document.querySelectorAll(`.${scrollableClass.value}`)]).filter((item) => item !== scrolledEle).forEach((ele: Element) => { | ||
ele.removeEventListener('scroll', handleScroll) | ||
// @ts-ignore | ||
syncScroll(scrolledEle, ele) | ||
window.requestAnimationFrame(() => { | ||
ele.addEventListener('scroll', handleScroll) | ||
}) | ||
}) | ||
} | ||
|
||
const initializeSyncScroll = (): void => { | ||
document?.querySelectorAll(`.${scrollableClass.value}`)?.forEach((el) => { | ||
el?.addEventListener('scroll', handleScroll) | ||
}) | ||
} | ||
|
||
const destroySyncScroll = (): void => { | ||
document?.querySelectorAll(`.${scrollableClass.value}`)?.forEach((el) => { | ||
el?.removeEventListener('scroll', handleScroll) | ||
}) | ||
} | ||
|
||
return { | ||
initializeSyncScroll, | ||
destroySyncScroll, | ||
} | ||
} |