From 9685d06f4529acb99afb1cd1f13ea8bcaddc257c Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Thu, 29 Sep 2016 18:00:23 +0800 Subject: [PATCH] fix: should test if window exist, close: #3216 --- components/_util/getScroll.tsx | 17 +++++++++++++++++ components/affix/index.tsx | 15 +-------------- components/back-top/index.tsx | 19 +------------------ 3 files changed, 19 insertions(+), 32 deletions(-) create mode 100644 components/_util/getScroll.tsx diff --git a/components/_util/getScroll.tsx b/components/_util/getScroll.tsx new file mode 100644 index 0000000000..1b085e3e3d --- /dev/null +++ b/components/_util/getScroll.tsx @@ -0,0 +1,17 @@ +export default function getScroll(target, top) { + if (typeof window === 'undefined') { + return 0; + } + + const prop = top ? 'pageYOffset' : 'pageXOffset'; + const method = top ? 'scrollTop' : 'scrollLeft'; + const isWindow = target === window; + + let ret = isWindow ? target[prop] : target[method]; + // ie6,7,8 standard mode + if (isWindow && typeof ret !== 'number') { + ret = window.document.documentElement[method]; + } + + return ret; +} diff --git a/components/affix/index.tsx b/components/affix/index.tsx index c8b7a08744..d5f2eae115 100644 --- a/components/affix/index.tsx +++ b/components/affix/index.tsx @@ -4,20 +4,7 @@ import addEventListener from 'rc-util/lib/Dom/addEventListener'; import classNames from 'classnames'; import shallowequal from 'shallowequal'; import omit from 'omit.js'; - -function getScroll(target, top) { - const prop = top ? 'pageYOffset' : 'pageXOffset'; - const method = top ? 'scrollTop' : 'scrollLeft'; - const isWindow = target === window; - - let ret = isWindow ? target[prop] : target[method]; - // ie6,7,8 standard mode - if (isWindow && typeof ret !== 'number') { - ret = window.document.documentElement[method]; - } - - return ret; -} +import getScroll from '../_util/getScroll'; function getTargetRect(target): any { return target !== window ? diff --git a/components/back-top/index.tsx b/components/back-top/index.tsx index f81dea4f0b..a10e5187fc 100644 --- a/components/back-top/index.tsx +++ b/components/back-top/index.tsx @@ -4,6 +4,7 @@ import Icon from '../icon'; import addEventListener from 'rc-util/lib/Dom/addEventListener'; import classNames from 'classnames'; import omit from 'omit.js'; +import getScroll from '../_util/getScroll'; const reqAnimFrame = (() => { if (window.requestAnimationFrame) { @@ -29,24 +30,6 @@ const easeInOutCubic = (t, b, c, d) => { } }; -function getScroll(target, top) { - if (typeof window === 'undefined') { - return 0; - } - - const prop = top ? 'pageYOffset' : 'pageXOffset'; - const method = top ? 'scrollTop' : 'scrollLeft'; - const isWindow = target === window; - - let ret = isWindow ? target[prop] : target[method]; - // ie6,7,8 standard mode - if (isWindow && typeof ret !== 'number') { - ret = window.document.documentElement[method]; - } - - return ret; -} - export interface BackTopProps { visibilityHeight?: number; onClick?: (event) => void;