mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-22 13:32:54 +08:00
chore(scrollTo): add scroll completed callback as option
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { CSS, pointerCoord, nativeRaf, cancelRaf } from '../util/dom';
|
||||
import { CSS, pointerCoord, nativeRaf, rafFrames, cancelRaf } from '../util/dom';
|
||||
|
||||
|
||||
export class ScrollView {
|
||||
@ -35,25 +35,35 @@ export class ScrollView {
|
||||
}
|
||||
}
|
||||
|
||||
scrollTo(x: number, y: number, duration: number): Promise<any> {
|
||||
scrollTo(x: number, y: number, duration: number, done?: Function): Promise<any> {
|
||||
// scroll animation loop w/ easing
|
||||
// credit https://gist.github.com/dezinezync/5487119
|
||||
let self = this;
|
||||
|
||||
let promise: Promise<any>;
|
||||
if (done === undefined) {
|
||||
// only create a promise if a done callback wasn't provided
|
||||
// done can be a null, which avoids any functions
|
||||
promise = new Promise((res, rej) => {
|
||||
done = res;
|
||||
done = rej;
|
||||
});
|
||||
}
|
||||
|
||||
const self = this;
|
||||
if (!self._el) {
|
||||
// invalid element
|
||||
return Promise.resolve();
|
||||
done();
|
||||
return promise;
|
||||
}
|
||||
|
||||
x = x || 0;
|
||||
y = y || 0;
|
||||
|
||||
let fromY = self._el.scrollTop;
|
||||
let fromX = self._el.scrollLeft;
|
||||
const fromY = self._el.scrollTop;
|
||||
const fromX = self._el.scrollLeft;
|
||||
|
||||
let maxAttempts = (duration / 16) + 100;
|
||||
const maxAttempts = (duration / 16) + 100;
|
||||
|
||||
return new Promise(resolve => {
|
||||
let startTime: number;
|
||||
let attempts = 0;
|
||||
|
||||
@ -63,7 +73,8 @@ export class ScrollView {
|
||||
|
||||
if (!self._el || !self.isPlaying || attempts > maxAttempts) {
|
||||
self.isPlaying = false;
|
||||
resolve();
|
||||
self._el.style.transform = ``;
|
||||
done();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -85,8 +96,8 @@ export class ScrollView {
|
||||
nativeRaf(step);
|
||||
|
||||
} else {
|
||||
// done
|
||||
resolve();
|
||||
self._el.style.transform = ``;
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,12 +105,12 @@ export class ScrollView {
|
||||
self.isPlaying = true;
|
||||
|
||||
// chill out for a frame first
|
||||
nativeRaf(() => {
|
||||
rafFrames(2, () => {
|
||||
startTime = Date.now();
|
||||
nativeRaf(step);
|
||||
step();
|
||||
});
|
||||
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
scrollToTop(duration: number): Promise<any> {
|
||||
|
Reference in New Issue
Block a user