HTML+CSS+JavaScript实现下拉菜单效果

实现思路

        HTML 方面,导航栏的每个 <li> 元素里面包含 <a> 和 <ul> ,将 <li> 设置为相对定位, 将 <ul> 设置为绝对定位,并将 <ul> 的 display 属性设置为 none 。通过 js 给导航栏的每个 <li> 添加事件监听器,当鼠标覆盖 <li> 时,令 <ul> 的 display 属性值为 block;当鼠标离开 <li> 时,令 <ul> 的 display 属性值为 none;

效果演示:

源代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>下拉菜单</title>
    <style>   
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background-color: #ffb900;
        }
        a {
            cursor: default;
            text-decoration: none;
            font-size: 20px;
            font-weight: 700;
            color: #fff;
            letter-spacing: 3px;
        }
        li {
            list-style: none;
            cursor: default;
        }
        .container {
            position: absolute;
            top: 30%;
            left:50%;
            transform: translate(-50%, -50%);
            box-shadow: 0 10px 10px rgba(10, 20, 20, .20), 0 0 10px rgba(10, 20, 20, .20);
            font-size: 0;
        }
        .container .nav {
            display: inline-block;
            position: relative;
            width: 150px;
            height: 50px;
            background-color: #505050;
            text-align: center;
            line-height: 50px;
        }
        .container .nav:hover {
            background-color: #3e3e3e;
        }
        .container .nav ul {
            display: none;
            position: absolute;
            top: 50px;
            width: 100%;
            box-shadow: 0 10px 10px rgba(10, 20, 20, .20), 0 0 10px rgba(10, 20, 20, .20);
            background-color: #fff;
            text-align: center;
        }
        .container .nav ul li a {
            display: block;
            color: #505050;
        }
        .container .nav ul li a:hover {
            background-color: #e1e1e1;
        }
    </style>
</head>
<body>
    <ul class="container">
        <li class="nav">
            <a href="#">水果</a>
            <ul>
                <li><a href="#">西瓜</a></li>
                <li><a href="#">香蕉</a></li>
                <li><a href="#">哈密瓜</a></li>
                <li><a href="#">橙子</a></li>
            </ul>
        </li>
        <li class="nav">
            <a href="#">蔬菜</a>
            <ul>
                <li><a href="#">西红柿</a></li>
                <li><a href="#">土豆</a></li>
                <li><a href="#">小白菜</a></li>
                <li><a href="#">黄花菜</a></li>
            </ul>
        </li>
        <li class="nav">
            <a href="#">酒水</a>
            <ul>
                <li><a href="#">啤酒</a></li>
                <li><a href="#">橙汁</a></li>
                <li><a href="#">可乐</a></li>
                <li><a href="#">雪碧</a></li>
            </ul>
        </li>
        <li class="nav">
            <a href="#">零食</a>
            <ul>
                <li><a href="#">饼干</a></li>
                <li><a href="#">面包</a></li>
                <li><a href="#">辣条</a></li>
                <li><a href="#">沙琪玛</a></li>
            </ul>
        </li>
    </ul>
    <script>
        let nav = document.querySelectorAll('.nav');
        for(let i=0; i<nav.length; i++) {
            nav[i].addEventListener('mouseover', function() {
                this.children[1].style.display = "block";
            });
            nav[i].addEventListener('mouseout', function() {
                this.children[1].style.display = "none";
            });
        }
    </script>
</body>
</html>

  • 30
    点赞
  • 345
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
好的,以下是一个基本的搜索框下拉列表的实现步骤: 1. HTML结构:在HTML中创建一个输入框和一个下拉列表的容器,下拉列表的容器初始设置为隐藏状态。 ```html <div class="search-container"> <input type="text" placeholder="请输入搜索关键字"> <div class="search-list-container"> <ul class="search-list"></ul> </div> </div> ``` 2. CSS样式:设置输入框的样式和下拉列表容器的样式,下拉列表容器的初始状态设置为 `display:none` 。 ```css .search-container { position: relative; } input[type="text"] { width: 300px; height: 30px; padding: 5px; border: 1px solid #ccc; border-radius: 5px; } .search-list-container { position: absolute; top: 35px; left: 0; z-index: 99; width: 300px; max-height: 200px; overflow-y: auto; background: #fff; border: 1px solid #ccc; border-radius: 5px; display: none; } ``` 3. JS交互:监听输入框的键盘输入事件,在输入框中输入内容时,向后台发送请求获取匹配的搜索结果,将结果渲染到下拉列表中,并将下拉列表容器设置为显示状态。 ```javascript const input = document.querySelector('input[type="text"]'); const searchListContainer = document.querySelector('.search-list-container'); const searchList = document.querySelector('.search-list'); input.addEventListener('input', function(e) { const keyword = e.target.value; if (keyword.trim()) { // 发送请求获取匹配的搜索结果 const searchResults = getSearchResults(keyword); renderSearchList(searchResults); searchListContainer.style.display = 'block'; } else { searchList.innerHTML = ''; searchListContainer.style.display = 'none'; } }); function getSearchResults(keyword) { // 向后台发送请求获取搜索结果 const results = ['搜索结果1', '搜索结果2', '搜索结果3']; return results; } function renderSearchList(results) { let html = ''; results.forEach(result => { html += `<li>${result}</li>`; }); searchList.innerHTML = html; } ``` 以上就是一个简单的搜索框下拉列表的实现方法,您可以根据自己的需求进行样式和交互的修改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@cooltea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值