forked from ant-design/ant-design
-
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.
Merge pull request ant-design#17827 from shaodahong/feat-anchor-targe…
…tOffset feat(Anchor): add targetOffset prop
- Loading branch information
Showing
16 changed files
with
382 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { easeInOutCubic } from '../easings'; | ||
|
||
describe('Test easings', () => { | ||
it('easeInOutCubic return value', () => { | ||
const nums = []; | ||
// eslint-disable-next-line no-plusplus | ||
for (let index = 0; index < 5; index++) { | ||
nums.push(easeInOutCubic(index, 1, 5, 4)); | ||
} | ||
|
||
expect(nums).toEqual([1, 1.25, 3, 4.75, 5]); | ||
}); | ||
}); |
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,56 @@ | ||
import scrollTo from '../scrollTo'; | ||
|
||
describe('Test ScrollTo function', () => { | ||
let dateNowMock; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
beforeEach(() => { | ||
dateNowMock = jest | ||
.spyOn(Date, 'now') | ||
.mockImplementationOnce(() => 0) | ||
.mockImplementationOnce(() => 1000); | ||
}); | ||
|
||
afterEach(() => { | ||
dateNowMock.mockRestore(); | ||
}); | ||
|
||
it('test scrollTo', async () => { | ||
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { | ||
window.scrollY = y; | ||
window.pageYOffset = y; | ||
}); | ||
|
||
scrollTo(1000); | ||
|
||
jest.runAllTimers(); | ||
expect(window.pageYOffset).toBe(1000); | ||
|
||
scrollToSpy.mockRestore(); | ||
}); | ||
|
||
it('test callback - option', async () => { | ||
const cbMock = jest.fn(); | ||
scrollTo(1000, { | ||
callback: cbMock, | ||
}); | ||
jest.runAllTimers(); | ||
expect(cbMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('test getContainer - option', async () => { | ||
const div = document.createElement('div'); | ||
scrollTo(1000, { | ||
getContainer: () => div, | ||
}); | ||
jest.runAllTimers(); | ||
expect(div.scrollTop).toBe(1000); | ||
}); | ||
}); |
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,9 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export function easeInOutCubic(t: number, b: number, c: number, d: number) { | ||
const cc = c - b; | ||
t /= d / 2; | ||
if (t < 1) { | ||
return (cc / 2) * t * t * t + b; | ||
} | ||
return (cc / 2) * ((t -= 2) * t * t + 2) + b; | ||
} |
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,37 @@ | ||
import raf from 'raf'; | ||
import getScroll from './getScroll'; | ||
import { easeInOutCubic } from './easings'; | ||
|
||
interface ScrollToOptions { | ||
/** Scroll container, default as window */ | ||
getContainer?: () => HTMLElement | Window; | ||
/** Scroll end callback */ | ||
callback?: () => any; | ||
/** Animation duration, default as 450 */ | ||
duration?: number; | ||
} | ||
|
||
export default function scrollTo(y: number, options: ScrollToOptions = {}) { | ||
const { getContainer = () => window, callback, duration = 450 } = options; | ||
|
||
const container = getContainer(); | ||
const scrollTop = getScroll(container, true); | ||
const startTime = Date.now(); | ||
|
||
const frameFunc = () => { | ||
const timestamp = Date.now(); | ||
const time = timestamp - startTime; | ||
const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration); | ||
if (container === window) { | ||
window.scrollTo(window.pageXOffset, nextScrollTop); | ||
} else { | ||
(container as HTMLElement).scrollTop = nextScrollTop; | ||
} | ||
if (time < duration) { | ||
raf(frameFunc); | ||
} else if (typeof callback === 'function') { | ||
callback(); | ||
} | ||
}; | ||
raf(frameFunc); | ||
} |
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
Oops, something went wrong.