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

Problems and solutions of using TweenMax animation library in angular

I have nothing to do recently, so I tinker with C...

Vue.js uses Element-ui to implement the navigation menu

This article shares the specific code for impleme...

Summary of practical experience of HTML knowledge points

1. The table tag is table, tr is row, td is cell, ...

Example of how to create a database name with special characters in MySQL

Preface This article explains how to create a dat...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

The difference between div and span in HTML (commonalities and differences)

Common points: The DIV tag and SPAN tag treat som...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

How to get form data in Vue

Table of contents need Get data and submit Templa...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...

How to install MySQL for beginners (proven effective)

1. Software Download MySQL download and installat...

How to use resident nodes for layer management in CocosCreator

CocosCreator version: 2.3.4 Most games have layer...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...