Skip to content

Commit

Permalink
fix: support requestAnimationFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
caisui committed Oct 10, 2015
1 parent 8e6138d commit f1b5bd8
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions plugin/smooth-scroll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// vim: set fdm=marker:
(function () {
const prefix = "mozRequestAnimationFrame" in window;
const requestAnimationFrame =
prefix ? "mozRequestAnimationFrame" : "requestAnimationFrame";
const cancelRequestAnimationFrame =
prefix ? "mozCancelRequestAnimationFrame" : "CancelRequestAnimationFrame";

const now = prefix ? Date.now.bind(Date) : performance.now.bind(performance);

function SmoothScroller(elem, dir) {
this.elem = Cu.getWeakReference(elem);
this.dir = dir || "x";
Expand All @@ -21,7 +29,7 @@
return;
}

this.startTime = Date.now();
this.startTime = now();
this.endTime = this.startTime + this.duration;

this._start = this._pos;
Expand Down Expand Up @@ -97,23 +105,23 @@
array.splice(k);
}
if (k > 0) {
this.handle = window.mozRequestAnimationFrame(this);
this.handle = window[requestAnimationFrame](this.sample.bind(this));
} else {
this.handle = 0;
Cu.reportError("new");
}
},
update: function () {
if (!this.handle) {
this.handle = window.mozRequestAnimationFrame(this);
this.handle = window[requestAnimationFrame](this.sample.bind(this));
}
},
reset: function reset() {
this.array = [];
this.x.clear();
this.y.clear();
if (this.handle) {
window.mozCancelRequestAnimationFrame(this.handle);
window[cancelRequestAnimationFrame](this.handle);
this.handle = 0;
}
},
Expand Down

0 comments on commit f1b5bd8

Please sign in to comment.