当前位置: 首页 > news >正文

vue中重新获取数据导致页面加长,要求在页面更新之后浏览器滚动条滚动到之前浏览记录的位置。以及获取当前页面中是哪个元素产生滚动条的方法。

目前的页面样式为:

 代码是:

  <section id="detailSection"><el-tableref="multipleTable":data="logDetailList"style="width: 650px;margin:20px auto;"id="dialogDetail":show-header="false":cell-style="cellStyle"v-loading="formLoading"><el-table-columnclass="log-detail-origin_timestamp"prop="_source.origin_timestamp"label="时间"width="185"></el-table-column><el-table-column class="log-detail-msg" label="查询的log"><template slot-scope="props"><el-buttonclass="asidebtn-up"v-if="props.$index == 0"type="primary"@click="searchUp">向上查询</el-button><el-buttonclass="asidebtn-down"v-if="props.$index == logDetailList.length - 1"type="primary"@click="searchDown">向下查询</el-button></template></el-table-column></el-table></section>

首先获取当前el-table的高度(此处$refs后的multipleTable是在el-table中用ref绑定的值)

 let positionHeight = this.$refs.multipleTable.bodyWrapper.scrollHeight;

其次用接口获取新数据(也就是更新logDetailList里的值),将新数据渲染到页面中。

最后用$nextTick方法在数据渲染完成之后的滚动条定位定到之前的位置上。

(1)获取新数据的el-table长度

let positionHeight2 = this.$refs.multipleTable.bodyWrapper.scrollHeight;

(2)长度相减。新数据页面的长度-旧数据页面长度=当前滚动条滚动位置

let screenHeight = positionHeight2 - positionHeight;

(3)设置滚动条位置。注:scrollTo方法必须是该页面存在滚动条的元素。且必须是el-table的父元素。

document.querySelector(".content-container").scrollTo(0, screenHeight);

这样就完成了。

附加 :

获取当前页面中是哪个元素产生滚动条的方法(vue中):页面滚动后控制台会打印

methods: {    findscroller(element) {element.onscroll = function() {console.log(element);};Array.from(element.children).forEach(this.findscroller);}
}
mounted() {this.findscroller()
}

 或者直接复制以下代码到控制台中。回车,拖动滚动条

function findscroller(element){element.onscroll=function () {console.log(element)}Array.from(element.children).forEach(findscroller)
}
findscroller(document.body)

 

http://www.lryc.cn/news/95386.html

相关文章:

  • 【深度学习】日常笔记14
  • [JAVAee]synchronized关键字
  • Unity游戏源码分享-3d机器人推箱子游戏
  • SAAS部署模式
  • 11、PHP面向对象1
  • 实训笔记7.25
  • 全方位对比 Postgres 和 MongoDB (2023 版)
  • 本地部署中文LLaMA模型实战教程,民间羊驼模型
  • 全志F1C200S嵌入式驱动开发(spi-nor image制作)
  • JSON格式Python,Java,PHP等封装图片识别商品数据API方法
  • Vue应用案例
  • GPT-3.5:ChatGPT的奇妙之处和革命性进步
  • 【Hadoop 01】简介
  • 【C++】开源:跨平台轻量日志库easyloggingpp
  • spring-websocket在SpringBoot(包含SpringSecurity)项目中的导入
  • SpringBoot + Vue前后端分离项目实战 || 六:Jwt加密整合配置
  • WPF 如何设置全局的订阅发布事件
  • STM32 USB使用记录:HID类设备(前篇)
  • 探索AI图像安全,助力可信AI发展
  • vue 学习笔记 【ElementPlus】el-menu 折叠后图标不见了
  • 【JavaEE初阶】HTTP协议
  • 基于SaaS模式的Java基层卫生健康云HIS系统源码【运维管理+运营管理+综合监管】
  • effective c++ 条款2
  • Python爬虫之Scrapy框架系列(23)——分布式爬虫scrapy_redis浅实战【XXTop250部分爬取】
  • html基于onmouse事件让元素变颜色
  • Linux环境PostgreSQL安装
  • Rust 数据类型 之 结构体(Struct)
  • 数据结构之Queue的实现
  • rust声明式宏
  • 第二章:Learning Deep Features for Discriminative Localization ——学习用于判别定位的深度特征