scrollIntoView的基本定义、以及Vue3、vue2中使用: 点击导航滚动到对应区域。

news/2024/10/29 0:32:41/

1. 基本定义

MDN 关于scorllIntoView的介绍

Element 接口的 scrollIntoView() 方法会滚动元素的父容器,使被调用 scrollIntoView() 的元素对用户可见。

  1. scrollIntoView()
  2. scrollIntoView(alignToTop)
  3. scrollIntoView(scrollIntoViewOptions)

1. alignToTop 可选

alignToTop可选
一个布尔值
如果为 true,元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 scrollIntoViewOptions: {block: "start", inline: "nearest"}。这是这个参数的默认值。
如果为 false,元素的底端将和其所在滚动区的可视区域的底端对齐。相应的 scrollIntoViewOptions: {block: "end", inline: "nearest"}

2. scrollIntoViewOptions 可选

一个包含下列属性的对象:
behavior 可选
定义动画过渡效果,auto 或 smooth 之一。默认为 auto
block 可选
定义垂直方向的对齐,start、center、end 或 nearest 之一。默认为 start
inline 可选
定义水平方向的对齐,start、center、end 或 nearest 之一。默认为 nearest

例子

const element = document.getElementById("box");element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({ block: "end" });
element.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });

在这里插入图片描述

2. 使用

常用到的点击顶部导航滚动到对应的区域。
在这里插入图片描述

  • 注意点:
  1. block 设置为startcenter

  2. aliginTop设置true

    • 外层元素、以及导航区域不要使用margin-top、否则整个区域都会向上滚动。可以使用padding代替。比如:.scroll-into-view-page 添加margin-top就变成下面这样了
      在这里插入图片描述
      在这里插入图片描述
  3. 内容区域设置个固定高度比如。height: 400px;overflow-y: scroll;

Demo

<template><div class="index-page"><scroll-intoview></scroll-intoview></div>
</template><script setup>
import scrollIntoview from '@/components/common/scrollIntoview.vue'
</script>

2.1. vue3

scrollIntoview.vue

<template><div class="scroll-into-view-page"><div class="tab-nav-list"><div class="list-item btn-active" :class="tabActive === index ? 'act':''" v-for="(item, index) in tabList"@click="changeTab(item,index)">{{ item.title }}</div></div><div class="tab-content"><div class="item" :ref="el=>tabListRef[index] = el" v-for="(item, index) in tabList">{{ item.title }}</div></div></div>
</template><script setup>
import {reactive, ref} from "vue"const tabList = reactive([{title: '科学'},{title: '地理'},{title: '英语'},{title: '数学'}
])
const tabActive = ref(0)
const tabListRef = ref([])const changeTab = (item, index) => {if(tabActive.value === index) returntabActive.value = indextabListRef.value[index].scrollIntoView({behavior: 'smooth', block: 'start'})
}
</script>
<style lang="less" scoped>
.scroll-into-view-page {font-size: 12px;color: #1D2129;z-index: 2;background: #f7f7f8;//margin-top: 20px;.tab-nav-list {border-radius: 4px;display: flex;align-items: center;justify-content: space-between;padding: 0 16px;height: 32px;background: #3d4f5b;font-size: 14px;.list-item {color: #8592A6;padding: 2px 6px;border-radius: 2px;&.act {transition: all ease .3s;color: #FFFFFF;background: #b7b3b4;font-weight: bold;}}}.tab-content {padding: 0 16px;margin-top: 16px;font-size: 20px;height: 400px;overflow-y: scroll;.item {margin-bottom: 10px;&:first-child {height: 300px;background: #568686;}&:nth-child(2) {height: 130px;background: #807536;}&:nth-child(3) {height: 300px;background: olivedrab;}&:last-child {height: 200px;background: #477070;}}}
}
</style>

2.2. vue2

vue2中使用、主要就是获取dom元素方法改变下即可。

 <div class="item" v-for="(item, index) in tabList">{{ item.title }}</div>
