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

如何使用CSS和JS实现一个响应式的滚动时间轴

随着互联网的发展,网站的界面设计越来越重要。吸引用户的关注、提高用户体验已经成为了许多网站的目标。而在实现各种复杂的界面效果中,CSS与JS的组合无疑是开发者的得力工具。本文将介绍如何使用CSS和JS实现一个响应式的滚动时间轴。

1.需求分析

在开始实现之前,我们需要分析出需求。首先,我们需要一个时间轴的框架,其中包含一系列的事件节点,这些事件节点可以根据滚动条的位置来进行滚动。其次,在滚动条滚动期间,我们需要将当前事件节点高亮显示,以便用户更好地了解当前所处的时间位置。最后,如果用户拖拽滚动条时,我们需要平滑地接过控制权,并根据滚动条位置来定位事件节点。

2.准备工作

在开始实现之前,我们需要做一些准备工作。首先,我们需要准备好HTML结构,包括时间轴框架、事件节点等元素;其次,我们需要使用CSS样式来美化这些元素,并且确保它们能正确地显示在页面上;最后,我们需要使用JS来控制滚动条的位置并根据其位置来定位事件节点。下面是我们需要准备的HTML结构:

<div class="timeline__container"> <div class="timeline__track"> <ul class="timeline__events"> <li class="timeline__event" style="left: 0;"> <div class="timeline__event_title">Event 1</div> <div class="timeline__event_date">2023-05-03</div> </li> <li class="timeline__event" style="left: 20%;"> <div class="timeline__event_title">Event 2</div> <div class="timeline__event_date">2023-05-04</div> </li> <li class="timeline__event" style="left: 40%;"> <div class="timeline__event_title">Event 3</div> <div class="timeline__event_date">2023-05-05</div> </li> <li class="timeline__event" style="left: 60%;"> <div class="timeline__event_title">Event 4</div> <div class="timeline__event_date">2023-05-06</div> </li> <li class="timeline__event" style="left: 80%;"> <div class="timeline__event_title">Event 5</div> <div class="timeline__event_date">2023-05-07</div> </li> </ul> <div class="timeline__scrollbar"> <div class="timeline__thumb"></div> </div> </div> </div>

这段代码中,我们使用了一个div元素来作为时间轴的容器,其中包含了一个div元素作为时间轴的轨道。轨道中包含了一个无序列表ul,其中的每个列表项li代表一个事件节点。每个事件节点中包含了一个标题和日期。最后,我们还需要一个滚动条以便用户进行滚动。

接下来,我们需要使用CSS样式来美化这些元素并确保它们能正确地显示在页面上。

