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
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
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:201 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