JIYIK CN >

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

How to scroll to the top or bottom of the page in Vue.js

Author:JIYIK Last Updated:2025/03/01 Views:

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 feature in Vue.js.


Scroll to the bottom of the page

To scroll the page to the bottom, we can use scrollTo()the scroll method of the window object and set the y coordinate to the height of the document.

In Vue.js, we can use vue-scrolltothe plugin to implement the function of scrolling to the bottom of the page. Similar to scrolling to the top of the page, we can use v-scroll-tothe directive to scroll the element to the bottom.

To use the vue-scrollto plugin, we need to install it first. You can install the plugin via npm:

$ npm install vue-scrollto --save

Once installed, we can use the plugin in our Vue.js application. Here is an example:

<template>
  <div>
    <button v-scroll-to="'#bottom'">Scroll to Bottom</button>
    ...
    <div id="bottom">Bottom of Page</div>
  </div>
</template>

<script>
import VueScrollTo from 'vue-scrollto'

export default {
  ...
  directives: {
    scrollTo: VueScrollTo.directive
  }
  ...
}
</script>

In the code above, we added v-scroll-toa directive for the button and set it to scroll to the bottom of the page. We also added an element with id "bottom" to the bottom of the page in order to specify the scroll position.


Scroll to top of page

To scroll the page to the top, we can use scrollTo()the method of the window object. This method accepts two parameters: the x and y coordinates. To scroll the page to the top, we simply set the y coordinate to 0.

In Vue.js, we can use the vue-scrollto plugin to scroll to the top of the page. This plugin provides a v-scroll-todirective that can scroll an element to a specified position.

Here is an example:

<template>
  <div>
    <button v-scroll-to="'#top'">Scroll to Top</button>
    <div id="top">Top of Page</div>
    ...
  </div>
</template>

<script>
import VueScrollTo from 'vue-scrollto'

export default {
  ...
  directives: {
    scrollTo: VueScrollTo.directive
  }
  ...
}
</script>

In the code above, we added v-scroll-toa directive for the button and set it to scroll to the top of the page. We also added an element with id "top" to the top of the page in order to specify the scroll position.


in conclusion

Scrolling to the top or bottom of the page in Vue.js is a common requirement. By using the vue-scrollto plugin, we can easily achieve this function. Whether scrolling to the top or bottom of the page, we can use the v-scroll-to directive to specify the scroll position.

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

Display element or text on mouse hover in vue

Publish Date:2025/03/01 Views:177 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

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:65 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:185 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:138 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:85 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

How to find all elements by class name in Vue

Publish Date:2025/03/01 Views:182 Category:Vue

Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in

How to get the initial state of a certain data in Vue?

Publish Date:2025/03/01 Views:146 Category:Vue

In Vue, sometimes we want to get the initial state of a data in data so that we can compare or reset it in subsequent operations. In this article, we will introduce how to get the initial state of a data in data and provide an example to il

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial