vue保存滚动位置

vue根据链接生成二维码
https://www.jianshu.com/p/44a0f1d4e86f
vue 定位滚动条位置
https://blog.csdn.net/qq_40963664/article/details/79850589

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,要缓存table滚动条的位置,你可以使用以下步骤: 1. 首先,在Vue的data中定义一个变量,用于存储滚动条的位置,比如scrollPosition。 2. 在table元素上添加一个scroll事件监听器,当滚动事件触发时,将滚动条的位置更新到scrollPosition中。 3. 在Vue的生命周期钩子函数中,比如created或mounted中,通过获取缓存中的滚动位置,并将其赋值给scrollPosition。 4. 在Vue的updated钩子函数中,将scrollPosition中存储的滚动位置重新应用到table元素上,以恢复滚动条的位置。 下面是一个示例代码: ```vue <template> <div> <table ref="table" @scroll="saveScrollPosition"> <!-- 表格内容 --> </table> </div> </template> <script> export default { data() { return { scrollPosition: 0 } }, created() { // 从缓存中获取滚动位置,并赋值给scrollPosition this.scrollPosition = localStorage.getItem('scrollPosition') }, mounted() { // 应用滚动位置到table元素上 this.$refs.table.scrollTop = this.scrollPosition }, updated() { // 更新滚动位置 this.$refs.table.scrollTop = this.scrollPosition }, methods: { saveScrollPosition() { // 监听滚动事件,并将滚动位置保存到scrollPosition中 this.scrollPosition = this.$refs.table.scrollTop // 将滚动位置保存到缓存中 localStorage.setItem('scrollPosition', this.scrollPosition) } } } </script> ``` 在这个示例中,我们使用localStorage来保存滚动位置,以便在页面刷新或重新加载后能够恢复滚动位置。你也可以使用其他方式来存储和读取滚动位置,比如vuex或cookie等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值