-
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
Showing
3 changed files
with
40 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as numberParse } from './numberParse' | ||
export { numberParse, numberMatch } from './numberParse' | ||
export { default as scrollToView } from './scrollToView' |
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,36 @@ | ||
|
||
export type Options = ScrollToOptions & { | ||
/** 横向滚动位置偏移量,正数偏右,负数偏左 */ | ||
offsetX?: number | ||
/** 纵向滚动位置偏移量,正数偏上,负数偏下 */ | ||
offsetY?: number | ||
/** css选择器,用于指定滚动的dom */ | ||
selector?: string | ||
} | ||
|
||
/** 滚动至可视区域 */ | ||
const scrollToView = (options: Options) => { | ||
const { top, left, offsetY = 0, offsetX = 0, selector, behavior } = options ?? {} | ||
let elmSelector = selector | ||
if (!elmSelector) { | ||
// 如果未指定选择器,就从路由里面读取锚点 | ||
const matchs = window.location.hash.match(/(#\w+)/g) | ||
if (matchs) { | ||
elmSelector = matchs[matchs.length - 1] | ||
} | ||
} | ||
if (elmSelector) { | ||
const dom = document.querySelector(elmSelector) | ||
if (dom) { | ||
window.scrollTo({ | ||
behavior, | ||
top: top ?? dom.getBoundingClientRect().top + offsetY, | ||
left: left ?? dom.getBoundingClientRect().left + offsetX, | ||
}) | ||
} | ||
} else { | ||
throw new Error('Did you forget params selector?') | ||
} | ||
} | ||
|
||
export default scrollToView |