使用javascript实现电梯导航(循环)

文章讲述了如何通过JavaScript操作DOM和CSS样式,实现在页面布局中实现楼层的动态高度调整以及与电梯导航栏联动的滚动效果,包括获取楼层高度、添加点击和滚动事件处理。
摘要由CSDN通过智能技术生成

页面布局

在这里插入图片描述
后期会调整每块的高度,方便实现滑动效果


        <div class="content">
            <div style="height: 500px;width: 90%; background-color: antiquewhite;">第一块</div>
            <div style="height: 500px;width: 90%; background-color: aquamarine;">第二块</div>
            <div style="height: 500px;width: 90%; background-color: cadetblue;">第三块</div>
            <div style="height: 1000px;width: 90%; background-color:gainsboro;">第四块</div>
         </div>
         <div class="elevator">
              <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
              </ul>
         </div>

逻辑实现

实现思路:

建议看完代码再来看
1.我们首先获得楼层据顶部的距离,并且存储在一个数组中
2通过document.querySelectorAll()获得楼层和楼层按钮的伪数组,使得每个楼层与其楼层的按钮相同
3.添加点击事件,点击楼层按钮就跳转到与楼层按钮下标相应的楼层(设置页面滚动的高度)
4.添加滚动事件,使得滚动相应的位置,就给与滚动位置的模块相同的i添加active属性
解释变量含义

 // 获得导航需要的楼层
    const floor=document.querySelectorAll('.content div')
    // 获取电梯导航栏中所有的li,可以理解为楼层按钮
    const navBar=document.querySelectorAll('.elevator li')
    // 获得导航栏盒子
    const elevator=document.querySelector('.elevator')
    // 定义一个数组储存楼层高度
     let floorHeight = []

点击相应的楼层按钮,跳转到相应的楼层

for(let i=0;i< floor.length;i++){
      //  获得每个楼层据顶部的高度
      floorHeight[i]=floor[i].offsetTop
      // 给每个楼层添加点击事件
      navBar[i].addEventListener('click',function(){
        // 切换active事件,楼层按钮高亮显示
        let active=document.querySelector('.elevator .active')
        if(active)  active.classList.remove('active')
        this.classList.add('active')
        // 页面滚动到相应的楼层
        document.documentElement.scrollTop=floorHeight[i]
        
         
      })
    }

页面滚动到相应的位置,进行高亮显示

 window.addEventListener('scroll',function(){
    const n=document.documentElement.scrollTop
    for(let i=0;i< navBar.length;i++){
      console.log(i);
      if((n> floorHeight[i]&&n<floorHeight[i+1])||(i===navBar.length-1&&n>floorHeight[i])){
        let active=document.querySelector('.elevator .active')
        if(active)  active.classList.remove('active')
         navBar[i].classList.add('active')
      }
    } 
   })

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电梯导航</title>
    <style>
      *{
        padding: 0;
        margin: 0;
      }
       
       .content{
          width:90%;
          margin: 0 auto;
       }
       .content  div{
        margin-bottom: 20px ;
       }
       .elevator{
         position: fixed;
         top: 100px;
         right: 110px;
         background-color: darkgray;
       }
       .elevator ul{
        list-style-type: none;
          
         display: flex; 
         flex-direction: column;
        flex-wrap: wrap;
         justify-content: center; 
         align-items: center; 
       }
       .elevator li{
        width: 40px; 
        height: 40px;
        line-height: 40px; 
        font-size: 20px;
       padding-left: 20px;
       }
      .active{
        color: #db0000;
        background-color: aqua;
       }
    </style>
</head>
<body>
    
        <div class="content">
            <div style="height: 500px;width: 90%; background-color: antiquewhite;">第一块</div>
            <div style="height: 500px;width: 90%; background-color: aquamarine;">第二块</div>
            <div style="height: 500px;width: 90%; background-color: cadetblue;">第三块</div>
            <div style="height: 1000px;width: 90%; background-color:gainsboro;">第四块</div>
         </div>
         <div class="elevator">
              <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
              </ul>
         </div>
  <script>
    // 获得导航需要的楼层
    const floor=document.querySelectorAll('.content div')
    // 获取电梯导航栏中所有的li,可以理解为楼层按钮
    const navBar=document.querySelectorAll('.elevator li')
    // 获得导航栏盒子
    const elevator=document.querySelector('.elevator')
    // 定义一个数组储存楼层高度
     let floorHeight = []
    
// 点击相应的楼层按钮,跳转到相应的楼层
    for(let i=0;i< floor.length;i++){
      //  获得每个楼层据顶部的高度
      floorHeight[i]=floor[i].offsetTop
      // 给每个楼层添加点击事件
      navBar[i].addEventListener('click',function(){
        // 切换active事件,楼层按钮高亮显示
        let active=document.querySelector('.elevator .active')
        if(active)  active.classList.remove('active')
        this.classList.add('active')
        // 页面滚动到相应的楼层
        document.documentElement.scrollTop=floorHeight[i]
      })
    }


// 页面滚动到相应的位置,进行高亮显示
   window.addEventListener('scroll',function(){
    const n=document.documentElement.scrollTop
    for(let i=0;i< navBar.length;i++){
      console.log(i);
      if((n> floorHeight[i]&&n<floorHeight[i+1])||(i===navBar.length-1&&n>floorHeight[i])){
        let active=document.querySelector('.elevator .active')
        if(active)  active.classList.remove('active')
         navBar[i].classList.add('active')
      }
    } 
   })
  </script>
     
</body>
</html>
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,实现电梯导航可以采用 Vue.js 框架中的组件和指令。下面是一个简单的实现步骤: 1. 在 Vue 组件中定义电梯导航的结构,包括楼层按钮和电梯指示器。 ```html <template> <div class="elevator"> <div class="elevator-btns"> <button v-for="floor in floors" :key="floor" @click="goTo(floor)"> {{ floor }}F </button> </div> <div class="elevator-indicator"> <span class="current-floor">1F</span> </div> </div> </template> ``` 2. 在组件的 data 中定义楼层和当前楼层状态。 ```javascript data() { return { floors: [1, 2, 3, 4, 5], currentFloor: 1 } } ``` 3. 定义 goTo 方法,用于处理点击楼层按钮的事件,更新当前楼层状态并显示在电梯指示器中。 ```javascript methods: { goTo(floor) { this.currentFloor = floor this.$el.querySelector('.current-floor').textContent = floor + 'F' } } ``` 4. 最后,在组件的样式中添加电梯导航的样式,包括楼层按钮的样式和电梯指示器的样式。 ```css .elevator { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; align-items: center; } .elevator-btns { display: flex; flex-direction: column; align-items: center; margin-bottom: 10px; } .elevator-btns button { padding: 10px 20px; margin: 5px; font-size: 16px; } .elevator-indicator { background-color: #333; color: #fff; padding: 10px 20px; border-radius: 5px; } .elevator-indicator .current-floor { font-size: 24px; font-weight: bold; } ``` 这样,一个简单的电梯导航实现了。用户点击楼层按钮,电梯就会移动到相应的楼层,并在电梯指示器上显示当前楼层。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值