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

楼层滚动效果(超级简单,易懂)

系列文章目录

文章目录

  • 系列文章目录
  • 一、楼层滚动效果图如下
    • 1. 下图是纯Css实现的楼层滚动
    • 2.通过Js优化后的楼层滚动如下图(🌹🌹)
  • 二、楼层滚动(Css实现)
    • 1.滚动原理
    • 2.代码如下
  • 三、楼层滚动(Js+Css优化后的楼层滚动)
    • HTML、CSS代码如下
    • JS代码如下
  • 总结(收工)

一、楼层滚动效果图如下

1. 下图是纯Css实现的楼层滚动

在这里插入图片描述

2.通过Js优化后的楼层滚动如下图(🌹🌹)

在这里插入图片描述


二、楼层滚动(Css实现)

1.滚动原理

  1. 使用了一个 Css的属性值 scroll-behavior 。 对应的信息可以查找网上的资料。
  2. 使用了锚点功能。和对应的 标签上的 id属性

2.代码如下

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {padding: 0;margin: 0;}main {width: auto;height: 100vh;padding: 0 10px;overflow-y: scroll;scroll-behavior: smooth;}body ul {position: fixed;top: 50px;right: 50px;background-color: aliceblue;}ul li {width: 100%;height: 35px;line-height: 35px;text-align: center;border-bottom: 1px solid black;list-style: none;}ul li:active{background-color: rgb(255, 170, 0);}a {text-decoration: none;}a:hover {color: brown;}</style>
</head><body><ul><li> <a href="#sectionOne"> sectionOne</a> </li><li> <a href="#sectionTwo"> sectionTwo</a> </li><li> <a href="#sectionThree"> sectionThree</a> </li><li> <a href="#sectionFour"> sectionFour</a> </li><li> <a href="#sectionFive"> sectionFive</a> </li><li> <a href="#sectionSix"> sectionSix</a> </li><li> <a href="#sectionSeven"> sectionSeven</a> </li><li> <a href="#sectionEight"> sectionEight</a> </li></ul><main><section id="sectionOne" style="height: 500px;background-color: rgb(81, 185, 245);margin-bottom: 30px;">sectionOne</section><section id="sectionTwo" style="height: 500px;background-color: rgb(172, 176, 180);margin-bottom: 30px;">sectionTwo</section><section id="sectionThree" style="height: 500px;background-color: rgb(87, 216, 201);margin-bottom: 30px;">sectionThree</section><section id="sectionFour" style="height: 500px;background-color: aliceblue;margin-bottom: 30px;">sectionFour</section><section id="sectionFive" style="height: 500px;background-color: rgb(180, 153, 74);margin-bottom: 30px;">sectionFive</section><section id="sectionSix" style="height: 500px;background-color: rgb(96, 99, 175);margin-bottom: 30px;">sectionSix</section><section id="sectionSeven" style="height: 500px;background-color: rgb(224, 70, 238);margin-bottom: 30px;">sectionSeven</section><section id="sectionEight" style="height: 500px;background-color: rgb(226, 141, 101);margin-bottom: 30px;">sectionEight</section></main></body><script></script></html>

三、楼层滚动(Js+Css优化后的楼层滚动)

HTML、CSS代码如下

<style>* {padding: 0;margin: 0;}main {width: auto;height: 100vh;padding: 0 10px;overflow-y: scroll;scroll-behavior: smooth;}body ul {position: fixed;top: 50px;right: 50px;background-color: aliceblue;}ul li {width: 100%;height: 35px;line-height: 35px;text-align: center;border-bottom: 1px solid black;list-style: none;}a {display: inline-block;width: 100%;height: 100%;text-decoration: none;}a:hover {color: brown;}</style>
<body><ul><li><a href="#sectionOne"> sectionOne</a></li><li><a href="#sectionTwo"> sectionTwo</a></li><li><a href="#sectionThree"> sectionThree</a></li><li><a href="#sectionFour"> sectionFour</a></li><li><a href="#sectionFive"> sectionFive</a></li><li><a href="#sectionSix"> sectionSix</a></li><li><a href="#sectionSeven"> sectionSeven</a></li><li><a href="#sectionEight"> sectionEight</a></li></ul><main><section id="sectionOne" style="height: 500px;background-color: rgb(81, 185, 245);margin-bottom: 30px;">sectionOne</section><section id="sectionTwo" style="height: 500px;background-color: rgb(172, 176, 180);margin-bottom: 30px;">sectionTwo</section><section id="sectionThree" style="height: 500px;background-color: rgb(87, 216, 201);margin-bottom: 30px;">sectionThree</section><section id="sectionFour" style="height: 500px; background-color: aliceblue; margin-bottom: 30px">sectionFour</section><section id="sectionFive" style="height: 500px;background-color: rgb(180, 153, 74);margin-bottom: 30px;">sectionFive</section><section id="sectionSix" style="height: 500px;background-color: rgb(96, 99, 175);margin-bottom: 30px;">sectionSix</section><section id="sectionSeven" style="height: 500px;background-color: rgb(224, 70, 238);margin-bottom: 30px;">sectionSeven</section><section id="sectionEight" style="height: 500px;background-color: rgb(226, 141, 101);margin-bottom: 30px;">sectionEight</section></main>
</body>

