html实现菜单点击小三角睁开和收回

[复制链接]
发表于 2025-10-20 20:42:39 | 显示全部楼层 |阅读模式
1. 代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>带小三角的参数设置菜单</title>
  6.     <style>
  7.         /* 容器定位‌:ml-citation{ref="4,7" data="citationList"} */
  8.         .dropdown {
  9.             position: relative;
  10.             display: inline-block;
  11.             margin: 20px;
  12.         }
  13.         /* 按钮基础样式‌:ml-citation{ref="4,7" data="citationList"} */
  14.         .dropbtn {
  15.             background: #4CAF50;
  16.             color: white;
  17.             padding: 10px 35px 10px 15px;
  18.             border: none;
  19.             border-radius: 4px;
  20.             cursor: pointer;
  21.             position: relative;
  22.             transition: all 0.3s;
  23.         }
  24.         /* CSS绘制小三角‌:ml-citation{ref="1,4" data="citationList"} */
  25.         .triangle {
  26.             position: absolute;
  27.             right: 12px;
  28.             top: 50%;
  29.             transform: translateY(-50%);
  30.             width: 0;
  31.             height: 0;
  32.             border-left: 5px solid transparent;
  33.             border-right: 5px solid transparent;
  34.             border-top: 6px solid white;
  35.             transition: transform 0.3s;
  36.         }
  37.         /* 下拉菜单容器‌:ml-citation{ref="4,7" data="citationList"} */
  38.         .dropdown-content {
  39.             display: none;
  40.             position: absolute;
  41.             min-width: 160px;
  42.             background: #fff;
  43.             box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  44.             margin-top: 5px;
  45.             border-radius: 4px;
  46.             opacity: 0;
  47.             transition: opacity 0.3s;
  48.         }
  49.         /* 展开状态‌:ml-citation{ref="2,4" data="citationList"} */
  50.         .active .dropdown-content {
  51.             display: block;
  52.             opacity: 1;
  53.         }
  54.         .active .triangle {
  55.             transform: translateY(-50%) rotate(180deg);
  56.         }
  57.         /* 菜单项交互‌:ml-citation{ref="4,7" data="citationList"} */
  58.         .dropdown-content a {
  59.             display: block;
  60.             padding: 10px 15px;
  61.             color: #333;
  62.             text-decoration: none;
  63.             transition: background 0.2s;
  64.         }
  65.         .dropdown-content a:hover {
  66.             background: #f8f9fa;
  67.         }
  68.     </style>
  69. </head>
  70. <body>
  71.     <div class="dropdown">
  72.         <button class="dropbtn" onclick="toggleMenu()" aria-expanded="false">
  73.             参数设置
  74.             <i class="triangle"></i>
  75.         </button>
  76.         <div class="dropdown-content">
  77.             <a href="#" tabindex="0">显示设置</a>
  78.             <a href="#" tabindex="0">声音设置</a>
  79.             <a href="#" tabindex="0">隐私选项</a>
  80.         </div>
  81.     </div>
  82.     <script>
  83.         // 切换菜单状态‌:ml-citation{ref="2,4" data="citationList"}
  84.         function toggleMenu() {
  85.             const dropdown = document.querySelector('.dropdown');
  86.             const isActive = dropdown.classList.toggle('active');
  87.             dropdown.querySelector('.dropbtn').setAttribute('aria-expanded', isActive);
  88.         }
  89.         // 点击外部关闭‌:ml-citation{ref="2,4" data="citationList"}
  90.         document.addEventListener('click', (e) => {
  91.             if (!e.target.closest('.dropdown')) {
  92.                 document.querySelector('.dropdown').classList.remove('active');
  93.             }
  94.         });
  95.         // 键盘导航支持‌:ml-citation{ref="4,7" data="citationList"}
  96.         document.addEventListener('keydown', (e) => {
  97.             if (e.key === 'Escape') {
  98.                 document.querySelector('.dropdown').classList.remove('active');
  99.             }
  100.         });
  101.     </script>
  102. </body>
  103. </html>
复制代码
2. 效果




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表