js简单实现京东的电梯导航

目录

css代码

html代码

js代码

完整代码

效果图:


思路:首先先搭建好结构,在写css样式

        由于京东本身一开始是看不见下拉的导航,就把这导航一开始用固定定位,并使其完全不显           示页面,用top的值为-100%,

        js,给窗口设置窗口滚动事件,根据页面被卷去的尺寸document.documentElement.scrollTop的值来判断,下拉导航什么时候出现,就设置其top值为0,不满足时保持原样。

css代码

  *{
            padding: 0;
            margin: 0;
        }
        .header {
            height: 80px;
            background-color: bisque;
        }

        .search {
            width: 1120px;
            height: 100px;
            margin: 0 auto;
            background-color: rgb(67, 165, 42);
        }
/* 导航区 */

        .daohang {
            height: 50px;
            background-color: pink;
            border-bottom: 5px solid red;
            transition: all 1s;
            /* 设置固定定位,位置为 -100%*/
            position: fixed;
            top: -100%;
            width: 100%;

        }

        .main {
            width: 1120px;
            height: 3000px;
            background-color: darkcyan;
            margin: 0 auto;
        }

        .box {
            width: 1120px;
            height: 100%;
            background-color: white;
        
            margin: 0 auto;
        }

        .left img {
            width: 125px;
            height: 40px;
        }

      .left{
        float: left;
        margin-right: 30px;
      }
      .right{
        float: left;
      }
      input{
        width: 500px;
        height: 28px;
        outline: none;
        border: none;
        float: left;
        /* background-color: aquamarine; */
       padding-left: 10px;
      }
      button{
        width: 60px;
        height: 30px;
        outline: none;
        border: none;
        float: right;
        color: white;
    
        background-color: rgb(225, 36, 26);
      }
      .s{
        width: 574px;
        height: 30px;
        border: 2px solid rgb(226, 35, 26);
      }
   .s{
    margin-top: 10px;
   }

html代码

    <div class="wrapper">
        <div class="header"></div>

        <div class="daohang">
            <div class="box">
                <div class="left">
                    <img src="./images/jindong.png" alt="">

                </div>
                <div class="right">
                    <div class="s">
                        <input type="text" placeholder="飞利浦电动牙刷"><button>搜索</button>
                    </div>

                </div>
            </div>

        </div>
        <div class="search"></div>
        <div class="main"></div>
    </div>

js代码

 const daohang=document.querySelector('.daohang')
        const head=document.querySelector('.header')
        //设置窗口滚动事件
        window.addEventListener('scroll',function(){
            //如果页面被卷去的尺寸大于800,就显示下拉导航,否则继续保持原样
            if(document.documentElement.scrollTop>800){
                daohang.style.top=0
            }else{
               daohang.style.top='-100%'
            }
        })

完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .header {
            height: 80px;
            background-color: bisque;
        }

        .search {
            width: 1120px;
            height: 100px;
            margin: 0 auto;
            background-color: rgb(67, 165, 42);
        }
/* 导航区 */

        .daohang {
            height: 50px;
            background-color: pink;
            border-bottom: 5px solid red;
            transition: all 1s;
            /* 设置固定定位,位置为 -100%*/
            position: fixed;
            top: -100%;
            width: 100%;

        }

        .main {
            width: 1120px;
            height: 3000px;
            background-color: darkcyan;
            margin: 0 auto;
        }

        .box {
            width: 1120px;
            height: 100%;
            background-color: white;
        
            margin: 0 auto;
        }

        .left img {
            width: 125px;
            height: 40px;
        }

      .left{
        float: left;
        margin-right: 30px;
      }
      .right{
        float: left;
      }
      input{
        width: 500px;
        height: 28px;
        outline: none;
        border: none;
        float: left;
        /* background-color: aquamarine; */
       padding-left: 10px;
      }
      button{
        width: 60px;
        height: 30px;
        outline: none;
        border: none;
        float: right;
        color: white;
    
        background-color: rgb(225, 36, 26);
      }
      .s{
        width: 574px;
        height: 30px;
        border: 2px solid rgb(226, 35, 26);
      }
   .s{
    margin-top: 10px;
   }

    </style>
</head>

<body>
    <div class="wrapper">
        <div class="header"></div>

        <div class="daohang">
            <div class="box">
                <div class="left">
                    <img src="./images/jindong.png" alt="">

                </div>
                <div class="right">
                    <div class="s">
                        <input type="text" placeholder="飞利浦电动牙刷"><button>搜索</button>
                    </div>

                </div>
            </div>

        </div>
        <div class="search"></div>
        <div class="main"></div>
    </div>
    <script>
        const daohang=document.querySelector('.daohang')
        const head=document.querySelector('.header')
        //设置窗口滚动事件
        window.addEventListener('scroll',function(){
            //如果页面被卷去的尺寸大于800,就显示下拉导航,否则继续保持原样
            if(document.documentElement.scrollTop>800){
                daohang.style.top=0
            }else{
               daohang.style.top='-100%'
            }
        })
    </script>
</body>

</html>

效果图:

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,实现电梯导航可以采用 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; } ``` 这样,一个简单电梯导航实现了。用户点击楼层按钮,电梯就会移动到相应的楼层,并在电梯指示器上显示当前楼层。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值