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.点击元素高亮,滚动到对应区域

相关推荐
林晓lx几秒前
使用Git钩子+ husky + lint语法检查提高前端项目代码质量
前端·git·gitlab·源代码管理
王同学要变强27 分钟前
【深入学习Vue丨第二篇】构建动态Web应用的基础
前端·vue.js·学习
程序定小飞39 分钟前
基于springboot的web的音乐网站开发与设计
java·前端·数据库·vue.js·spring boot·后端·spring
Hello_WOAIAI41 分钟前
2.4 python装饰器在 Web 框架和测试中的实战应用
开发语言·前端·python
FinClip1 小时前
凡泰极客亮相香港金融科技周,AI助力全球企业构建超级应用
前端
阿四1 小时前
【Nextjs】为什么server action中在try/catch内写redirect操作会跳转失败?
前端·next.js
申阳1 小时前
Day 6:04. 基于Nuxt开发博客项目-LOGO生成以及ICON图标引入
前端·后端·程序员
中国lanwp1 小时前
npm中@your-company:registry 和 registry 的区别
前端·npm·node.js
Bacon1 小时前
Electron 应用商店:开箱即用工具集成方案
前端·github
行走的陀螺仪1 小时前
uni-app + Vue3 实现折叠文本(超出省略 + 展开收起)
前端·javascript·css·uni-app·vue3