JS代码如下

<script>let arr = [];let obj = nullvar sectionTags = document.querySelectorAll("section");var main = document.querySelector("main");var ul = document.querySelector("ul");var aTags = document.querySelectorAll("a");function removeColor() {aTags.forEach((element) => {element.style.background = "none";});}function getheight() {sectionTags.forEach((element) => {arr.push(element.offsetTop);});}function scrollMoveColor(e) {let scrollTop = e.srcElement.scrollTop;for (let i = 0; i < arr.length; i++) {if (scrollTop <= arr[i]) {removeColor();aTags[i].style.background = "rgb(255, 170, 0)";break;}}}getheight();main.addEventListener("scroll", scrollMoveColor);ul.addEventListener("click", function (e) {if (obj) {clearTimeout(obj)main.removeEventListener("scroll", scrollMoveColor);removeColor();e.target.style.background = "rgb(255, 170, 0)";}obj = setTimeout(() => {main.addEventListener("scroll", scrollMoveColor);}, 1000);});
</script>

总结(收工)

由于这里,只对跳转功能尽进行了展示。后续还会将【滚动条滚动到对应的区域】对应的标题进行高粱的功能进行优化!! 💪💪

目前:超简单的楼层滚动效果已经✅

如果对代码有疑问(🤔️)的👨‍🎓(👩‍🎓),一定要记得联系作者!!!!!!

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

相关文章:

  • FPGA、 CPU、GPU、ASIC区别
  • ChatGPT 之父承认 GPT-5 并不存在,为什么 OpenAI 总是这么实诚?|万字详述
  • 华为交换机配置telnet登录图文教程
  • Linux:网络基础1
  • Matlab对日期变量和时间变量的管理
  • js字符串 常用方法 并带详细讲解
  • Oracle_Audit_审计
  • python算法中的深度学习算法之生成对抗网络(详解)
  • 【VM服务管家】VM4.0软件使用_1.2 工具类
  • Android系统架构
  • 零基础想成为黑客,只需要四步
  • ChatGPT研究报告:AIGC带来新一轮范式转移
  • 自助式数据分析平台:jvs数据智仓-统计报表的使用条件及界面介绍
  • php连接sqlserver
  • Android 9.0 原生SystemUI下拉通知栏UI背景设置为圆角背景的定制(一)
  • vCenter(PSC)正常更改或重置administrator@vsphere.local用户的密码方法
  • 【五一创作】Java 反射
  • 常见元件、封装、尺寸、表面处理等
  • 作为一名8年测试工程师,因为偷偷接私活被····
  • 前端面试八股文
  • [创新工具和方法论]-02- DOE实验设计步骤
  • XXL-JOB分布式任务调度平台搭建以及和SpringBoot整合应用
  • 【LeetCode】236. 二叉树的最近公共祖先
  • STM32F4 HAL库使用DMA进行ADC采样实时发送波形到串口显示(包含傅里叶变换)
  • ChatGPT 平替天花板:HuggingFace 版 ChatGPT 来了,无需魔法无需等待直接起飞 ~
  • 桐乡学会计实操—小规模纳税人征收率的汇总帖来啦!
  • 权威学者、企业CFO荟聚上海国家会计学院,共探「智能会计 价值财务」
  • 根据cadence设计图学习硬件知识day06 了解一些电源转化芯片和 稳压器 和 开关芯片
  • 简单理解内存分页机制
  • 如何提高三维模型OSGB格式转换3DTILES的转换速度和数据质量