Last active
March 20, 2023 18:50
-
-
Save jackabox/ff0e92b9633f2b6a1165e5f423220a40 to your computer and use it in GitHub Desktop.
Vue/Nuxt window on scroll event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="progress-bar"> | |
<div class="filled-bar" :style="{ transform: `translate3d(-${100-value}%, 0, 0)` }"></div> | |
</div> | |
</template> | |
<script> | |
export default { | |
props: ['value'] | |
} | |
</script> | |
<style lang="postcss" scoped> | |
.progress-bar { | |
position: fixed; | |
top: 0; | |
height: 7px; | |
width: 100%; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
overflow: hidden; | |
z-index: 100; | |
} | |
.filled-bar { | |
position: absolute; | |
top: 0; | |
left: 0; | |
z-index: 2; | |
height: 100%; | |
width: 100%; | |
@apply .bg-red; | |
} | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<progress-bar :value="progress"/> | |
</template> | |
<script> | |
export default { | |
components: { | |
ProgressBar | |
}, | |
data() { | |
return { | |
progress: 0 | |
} | |
}, | |
mounted() { | |
window.addEventListener('scroll', e => { | |
requestAnimationFrame(() => { | |
var scrollPos = window.scrollY | |
var winHeight = window.innerHeight | |
var docHeight = document.documentElement.scrollHeight | |
var perc = (100 * scrollPos) / (docHeight - winHeight) | |
this.progress = perc | |
}) | |
}) | |
} | |
} | |
</script> |
this is great thanks mate <3
Doesn't work for me, it just won't execute it.
<script>
export default {
mounted() {
window.addEventListener('scroll', e => {
var reveals = document.querySelectorAll(".reveal-content");
for (var i = 0; i < reveals.length; i++) {
var windowheight = window.innerHeight;
var revealtop = reveals[i].getBoundingClientRect().top;
var revealpoint = 300;
if (revealtop < windowheight - revealpoint) {
reveals[iev].classList.add("active");
}
}
})
}
}
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, thx !