diff options
author | afc163 <afc163@gmail.com> | 2015-11-29 15:50:19 +0800 |
---|---|---|
committer | afc163 <afc163@gmail.com> | 2015-11-29 15:51:04 +0800 |
commit | 0b9ef41f8507b6d5f11b4c42d57397e9a1850402 (patch) | |
tree | 37f3176b98ce650e3fd06342b842ee091d829f75 /src | |
parent | 85d09ad3310c26d61b6af60b28d680eed1f2bf1c (diff) | |
download | time-picker-0b9ef41f8507b6d5f11b4c42d57397e9a1850402.tar.gz time-picker-0b9ef41f8507b6d5f11b4c42d57397e9a1850402.tar.zst time-picker-0b9ef41f8507b6d5f11b4c42d57397e9a1850402.zip |
replace setTimeout with requestAnimationFrame
Diffstat (limited to 'src')
-rw-r--r-- | src/module/Select.jsx | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/module/Select.jsx b/src/module/Select.jsx index b386d07..0b91ac5 100644 --- a/src/module/Select.jsx +++ b/src/module/Select.jsx | |||
@@ -3,6 +3,10 @@ import ReactDom from 'react-dom'; | |||
3 | import classnames from 'classnames'; | 3 | import classnames from 'classnames'; |
4 | 4 | ||
5 | const scrollTo = (element, to, duration) => { | 5 | const scrollTo = (element, to, duration) => { |
6 | const requestAnimationFrame = window.requestAnimationFrame || | ||
7 | function requestAnimationFrameTimeout() { | ||
8 | return setTimeout(arguments[0], 10); | ||
9 | }; | ||
6 | // jump to target if duration zero | 10 | // jump to target if duration zero |
7 | if (duration <= 0) { | 11 | if (duration <= 0) { |
8 | element.scrollTop = to; | 12 | element.scrollTop = to; |
@@ -11,11 +15,11 @@ const scrollTo = (element, to, duration) => { | |||
11 | const difference = to - element.scrollTop; | 15 | const difference = to - element.scrollTop; |
12 | const perTick = difference / duration * 10; | 16 | const perTick = difference / duration * 10; |
13 | 17 | ||
14 | setTimeout(() => { | 18 | requestAnimationFrame(() => { |
15 | element.scrollTop = element.scrollTop + perTick; | 19 | element.scrollTop = element.scrollTop + perTick; |
16 | if (element.scrollTop === to) return; | 20 | if (element.scrollTop === to) return; |
17 | scrollTo(element, to, duration - 10); | 21 | scrollTo(element, to, duration - 10); |
18 | }, 10); | 22 | }); |
19 | }; | 23 | }; |
20 | 24 | ||
21 | const Select = React.createClass({ | 25 | const Select = React.createClass({ |