js仿京东电梯导航

文章描述了一种网页设计中,通过CSS和JavaScript实现的滚动导航栏与内容区域动态高亮的解决方案,当用户滚动时,对应的导航选项会根据滚动位置自动高亮显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<style>

    html {

      scroll-behavior: smooth;

    }

    body {

      height: 3000px;

      position: relative;

    }

    li {

      list-style: none;

    }

    ul {

      width: 5%;

      height: 328.8px;

      background-color: pink;

      color: #fff;

      position: fixed;

      right: 10px;

      top: 200px;

      opacity: 0;

    }

    ul li {

      width: 40px;

      padding: 10px 0;

    }

    .box div {

      width: 800px;

      height: 300px;

      background: pink;

      margin: 40px auto;

      font-size: 40px;

      color: #fff;

      text-align: center;

    }

    .active {

      color: forestgreen;

    }

  </style>

<div class="nav">

    <ul>

      <li data-name="new">新鲜好物</li>

      <li data-name="pula">人气推荐</li>

      <li data-name="hot">热门品牌</li>

      <li data-name="topic">最新专题</li>

      <li class="back">返回顶部</li>

    </ul>

  </div>

  <div class="box">

    <div class="box_new">新鲜好物</div>

    <div class="box_pula">人气推荐</div>

    <div class="box_hot">热门品牌</div>

    <div class="box_topic">最新专题</div>

  </div>

 <script>

//返回顶部

    (function () {

//获取元素

      const nav = document.querySelector("ul");

      const back = document.querySelector(".back");

      window.addEventListener("scroll", function () {

//获取页面的高度

        let n = parseInt(document.documentElement.scrollTop);

        nav.style.opacity = n >= nav.offsetTop ? 1 : 0;

      });

      back.addEventListener("click", function () {

        document.documentElement.scrollTop = 0;

      });

    })();

//点击对应的导航直接到对应的内容区

    (function () {

//获取元素

      const ul = document.querySelector("ul");

//把点击事件委托给ul

      ul.addEventListener("click", function (e) {

//先看能不能找到带有active类的元素

        if (document.querySelector(".active"))

          document.querySelector(".active").classList.remove("active");

        if (e.target.tagName === "LI" && e.target.dataset.name) {

          e.target.classList.add("active");

          document.documentElement.scrollTop = document.querySelector(

            `.box_${e.target.dataset.name}`

          ).offsetTop;

        }

      });

    })();

//下拉到对应内容区域,则导航高亮显示

    (function () {

      window.addEventListener("scroll", function () {

        const news = document.querySelector(".box_new").offsetTop;

        const pulas = document.querySelector(".box_pula").offsetTop;

        const hots = document.querySelector(".box_hot").offsetTop;

        const topics = document.querySelector(".box_topic").offsetTop;

        const ne = document.querySelector("[data-name=new]");

        const pula = document.querySelector("[data-name=pula]");

        const hot = document.querySelector("[data-name=hot]");

        const topic = document.querySelector("[data-name=topic]");

        const n = parseInt(document.documentElement.scrollTop);

        console.log(n);

        if (document.querySelector(".active"))

          document.querySelector(".active").classList.remove("active");

        // console.log(news,pulas,hots,topics);

        if (n >= news && n < pulas) {

          ne.classList.add("active");

        } else if (n >= pulas && n < hots) {

          pula.classList.add("active");

        } else if (n >= hots && n < topics) {

          hot.classList.add("active");

        } else if (n >= topics) {

          topic.classList.add("active");

        }

      });

    })();

  </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值