Example of using store in vue3 to record scroll position

Example of using store in vue3 to record scroll position

Overall Effect

When you scroll through the homepage list and enter the details page, you can return to the previously browsed location when you switch back to the homepage.

Listen for container scroll events

Define a scroll event and bind it to the container's scroll event. I've done some throttling here.

const savePosY = () => {
      if(state.timer) return;
      state.timer = setTimeout(() => {
        let node = document.querySelector(".contentWrapper");
        //Record scroll positionstore.commit("setY",node.scrollTop)
        state.timer = null;
        clearTimeout(state.timer);
      },100)

Get the container in mounted to bind the event

onMounted(() => {
    let contentWrapper = document.querySelector(".contentWrapper");
    contentWrapper.addEventListener("scroll",savePosY);
})

Configuration in the store

The store is relatively simple, containing only one state: y and mutations: setY

export default {
    state:{
         y:0
    },
    mutations:
        setY(state,value){
            state.y = value;
        }
    }
}

Get the scroll position when the page jumps back

Also operate in onMounted, otherwise the container element cannot be obtained, and because DOM in Vue is rendered asynchronously, we need to operate in nextTick to be effective

nextTick(() => { contentWrapper.scrollTop = store.state.y; })

at last

The above is the full content of this article. If there is something wrong or a better method, you are welcome to communicate and point it out.

The above is the details of the example of how vue3 uses store to record the scroll position. For more information about vue to record the scroll position, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Keep-alive in Vue implements back without refreshing and keeps the scroll position
  • Solve the problem that Vue cannot set the scroll position
  • How to remember the scroll position when entering the details page in Vue (keep-alive)
  • Solution to the bug that the page scroll position remains unchanged after vue2.0 route switching
  • Vue scroller returns the page to remember the scroll position example code
  • vue-scroller sample code for recording scroll position
  • How to solve the problem of fixed scroll position of Vue page

<<:  How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

>>:  How to view and clean up Docker container logs (tested and effective)

Recommend

Vue integrates a rich text editor that supports image zooming and dragging

need: According to business requirements, it is n...

Deployment and configuration of Apache service under Linux

Table of contents 1 The role of Apache 2 Apache I...

Detailed explanation of anonymous slots and named slots in Vue

Table of contents 1. Anonymous slots 2. Named slo...

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

MySQL 8.0.23 free installation version configuration detailed tutorial

The first step is to download the free installati...

Automatic file synchronization between two Linux servers

When server B (172.17.166.11) is powered on or re...

Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial

Preparation 1. Environmental Description: Operati...

VMware and CentOS system installation method to reset the root password

Today's Tasks 1. Choice of Linux distribution...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

Issues with locking in MySQL

Lock classification: From the granularity of data...

Sample code for installing ElasticSearch and Kibana under Docker

1. Introduction Elasticsearch is very popular now...

CSS3 mobile vw+rem method to achieve responsive layout without relying on JS

1. Introduction (1) Introduction to vw/vh Before ...

Solution to the problem that docker nginx cannot be accessed after running

## 1 I'm learning docker deployment recently,...