This article mainly introduces the sample code for vue-scroller to record the scroll position. The content is quite good. I will share it with you now and give it as a reference.
Problem description:
The list page enters the details page, or switches the tab page, and then returns to the list page, hoping to switch to the previous scroll position
Solution to the problem:
Record the position before switching to other pages, and return to the position when returning to the list page. This needs to be achieved with the help of vue-router's beforeRouteEnter and beforeRouteLeave hooks.
There is also a simpler and more crude method. Add a judgment that the width and height are not zero in the vue-scroller.min.js source code. See the comments for the implementation method, which was discovered during recent code optimization.
Code part:
beforeRouteEnter(to,from,next){ if(!sessionStorage.askPositon || from.path == '/'){//当前页面刷新不需要切换位置 sessionStorage.askPositon = ''; next(); }else{ next(vm => { if(vm && vm.$refs.scrollerBottom){//通过vm实例访问this setTimeout(function () { vm.$refs.scrollerBottom.scrollTo(0, sessionStorage.askPositon, false); },0)//同步转异步操作 } }) } }, beforeRouteLeave(to,from,next){//记录离开时的位置 sessionStorage.askPositon = this.$refs.scrollerBottom && this.$refs.scrollerBottom.getPosition() && this.$refs.scrollerBottom.getPosition().top; next() },
Points to note:
1. Familiar with vue-router and vue -Scroller's api
2.beforeRouteEnter, you cannot access the vue instance through this, you need to use vm
3.setTimeout 0
Wait a moment When the weekly version is released, I will post a link so you can experience the effect
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to the method of using the better-scroll plug-in in Angular
How to understand Vue’s .sync modification The use of symbols
Introduction to the development of Vue drag and drop components
##
The above is the detailed content of Introduction to the code for vue-scroller to record the scroll position. For more information, please follow other related articles on the PHP Chinese website!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

在VSCode中开发Vue/React组件时,怎么实时预览组件?本篇文章就给大家分享一个VSCode 中实时预览Vue/React组件的插件,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.