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

Vue 让视图区域滑到指定位置、回到顶部

滑倒指定位置:获取指定的dom,然后用scrollIntoView使dom出现在视图区域

回到顶部:操作父级dom的scrollTop = 0,让该父级下的列表回到顶部

代码如下

<template><div class="testDemo"><div><el-button @click="toItem('id50')">滑到指定位置</el-button><el-button @click="toTop">回到顶部</el-button></div><div class="box"><divv-for="(item, index) in 99":key="index":id="'id' + item"class="box_item">{{ item }}</div></div></div>
</template><script>
export default {data() {return {};},methods: {// 滑到指定位置toItem(id) {this.$nextTick(() => {const node = document.getElementById(id); // 通过Id获取到对应的dom元素setTimeout(() => {if (node) {this.$nextTick(() => {node.scrollIntoView({behavior: "smooth", //顺滑的滚动block: "center", //容器上下的中间 start:顶部 end:底部 center:垂直方向居中inline: "start", //容器左右的左边}); // 通过scrollIntoView方法将对应的dom元素定位到可见区域 【block: 'center'】这个属性是在垂直方向居中显示});}}, 100);});},// 回到顶部toTop() {// 第一种方式// var et = document.getElementsByClassName("box");// if (et && et.length) {//   et[0].scrollTop = 0;// }// 第二种方式 较为平滑this.$nextTick(() => {const node = document.getElementById("id1");setTimeout(() => {if (node) {this.$nextTick(() => {node.scrollIntoView({behavior: "smooth",block: "start",inline: "start",});});}}, 100);});},},
};
</script>
<style lang="scss" scoped>
.testDemo {display: flex;flex-direction: column;.box {flex-grow: 1;border: 1px solid #000;overflow: auto;.box_item {height: 50px;border: 1px solid #000;}}
}
</style>
http://www.lryc.cn/news/504366.html

相关文章:

  • EasyGBS点对点穿透P2P远程访问技术在安防视频监控中的应用
  • Android 使用 Gson + OkHttp 实现 API 的常规使用(个人心得)
  • WPF+MVVM案例实战与特效(三十九)- 深度剖析一个弧形进度条的实现
  • opencv——图片矫正
  • 前端核心知识总结
  • 【C语言】五子棋(c语言实现)
  • 【数据结构——查找】顺序查找(头歌实践教学平台习题)【合集】
  • Python的3D可视化库【vedo】2-1 (plotter模块) 绘制器的使用
  • 6.1 初探MapReduce
  • 【数模学习笔记】模糊综合评价
  • 【C语言】库函数常见的陷阱与缺陷(四):内存内容操作函数[1]--memcmp
  • jmeter CLI Mode 传参实现动态设置用户数
  • 数据库和SQL的基本概念
  • CSS系列(9)-- Transform 变换详解
  • 一些浅显易懂的IP小定义
  • C 语言动态爱心代码
  • 【Figma_01】Figma软件初始与使用
  • 【Python篇】PyQt5 超详细教程——由入门到精通(序篇)
  • day2 数据结构 结构体的应用
  • CSS 进阶教程:从定位到动画与布局
  • Nginx性能优化全方案:打造一个高效服务器
  • 详解Maven的setting配置文件中mirror和repository的区别
  • 框架模块说明 #07 API加密
  • 安卓BLE蓝牙开发经验分享
  • 后缀表达式有什么场景应用
  • 使用 Kubernetes 部署 Redis 主从及 Sentinel 高可用架构(未做共享存储版)
  • AI开发 - 用GPT写一个GPT应用的真实案例
  • C#—索引器
  • 杨振宁大学物理视频中黄色的字去掉(稳定简洁版本,四)
  • 排序算法(5):归并排序