JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Creating a sticky footer in Vue

Author:JIYIK Last Updated:2025/02/26 Views:

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 positionproperty. positionSet the footer's property to fixed, and its bottomproperty 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.innerHeightto 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.addEventListenerlisten to window size changes and use this.handleResizethe method to call when the component is created and when it is destroyed. handleResizeThe method compares the viewport height to the page height and uses this.footerFixedthe property to set the fixed state of the footer to trueor false.

Dynamically set footer style

Finally, we need to dynamically style the footer based on its pinned state. We can use Vue's :classbindings 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 fixedto apply to the footer if footerFixedthe property is true.

Summarize

Creating a sticky footer in Vue requires the use of the CSS sticky positionfooter property and Vue window.innerHeightsticky 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.

Article URL:

Related Articles

Various ways to format dates in Vue

Publish Date:2025/02/26 Views:68 Category:Vue

Vue is a popular front-end framework that provides many features, including multiple ways to format dates. In this article, we will introduce several methods that can help you format dates in Vue. Using moment.js moment.js is a popular

The difference between strict mode and non-strict mode in Vue

Publish Date:2025/02/26 Views:169 Category:Vue

Vue.js is a very popular JavaScript framework. Its design goal is to help developers build efficient and flexible web applications. Vue.js provides two different modes: strict mode and non-strict mode. These two modes are used in the develop

Click the button to switch the background color in vue

Publish Date:2025/02/26 Views:160 Category:Vue

Vue is a popular JavaScript framework for building interactive web applications. Vue provides many powerful features, one of which is the ability to easily toggle the background color when a button is clicked. In this article, we will show y

Introducing the bootstrap framework into the vue project

Publish Date:2025/02/26 Views:148 Category:Vue

Bootstrap is a popular front-end framework that provides a wealth of CSS and JavaScript components to help developers quickly build modern responsive websites and applications. Introducing the Bootstrap framework in a Vue project can provide

Using iframe to nest pages in Vue

Publish Date:2025/02/26 Views:177 Category:Vue

With the development of the Internet, websites are becoming more and more complex, and various technologies are needed to implement them. Among them, iframe technology is a commonly used technology that can embed a web page into another web

Different ways to send POST requests in Vue

Publish Date:2025/02/26 Views:82 Category:Vue

In Vue, sending POST requests is a very common operation, which can be achieved in many ways. This article will introduce several methods of sending POST requests in Vue, and will explain them in detail with examples. Method 1: Use Vue-resou

Importing JSON files into Vue

Publish Date:2025/02/26 Views:100 Category:Vue

Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in V

How to introduce style files in Vue

Publish Date:2025/02/26 Views:181 Category:Vue

Vue is a popular front-end framework that allows developers to build single-page applications in a componentized way. During the development process, we usually need to introduce style files to beautify the page. This article will introduce

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial