mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 05:58:26 +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 {
|
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
|
// scroll animation loop w/ easing
|
||||||
// credit https://gist.github.com/dezinezync/5487119
|
// 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) {
|
if (!self._el) {
|
||||||
// invalid element
|
// invalid element
|
||||||
return Promise.resolve();
|
done();
|
||||||
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = x || 0;
|
x = x || 0;
|
||||||
y = y || 0;
|
y = y || 0;
|
||||||
|
|
||||||
let fromY = self._el.scrollTop;
|
const fromY = self._el.scrollTop;
|
||||||
let fromX = self._el.scrollLeft;
|
const fromX = self._el.scrollLeft;
|
||||||
|
|
||||||
let maxAttempts = (duration / 16) + 100;
|
const maxAttempts = (duration / 16) + 100;
|
||||||
|
|
||||||
return new Promise(resolve => {
|
|
||||||
let startTime: number;
|
let startTime: number;
|
||||||
let attempts = 0;
|
let attempts = 0;
|
||||||
|
|
||||||
@ -63,7 +73,8 @@ export class ScrollView {
|
|||||||
|
|
||||||
if (!self._el || !self.isPlaying || attempts > maxAttempts) {
|
if (!self._el || !self.isPlaying || attempts > maxAttempts) {
|
||||||
self.isPlaying = false;
|
self.isPlaying = false;
|
||||||
resolve();
|
self._el.style.transform = ``;
|
||||||
|
done();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +96,8 @@ export class ScrollView {
|
|||||||
nativeRaf(step);
|
nativeRaf(step);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// done
|
self._el.style.transform = ``;
|
||||||
resolve();
|
done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,12 +105,12 @@ export class ScrollView {
|
|||||||
self.isPlaying = true;
|
self.isPlaying = true;
|
||||||
|
|
||||||
// chill out for a frame first
|
// chill out for a frame first
|
||||||
nativeRaf(() => {
|
rafFrames(2, () => {
|
||||||
startTime = Date.now();
|
startTime = Date.now();
|
||||||
nativeRaf(step);
|
step();
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollToTop(duration: number): Promise<any> {
|
scrollToTop(duration: number): Promise<any> {
|
||||||
|
Reference in New Issue
Block a user