<script>
export default {data() {return {tabList: [{title: '科学'},{title: '地理'},{title: '英语'},{title: '数学'}],tabActive: 0,tabListRef: []}},mounted() {this.getDomElements()},methods: {getDomElements() {this.$nextTick(()=> {let el = document.getElementsByClassName('tab-content')[0]this.tabListRef = el.children})},changeTab(item, index) {if(this.tabActive === index) returnthis.tabActive = indexthis.tabListRef[index].scrollIntoView({behavior: 'smooth', block: 'start'})}}
}
</script>

2.3 也可以用于横向滚动

修改下基本样式即可。

 .tab-content {white-space: nowrap;overflow-x: scroll;.item {display: inline-block;width: 100%;margin-bottom: 0;}}

在这里插入图片描述


http://www.ppmy.cn/news/68855.html

相关文章

[译] Flutter 3.10 的新功能

[译] Flutter 3.10 的新功能 原文 https://medium.com/flutter/whats-new-in-flutter-3-10-b21db2c38c73 无缝的Web和移动端集成&#xff0c;Impeller稳定版的突破性图形性能&#xff0c;以及更多 欢迎使用Flutter 3.10&#xff01;我们非常期待展示我们令人惊叹的Flutter社区所…

oracle用户账号被锁的问题

oracle数据库用户被锁 select username,account_status,lock_date from dba_users; select RESOURCE_NAME,RESOURCE_TYPE,LIMIT from dba_profiles where resource_name like FAILED_LOGIN_ATTEMPTS% AND profileDEFAULT; 3&#xff0c;解锁方法   ALTER USER USER_NAME…

方案设计——食物测温仪方案

食物测温仪&#xff0c;在食物烹饪时&#xff0c;温度和时间至关重要&#xff0c;所以食物测温仪孕育而生&#xff0c;当用户使用时只需将食物测温仪的探头插入食物中&#xff0c;即刻能得到当前食物温度数据&#xff0c;不必用经验判断。做为一款食物测温仪&#xff0c;运用场…

springboot + vue3实现视频播放Demo

文章目录 学习链接前言ffmpeg安装ffmpeg配置环境变量分割视频文件 后台配置WebConfig 前端代码video.js示例安装依赖视频播放组件效果 Vue3-video-play示例安装依赖main.js中使用视频播放组件效果 学习链接 ffmpeg官网 长时长视频java存储及vue播放解决方法 【 攻城略地 】vue…

微服务保护——Sentinel

初识Sentinel 雪崩问题 微服务调用链路中的某个服务故障&#xff0c;引起整个链路中的所有微服务都不可用&#xff0c;这就是雪崩。 解决雪崩问题的常见方式有四种: 超时处理:设定超时时间&#xff0c;请求超过一定时间没有响应就返回错误信息&#xff0c;不会无休止等待舱壁…

电力电子与电气行业常用英语单词汇总

1.共模电感---Coupled inductor 2.饱和电感---Saturable inductor 3.共模电感---coupled inductor 4.共模电感---coupled inductor 5.共模电感---coupled inductor 6.共模电感---coupled inductor 1.共模电感---coupled inductor 2.共模电感---coupled inductor 3.共模…

vue+elementui+nodejs机票航空飞机航班查询与推荐

语言 node.js 框架&#xff1a;Express 前端:Vue.js 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat 开发软件&#xff1a;VScode )本系统主要是为旅客提供更为便利的机票预定方式&#xff0c;同时提高民航的预定机票的工作效率。通过网络平台实现信息化和网络化&am…

教育单位启动网络安全排查,查询IP地址能做什么?

近日&#xff0c;南京教育局主办了一场“网安2023”教育行动&#xff0c;各级教育单位都将开始教育系统网络安全责任制&#xff0c;做实各项教育网络安全工作&#xff0c;提升教育系统网络安全防护能力。 本次行动采取远程安全检查与现场检查相结合方式对各级教育单位进行网络…