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

css伪元素实现li列表圆点相连+锚点跳转悬浮窗实现

实现效果:
在这里插入图片描述
html代码:

<div class="sidenav"><ul class="nav-text progressbar"><!-- data-target的值对应要跳转的模块的id --><li data-target="module1"><div class="text">锚点名称</div></li><li data-target="module2"><div class="text">锚点名称</div></li><li data-target="module3"><div class="text">锚点名称</div></li><li data-target="module4"><div class="text">锚点名称</div></li></ul>
</div>

给要跳转的dom元素id赋值即可。

css:

/* 侧边锚点跳转 */
.sidenav {z-index: 40;left: 1%;bottom: 15%;min-height: 55px;position: fixed;/* width: 140px; */background: #ffffff;box-shadow: 0px 0px 35px 0px rgba(106, 76, 248, 0.16);border-radius: 8px;padding: 25px 22px 0px 16px;
}.progressbar li {list-style-type: none;/* float: left; width: 33.33%;  */position: relative;text-align: center;font-size: 16px;align-items: center;display: flex;height: 42px;max-width: 102px;margin-bottom: 25px;
}.progressbar .text {-webkit-box-orient: vertical;display: -webkit-box;/* width: 64px; */width: 80px;text-align: left;-webkit-line-clamp: 2;overflow: hidden;text-overflow: ellipsis;
color: #333333;
cursor: pointer;
}.progressbar li:before {content: "";text-align: center;width: 12px;height: 12px;
background: #999999;border-radius: 50%;margin-right: 10px;
}.progressbar li:after {/* 伪元素实现li圆点垂直相连的线*/top: -13px;transform: translate(-50%, 0) rotate(90deg);content: "";position: absolute;width: 66%;height: 1px;background: #999999;;left: 6px;z-index: -1;
}.progressbar li:first-child:after {content: none;
}

js:

 // 避免点击事件触发时,触发滚动事件增加active类的函数var isProgressBarClick = false;// 获取悬浮窗的ul元素const moduleList = document.querySelector('.progressbar');var timeoutId = null;// 滚动到对应模块并添加active类的点击事件处理函数function scrollToModule(event){isProgressBarClick = true;var aim = event.target;if(event.target.className === 'text'){aim = event.target.parentNode}const targetModuleId = aim.getAttribute('data-target');const targetModule = document.getElementById(targetModuleId);if (targetModule) {// 使用平滑滚动实现滚动效果targetModule.scrollIntoView({ behavior: 'smooth' });// 移除之前被添加的active类const activeItem = document.querySelector('.progressbar li.active');if (activeItem) {activeItem.classList.remove('active');}// 为当前点击的列表项添加active类aim.classList.add('active');}// 等待锚点跳转完成后再重置标志变量clearTimeout(timeoutId);timeoutId = setTimeout(function() {isProgressBarClick = false;}, 1000);}$('.progressbar li').click(scrollToModule)$('.progressbar li .text').click(scrollToModule)// 监听页面滚动事件window.addEventListener('scroll', handleScroll);function handleScroll() {if(!isProgressBarClick){// 获取页面滚动的垂直位置const scrollPosition = window.scrollY;// 遍历模块元素,找到当前可见的模块var activeModuleId = null;var moduleElements = document.querySelectorAll('.module_list>div')for (const moduleElement of moduleElements) {// 用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置const { top, bottom } = moduleElement.getBoundingClientRect();// 判断模块是否在视口内(至少一半在视口内)if (top <= window.innerHeight / 2 && bottom >= window.innerHeight / 2) {activeModuleId = moduleElement.id;break;}}// 添加active类if (activeModuleId) {// 移除之前被添加的active类const activeItem = document.querySelector('.progressbar li.active');if (activeItem) {activeItem.classList.remove('active');}// 为当前可见的模块对应的列表项添加active类const activeListItem = document.querySelector(`.progressbar li[data-target="${activeModuleId}"]`);if (activeListItem) {activeListItem.classList.add('active');}}}}// 初始加载时触发一次滚动事件handleScroll();
http://www.lryc.cn/news/130519.html

相关文章:

  • IronPDF for .NET Crack
  • 【C++进阶】继承、多态的详解(多态篇)
  • excel快速选择数据、选择性粘贴、冻结单元格
  • 【数仓建设系列之一】什么是数据仓库?
  • Vue2-配置脚手架、分析脚手架、render函数、ref属性、props配置项、mixin配置项、scoped样式、插件
  • VS2015项目中,MFC内存中调用DLL函数(VC6生成的示例DLL)
  • 人流目标跟踪pyqt界面_v5_deepsort
  • angular 子组件ngOnChanges监听@input传入的输入属性
  • 移植PeerTalk开源库IOS的USB通信监听服务到QT生成的FFmpeg工程
  • PHREEQC模型化学热力学理论和数据库.dat、各种模拟反应平衡反应模拟、化学动力模拟、反应迁移模拟
  • centos下使用jemalloc解决Mysql内存泄漏问题
  • 【100天精通python】Day41:python网络爬虫开发_爬虫基础入门
  • 开源和自研——机器人
  • 【AIGC 讯飞星火 | 百度AI|ChatGPT| 】智能对比
  • Wazuh安装及使用
  • docker pull 设置代理 centos
  • 仪表板展示 | DataEase看中国:2023年中国电影市场分析
  • 在APP中如何嵌入小游戏?
  • 神经网络基础-神经网络补充概念-02-逻辑回归
  • DICOM图像的常用一些参数解析
  • Java虚拟机(JVM):虚拟机栈溢出
  • MySQL流程控制
  • 智安网络|深入比较:Sass系统与源码系统的差异及选择指南
  • Day14 01-Shell脚本编程详解
  • NVIDIA GPU驱动和CUDA工具包 Linux CentOS 7 在线安装指南
  • Php“牵手”拼多多商品详情页数据采集方法,拼多多API接口申请指南
  • 未来公文的智能化进程
  • C语言:深度学习知识储备
  • 探索大模型时代下的算法工程师前景与发展路径
  • 【福建事业单位-综合基础知识】03行政法