Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue #21 add sticky bottom option #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions example/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default {
stickyConfig: {
zIndex: 80,
stickyTop: 10,
stickyBottom: null,
disabled: false
},
stickyConfigBackup: {},
Expand Down Expand Up @@ -36,9 +37,16 @@ export default {
this.stickyConfig.disabled = true
},

update() {
updateTop() {
this.stickyConfig.disabled = false
this.stickyConfig.stickyTop = Math.ceil((Math.random() * 300) % 100)
this.stickyConfig.stickyBottom = null
},

updateBottom() {
this.stickyConfig.disabled = false
this.stickyConfig.stickyBottom = Math.ceil((Math.random() * 300) % 100)
this.stickyConfig.stickyTop = null
}
}
}
Expand All @@ -51,22 +59,34 @@ export default {
<div v-sticky="{ zIndex: 100, stickyTop: 20, className: 'sticky-buttons' }">
<div>
<button @click="disable">disable sticky</button>
<button @click="update">update sticky top value</button>
<button @click="updateTop">update sticky top value</button>
<button @click="updateBottom">update sticky bottom value</button>
</div>
</div>

<hr>

<div v-sticky="stickyConfig">
<div v-show="stickyConfig.stickyTop >= 0" v-sticky="stickyConfig">
<nav>
<div>
{{ msg }}
{{ msg }} top
<pre>{{ stickyConfig }}</pre>
</div>
</nav>
</div>

<p v-for="item in 100" :key="item">{{ item }}</p>

<div v-show="stickyConfig.stickyBottom >= 0" v-sticky="stickyConfig">
<nav>
<div>
{{ msg }} bottom
<pre>{{ stickyConfig }}</pre>
</div>
</nav>
</div>

<p v-for="item in 'abcde'" :key="item">{{ item }}</p>
</div>
</template>

Expand Down
49 changes: 39 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ let supportCSSSticky
const getBindingConfig = binding => {
const params = binding.value || {}
let stickyTop = params.stickyTop || 0
let stickyBottom = params.stickyBottom || 0
let zIndex = params.zIndex || 1000
let disabled = params.disabled
return { stickyTop, zIndex, disabled }
return { stickyTop, stickyBottom, zIndex, disabled }
}

const getInitialiConfig = el => {
Expand All @@ -31,7 +32,7 @@ export default {
bind(el, binding) {
bindingConfig = getBindingConfig(binding)
initialConfig = getInitialiConfig(el)
const { disabled, stickyTop, zIndex } = bindingConfig
const { disabled, stickyTop, stickyBottom, zIndex } = bindingConfig

if (disabled) return

Expand All @@ -45,11 +46,20 @@ export default {
supportCSSSticky = ~elStyle.position.indexOf('sticky')

if (supportCSSSticky) {
elStyle.top = `${stickyTop}px`
if (stickyTop >= 0) {
elStyle.top = `${stickyTop}px`
} else {
elStyle.bottom = `${stickyBottom}px`
}

elStyle.zIndex = zIndex
} else {
elStyle.position = ''
childStyle.cssText = `left: 0; right: 0; top: ${stickyTop}px; z-index: ${zIndex}; ${childStyle.cssText}`
if (stickyTop >= 0) {
childStyle.cssText = `left: 0; right: 0; top: ${stickyTop}px; bottom: auto; z-index: ${zIndex}; ${childStyle.cssText}`
} else {
childStyle.cssText = `left: 0; right: 0; top: auto; bottom: ${stickyBottom}px; z-index: ${zIndex}; ${childStyle.cssText}`
}
}

let active = false
Expand All @@ -72,10 +82,18 @@ export default {
}

listenAction = throttle(() => {
const offsetTop = el.getBoundingClientRect().top
if (offsetTop <= stickyTop) {
return sticky()
if (stickyTop >= 0) {
const offsetTop = el.getBoundingClientRect().top
if (offsetTop <= stickyTop) {
return sticky()
}
} else {
const offsetBottom = el.getBoundingClientRect().bottom
if (offsetBottom >= window.innerHeight - stickyBottom) {
return sticky()
}
}

reset()
})

Expand All @@ -86,14 +104,24 @@ export default {

update(el, binding) {
bindingConfig = getBindingConfig(binding)
const { stickyTop, zIndex } = bindingConfig
const { stickyTop, stickyBottom, zIndex } = bindingConfig

let childStyle = el.firstElementChild.style
if (supportCSSSticky) {
el.style.top = `${stickyTop}px`
if (stickyTop >= 0) {
el.style.top = `${stickyTop}px`
} else {
el.style.bottom = `${stickyBottom}px`
}
el.style.zIndex = zIndex
} else {
childStyle.top = `${stickyTop}px`
if (stickyTop >= 0) {
childStyle.top = `${stickyTop}px`
childStyle.bottom = ''
} else {
childStyle.bottom = `${stickyBottom}px`
childStyle.top = ''
}
childStyle.zIndex = zIndex
}

Expand All @@ -103,6 +131,7 @@ export default {
} else {
childStyle.position = ''
childStyle.top = ''
childStyle.bottom = ''
childStyle.zIndex = initialConfig.zIndex
unwatch()
}
Expand Down