.timeline__container { width: 100%; height: 500px; position: relative; } .timeline__track { width: 80%; height: 500px; background-color: #fff; margin: 0 auto; position: relative; overflow-x: hidden; } .timeline__events { display: flex; position: absolute; top: 50%; transform: translateY(-50%); left: 0; margin: 0; padding: 0; width: 100%; } .timeline__event { width: 20%; height: 80%; margin: 0; padding: 0; position: relative; list-style: none; cursor: pointer; transition: all 0.3s ease-in-out; z-index: 1; text-align: center; } .timeline__event_title { font-size: 18px; font-weight: bold; margin-bottom: 10px; } .timeline__event_date { font-size: 14px; } .timeline__event::before { content: ""; display: block; position: absolute; top: 50%; transform: translateY(-50%); left: -5px; width: 10px; height: 10px; border-radius: 50%; background-color: #fff; border: 3px solid #000; z-index: 2; } .timeline__event:hover { transform: scale(1.2); z-index: 3; } .timeline__event.active { transform: scale(1.2); z-index: 3; } .timeline__scrollbar { position: absolute; bottom: 0; left: 0; width: 100%; height: 20px; background-color: #eee; } .timeline__thumb { position: absolute; top: 0; left: 0; width: 20%; height: 100%; background-color: #ccc; cursor: pointer; }

这段代码中,我们设置了时间轴容器的宽高和位置相关属性。其中轨道的宽度为80%,高度为500px,并且在水平方向上隐藏了超出的内容。事件节点使用了flex布局,并通过调整样式让它们居中放置于轨道上。事件节点带有标题和日期,并通过伪元素实现了一个小球来标志每个事件节点。当鼠标悬浮在节点上时,其大小会变大以及增加z-index属性,以便用户更好地了解当前所处的事件节点。最后,我们还设置了滚动条的样式。

3.实现

接下来,我们使用JS来实现滚动事件处理。首先,我们需要获取一些元素。

const timelineContainer = document.querySelector(".timeline__container"); const timelineEvents = document.querySelector(".timeline__events"); const timelineThumb = document.querySelector(".timeline__thumb");

然后,我们需要计算出时间轴的实际宽度、事件节点之间的距离以及滚动条的宽度。

const timelineWidth = timelineEvents.scrollWidth - timelineContainer.clientWidth; const eventSpacing = (100 / (timelineEvents.children.length - 1)); const thumbWidth = (100 - (eventSpacing * 2)) / timelineWidth * 100; timelineThumb.style.width = `${thumbWidth}%`;

接下来,我们需要监控滚动条的变化,并根据变化计算出当前应该高亮显示的事件节点。

let dragging = false; let scrollX = 0; let activeEventIndex = 0; timelineContainer.addEventListener("mousedown", (e) => { dragging = true; scrollX = e.clientX - timelineContainer.offsetLeft - (timelineThumb.clientWidth / 2); }); timelineContainer.addEventListener("mousemove", (e) => { if (!dragging) return; let newX = e.clientX - timelineContainer.offsetLeft - (timelineThumb.clientWidth / 2); let left = Math.max(0, Math.min(newX, timelineContainer.clientWidth - timelineThumb.clientWidth)); timelineThumb.style.left = `${left}px`; timelineEvents.style.transform = `translateX(-${(left / timelineWidth) * 100}%)`; activeEventIndex = Math.round((left / timelineWidth) * (timelineEvents.children.length - 1)); setActiveEvent(); }); timelineContainer.addEventListener("mouseup", () => { dragging = false; }); function setActiveEvent() { Array.prototype.slice.call(timelineEvents.children).forEach((event, index) => { event.classList.toggle("active", index === activeEventIndex); }); }

在这段代码中,我们添加了鼠标按下、鼠标移动和鼠标抬起事件处理函数来监控滚动条的变化。当用户拖拽滚动条时,我们将计算出当前应该高亮显示的事件节点,并调用setActiveEvent()函数来更新它们的类名以便进行高亮显示。最后,我们还需要将滑块和时间轴上的事件节点位置同步。

4.总结

通过本文的介绍,我们学习了如何使用CSS和JS实现一个响应式的滚动时间轴。我们首先进行了需求分析,然后准备好HTML结构和CSS样式,最后使用JS实现了滚动事件处理。在实际开发中,我们可以根据自己的需求进行修改,并将其应用到网站中,为用户提供更好的体验。

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

相关文章:

  • Feign组件的使用及开发中使用方式
  • html css 面试题
  • LeetCode_双指针_中等_24.两两交换链表中的节点
  • 【openGauss实战11】性能报告WDR深度解读
  • Vue3实现打字机效果
  • maven无法依赖spring-cloud-stater-zipkin如何解决?
  • 实战踩坑---MFC---CreateEvent
  • JavaWeb学习------jQuery
  • 米哈游测开岗 【一面总结】
  • 微服务 Spring Boot 整合Redis 实现优惠卷秒杀 一人一单
  • 构建OVS网络
  • 【Python】万能之王 Lambda 函数详解
  • 手把手教你怎么搭建自己的AI数字人直播间?帮你24小时不间断直播卖货
  • MySQL性能监控全掌握,快来get关键指标及采集方法!
  • sed进阶之保留空间和排除命令
  • 21安徽练习
  • 【VAR | 时间序列】应用VAR模型时的15个注意点
  • 校招在线测评题目汇总
  • 『python爬虫』05. requests模块入门(保姆级图文)
  • WPF超好用的框架Prism入门使用,上位机赶紧学起来!
  • 十个机器学习应用实例
  • 【Redis17】Redis进阶:管道
  • Django项目页面样式如何“传给”客户端浏览器
  • python 进程间通信 Queue()、Pipe()、manager.list()、manager.dict()、manager.Queue()
  • 你想要的【微前端】都在这里了! | 京东云技术团队
  • 人生若只如初见,你不来看看Django吗
  • 项目人力资源管理
  • 提供接口给第三方调用,应该注意什么
  • ESL设计概述
  • 探究C语言数组的奥秘:大小可省略的定义、内存存储、数组名、传参、指针遍历、数组指针和指针数组、柔性数组等