Creating a sticky footer in Vue
In web design, the footer is one of the most important components on the page. It usually contains copyright information, contact information, and other relevant information about the website. However, when users scroll the page, the footer may disappear above the screen, which may prevent users from easily accessing the footer information. Therefore, creating a sticky footer can ensure that users can always access the footer information without having to worry about it disappearing.
In this article, we'll look at how to create a sticky footer in Vue.
Creating a Basic Template
First, create a basic template in Vue that contains a header, body, and footer. Here is a simple example:
<template>
<div id="app">
<header>
<!-- Header Content -->
</header>
<main>
<!-- Main Content -->
</main>
<footer>
<!-- Footer Content -->
</footer>
</div>
</template>
Fix footer to bottom
To fix the footer at the bottom, we can use the CSS position
property. position
Set the footer's property to fixed
, and its bottom
property to 0
, which will make the footer always stay at the bottom of the page.
<style>
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 50px;
background-color: #f2f2f2;
}
</style>
At this point, the footer will be sticky fixed to the bottom of the page, but when the page is too long, the footer may block the main content.
Use Vue to monitor page height
To prevent the footer from blocking the main content, we need to use Vue to monitor the page height. We can use window.innerHeight
to get the current viewport height and compare it with the total height of the page. If the page height is less than the viewport height, the footer is fixed to the bottom; otherwise, the footer is kept at the bottom to ensure it is visible.
Here is a simple Vue listener example:
<script>
export default {
data() {
return {
footerFixed: false
}
},
created() {
window.addEventListener('resize', this.handleResize)
this.handleResize()
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
methods: {
handleResize() {
this.footerFixed = window.innerHeight > document.body.offsetHeight
}
}
}
</script>
In this example, we use window.addEventListener
listen to window size changes and use this.handleResize
the method to call when the component is created and when it is destroyed. handleResize
The method compares the viewport height to the page height and uses this.footerFixed
the property to set the fixed state of the footer to true
or false
.
Dynamically set footer style
Finally, we need to dynamically style the footer based on its pinned state. We can use Vue's :class
bindings to apply different styles to the footer.
Here is a simple example:
<template>
<div id="app">
<header>
<!-- Header Content -->
</header>
<main>
<!-- Main Content -->
</main>
<footer :class="{ 'fixed': footerFixed }">
<!-- Footer Content -->
</footer>
</div>
</template>
<style>
footer {
position: absolute;
width: 100%;
height: 50px;
background-color: #f2f2f2;
}
.fixed {
position: fixed;
bottom: 0;
}
</style>
In this example, we use the :class="{ 'fixed': footerFixed }"
class fixed
to apply to the footer if footerFixed
the property is true
.
Summarize
Creating a sticky footer in Vue requires the use of the CSS sticky position
footer property and Vue window.innerHeight
sticky footer listeners. By using these techniques, we can ensure that users can always access the footer information without having to worry about it disappearing.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:199 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during
How to scroll to the top or bottom of the page in Vue.js
Publish Date:2025/03/01 Views:96 Category:Vue
-
Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this
Display element or text on mouse hover in vue
Publish Date:2025/03/01 Views:179 Category:Vue
-
Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following
When watching an object in Vue, how to exclude certain properties from being watc
Publish Date:2025/03/01 Views:125 Category:Vue
-
When using watch in Vue, you may need to monitor an object, but only care about certain properties in the object, not all properties of the object. In this case, you can use deep monitoring and calculated properties, or add some options to w
What is the use of the immediate property of watch in Vue?
Publish Date:2025/03/01 Views:67 Category:Vue
-
In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang
Setting up checkbox functionality in Vue
Publish Date:2025/03/01 Views:186 Category:Vue
-
In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.
What happens if a child component changes the data in props in Vue?
Publish Date:2025/03/01 Views:140 Category:Vue
-
In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t
How to refresh the page in Vue
Publish Date:2025/03/01 Views:91 Category:Vue
-
Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the