Skip to content

Commit

Permalink
fix module exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed Jun 13, 2021
1 parent e145983 commit 8cc4e10
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
4 changes: 4 additions & 0 deletions dist/stacky.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ class Stacky {
}
}
}

if (typeof window !== 'undefined') {
window.Stacky = Stacky;
}
2 changes: 1 addition & 1 deletion dist/stacky.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,4 @@ class Stacky {
}
}

export {
Stacky as default,
Stacky
};

if (typeof window !== 'undefined') {
window.Stacky = Stacky;
}
module.exports.Stacky = Stacky
72 changes: 72 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Stacky {
constructor(stackySelector=`header`) {
this.headers = document.querySelectorAll(stackySelector)
this.headerArray = []
}

init() {
let i = 0
let j = this.headers.length - 1

this.headers.forEach(header => {
header.setAttribute(`data-stacky-id`, `${i}`)
header.setAttribute(`data-stacky-top`, `${i}`)
header.setAttribute(`data-stacky-bottom`, `${j}`)
this.headerArray.push({
el: header,
id: i,
orderTop: i,
orderBottom: j,
parent: header.parentElement,
height: header.clientHeight,
position: '',
offsetTop: header.clientHeight * i,
offsetBottom: header.clientHeight * j
})
i++
j--
})

window.addEventListener(`scroll`, el => {
this.headerArray.forEach(element => {
this.updateStack(element)
})
})
}

updateStack(element) {
let position = ``
const headerEl = document.querySelector(`[data-stacky-id="${element.id}"]`)
const rect = headerEl.parentElement.getBoundingClientRect()
const top = rect.top

if (top - (headerEl.clientHeight * element.orderTop) <= 0) {
position = `top`
} else if (top + (headerEl.clientHeight * (element.orderBottom + 1)) >= document.documentElement.clientHeight) {
position = `bottom`
}

if (position) {
if (position === `top`) {
element.position = `top`
headerEl.style.position = `fixed`
headerEl.style.top = `${element.offsetTop}px`
headerEl.style.bottom = ``
headerEl.parentElement.style.paddingTop = `${element.height}px`
} else if (position === `bottom`) {
element.position = `bottom`
headerEl.style.position = `fixed`
headerEl.style.top = ``
headerEl.style.bottom = `${element.offsetBottom}px`
headerEl.parentElement.style.paddingTop = `${element.height}px`
}
} else {
headerEl.style.position = ``
headerEl.style.top = ``
headerEl.style.width = ``
headerEl.parentElement.style.paddingTop = ``
}
}
}

export { Stacky }
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stacky.js",
"version": "1.0.3",
"version": "1.0.4",
"description": "Stacking scrolling navigation, modern alternative to Slinky.js without no dependencies",
"main": "index.js",
"scripts": {
Expand All @@ -10,6 +10,7 @@
"type": "git",
"url": "git+https://github.com/matronator/stacky.js.git"
},
"module": "index.mjs",
"keywords": [
"sticky",
"slinky",
Expand Down

0 comments on commit 8cc4e10

Please sign in to comment.