JS APl关于电梯导航做法(ES6)

文章目录

文章目录

需求:

整体架构流程

技术名词解释

代码展示:

小结


需求:

1.点击哪个模块可以跳转的对应的模块

2.点击哪个模块对应的模块显示高亮(就是哪个盒子背景色变红色)

整体架构流程

// 1. 获元取素

document.querySelectorAll

// 2.内容的盒子获取

//3.. 左侧aside 模块 点击谁,谁高亮

这里用到冒泡就是获取父盒子就可以

//4.// 找到上一个active 移除类

例如:

在语言模型中,编码器和解码器都是由一个个的 Transformer 组件拼接在一起形成的。

技术名词解释

例如:

  • querySelectorAll(获取元素 但是返回是一组伪数组)
  • querySelector(获取元素)
  • this.classList.add(追加类)
  • document.documentElement.scrollTop(整个盒子被卷去的举例)

代码展示:

HTML:

html 复制代码
  <div class="aside">
        <div class="item active">男装/女装</div>
        <div class="item">儿童服装/游乐园</div>
        <div class="item">电子产品</div>
        <div class="item">电影/美食</div>
    </div>

    <div class="content">
        <div class="neirong content1">男装/女装</div>
        <div class="neirong content2">儿童服装/游乐园</div>
        <div class="neirong content3">电子产品</div>
        <div class="neirong content4">电影/美食</div>
    </div>

css:

css 复制代码
 <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 3000px;
        }

        .aside {
            position: fixed;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }

        .item {
            height: 40px;
            line-height: 40px;
            text-align: center;
            padding: 0 10px;
            cursor: pointer;
        }

        .active {
            background-color: red;
            color: #fff;
        }

        .content {
            width: 660px;
            margin: 400px auto;
        }

        .neirong {
            height: 300px;
            margin-bottom: 20px;
            color: #fff;
        }

        .content1 {
            background-color: red;
        }

        .content2 {
            background-color: blue;
        }

        .content3 {
            background-color: orange;
        }

        .content4 {
            background-color: yellowgreen;
        }
    </style>
javascript 复制代码
 <script>

        // 1. 获元取素  
        const items = document.querySelectorAll('.item')
        // 内容的盒子获取
        const neirongs = document.querySelectorAll('.neirong')
        // 2. 左侧aside 模块 点击谁,谁高亮
        for (let i = 0; i < items.length; i++) {
            items[i].addEventListener('click', function () {
                // 找到上一个active 移除类
                document.querySelector('.aside .active').classList.remove('active')
                // 点击谁谁添加类
                this.classList.add('active')
                // 3. 右侧内容跟随走动  让页面滚动到对应的offsetTop值位置
                // console.log(neirongs[i].offsetTop) 不用给单位
                document.documentElement.scrollTop = neirongs[i].offsetTop
            })
        }

    </script>

小结:

我们需要实现点击某一个按钮,然后滚动到对应的区域。 滚动的时候,右侧对应的分类实现高亮 其实,这个功能就2个步骤: 1.点击元素高亮,滚动到对应区域

相关推荐
比老马还六3 小时前
Bipes项目二次开发/扩展积木功能(八)
前端·javascript
易营宝3 小时前
全球建站SaaS平台能提升SEO评分吗?是否值得切换?
大数据·前端·人工智能
C_心欲无痕3 小时前
Next.js 的服务端路由:对应api文件夹
开发语言·javascript·ecmascript
513495923 小时前
在Vue.js项目中使用docx和file-saver实现Word文档导出
前端·vue.js·word
AC赳赳老秦4 小时前
Prometheus + DeepSeek:自动生成巡检脚本与告警规则配置实战
前端·javascript·爬虫·搜索引擎·prometheus·easyui·deepseek
接着奏乐接着舞。4 小时前
前端大数据渲染性能优化:Web Worker + 分片处理 + 渐进式渲染
大数据·前端·性能优化
Beginner x_u4 小时前
CSS 中的高度、滚动与溢出:从 height 到 overflow 的完整理解
前端·css·overflow·min-height
vx1_Biye_Design4 小时前
基于web的物流管理系统的设计与实现-计算机毕业设计源码44333
java·前端·spring boot·spring·eclipse·tomcat·maven
tqs_123455 小时前
倒排索引数据结构
java·前端·算法
a程序小傲5 小时前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先