- Import the necessary Vue.js components and styles.
<template>
<div>
<header class="fixed-header">
<!-- Your header content goes here -->
</header>
<!-- Rest of your page content goes here -->
</div>
</template>
<script>
export default {
// Your component logic goes here
}
</script>
<style scoped>
.fixed-header {
position: fixed;
top: 0;
width: 100%;
/ Add other styling as needed /
}
</style>
- Add a data property to track the scroll position.
<script>
export default {
data() {
return {
isScrolled: false
};
},
// Your component logic goes here
}
</script>
- Use the
window
scroll event to update the isScrolled
property based on the scroll position.
<script>
export default {
data() {
return {
isScrolled: false
};
},
mounted() {
window.addEventListener('scroll', this.handleScroll);
},
beforeDestroy() {
window.removeEventListener('scroll', this.handleScroll);
},
methods: {
handleScroll() {
this.isScrolled = window.scrollY > 0;
}
}
}
</script>
- Apply a dynamic class to the header based on the
isScrolled
property.
<template>
<div>
<header :class="{ 'fixed-header': isScrolled }">
<!-- Your header content goes here -->
</header>
<!-- Rest of your page content goes here -->
</div>
</template>
- Customize the styling of the fixed header using the scoped style.
<style scoped>
.fixed-header {
position: fixed;
top: 0;
width: 100%;
/ Add other styling as needed for the fixed header /
}
</style>