From 72254ef3dbe7f711a98e916df938e9c012d49a64 Mon Sep 17 00:00:00 2001 From: rulanfenghua Date: Fri, 12 Oct 2018 14:56:27 +0800 Subject: [PATCH 1/2] spelling mistake --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 2acf5f9..baa3076 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ const getBindingConfig = binding => { return { stickyTop, zIndex, disabled } } -const getInitialiConfig = el => { +const getInitialConfig = el => { return { zIndex: el.style.zIndex, } @@ -30,7 +30,7 @@ let initialConfig = {} export default { bind(el, binding) { bindingConfig = getBindingConfig(binding) - initialConfig = getInitialiConfig(el) + initialConfig = getInitialConfig(el) const { disabled, stickyTop, zIndex } = bindingConfig if (disabled) return From 6823e367af2719d29a9d917ac1059f2599871fd0 Mon Sep 17 00:00:00 2001 From: rulanfenghua Date: Sat, 13 Oct 2018 09:59:01 +0800 Subject: [PATCH 2/2] sticky position base to parent element --- src/index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index baa3076..13fcfe9 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ const getBindingConfig = binding => { return { stickyTop, zIndex, disabled } } -const getInitialConfig = el => { +const getInitialiConfig = el => { return { zIndex: el.style.zIndex, } @@ -30,7 +30,7 @@ let initialConfig = {} export default { bind(el, binding) { bindingConfig = getBindingConfig(binding) - initialConfig = getInitialConfig(el) + initialConfig = getInitialiConfig(el) const { disabled, stickyTop, zIndex } = bindingConfig if (disabled) return @@ -49,18 +49,21 @@ export default { elStyle.zIndex = zIndex } else { elStyle.position = '' - childStyle.cssText = `left: 0; right: 0; top: ${stickyTop}px; z-index: ${zIndex}; ${childStyle.cssText}` + childStyle.top = `${stickyTop}px` + childStyle.zIndex = zIndex } let active = false - const sticky = () => { + const sticky = (left, right) => { if (supportCSSSticky || active) return if (!elStyle.height) { elStyle.height = `${el.offsetHeight}px` } if (childStyle) { childStyle.position = 'fixed' + childStyle.left = `${left}px` + childStyle.right = `${right}px` } active = true } @@ -73,8 +76,12 @@ export default { listenAction = throttle(() => { const offsetTop = el.getBoundingClientRect().top + const offsetLeft = el.getBoundingClientRect().left + const windowWidth = document.documentElement.clientWidth + const offsetRight = el.getBoundingClientRect().right + console.log(el.getBoundingClientRect()) if (offsetTop <= stickyTop) { - return sticky() + return sticky(offsetLeft, windowWidth-offsetRight) } reset() })