From f1b5bd8856604925b3a481740754157e275e030f Mon Sep 17 00:00:00 2001 From: caisui Date: Sat, 10 Oct 2015 23:19:01 +0900 Subject: [PATCH] fix: support requestAnimationFrame --- plugin/smooth-scroll.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/plugin/smooth-scroll.js b/plugin/smooth-scroll.js index e549cd6..aeb9b6e 100644 --- a/plugin/smooth-scroll.js +++ b/plugin/smooth-scroll.js @@ -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"; @@ -21,7 +29,7 @@ return; } - this.startTime = Date.now(); + this.startTime = now(); this.endTime = this.startTime + this.duration; this._start = this._pos; @@ -97,7 +105,7 @@ 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"); @@ -105,7 +113,7 @@ }, update: function () { if (!this.handle) { - this.handle = window.mozRequestAnimationFrame(this); + this.handle = window[requestAnimationFrame](this.sample.bind(this)); } }, reset: function reset() { @@ -113,7 +121,7 @@ this.x.clear(); this.y.clear(); if (this.handle) { - window.mozCancelRequestAnimationFrame(this.handle); + window[cancelRequestAnimationFrame](this.handle); this.handle = 0; } },