【问题标题】:How to fix the Second dropdown which is not working?如何修复不起作用的第二个下拉菜单?
【发布时间】:2023-02-18 12:24:17
【问题描述】:

我创建了一个可以正常工作的下拉菜单,但我需要在第一个下拉菜单旁边再添加一个。但是当我在第一个旁边重复相同的 HTML 代码时,只有第一个在工作。所以,我需要一个修复程序,以便它们都能独立工作。我试过但做不到。我在下面发布了 html 和 CSS 代码,请看一下。请帮我解决它。

    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700;900&display=swap');
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }
    
    body {
        font-family: 'Roboto', sans-serif;
        font-weight: 500;
        font-size: 15px;
        line-height: 1.4;
        color: #fff;
        background-color: #1f2029;
        overflow-x: hidden;
        background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/1462889/pat-back.svg');
        background-position: center;
        background-repeat: repeat;
        background-size: 4%;
        width: 100%;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
        -ms-flex-pack: center;
        justify-content: center;
        padding-top: 100px;
        padding-bottom: 300px;
    }
    
    .sec-center {
        position: relative;
        max-width: 100%;
        text-align: center;
        z-index: 200;
    }
    
    .sec-center1 {
        position: relative;
        max-width: 100%;
        text-align: center;
        z-index: 200;
    }
    
    [type="checkbox"]:checked,
    [type="checkbox"]:not(:checked) {
        position: absolute;
        left: -9999px;
        opacity: 0;
        pointer-events: none;
    }
    
    .dropdown:checked+label,
    .dropdown:not(:checked)+label {
        position: relative;
        font-family: calibri, sans-serif;
        font-weight: 500;
        font-size: 14px;
        /* line-height: 2; */
        /* height: 50px; */
        transition: all 200ms linear;
        /* border-radius: 4px; */
        /* width: 220px; */
        letter-spacing: 1px;
        display: -webkit-inline-flex;
        display: -ms-inline-flexbox;
        display: inline-flex;
        -webkit-align-items: center;
        -moz-align-items: center;
        -ms-align-items: center;
        align-items: center;
        -webkit-justify-content: center;
        -moz-justify-content: center;
        -ms-justify-content: center;
        justify-content: center;
        -ms-flex-pack: center;
        text-align: center;
        border: none;
        background-color: #143b71;
        cursor: pointer;
        color: #fff;
        box-shadow: 0 12px 35px 0 rgb(255 235 167 / 15%);
        padding: 4px 20px;
        border-right: 2px solid #fff;
        border-radius: 50px;
    }
    
    .dropdown:checked+label:before,
    .dropdown:not(:checked)+label:before {
        position: fixed;
        top: 0;
        left: 0;
        content: '';
        width: 100%;
        height: 100%;
        z-index: -1;
        cursor: auto;
        pointer-events: none;
    }
    
    .dropdown:checked+label:before {
        pointer-events: auto;
    }
    
    .dropdown:not(:checked)+label .uil {
        content: '';
        position: absolute;
        right: 6px;
        transform: rotate(0deg);
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-top: 4px solid #fff;
    }
    
    .dropdown:checked+label .uil {
        content: '';
        position: absolute;
        right: 6px;
        transform: rotate(180deg);
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-top: 4px solid #fff;
    }
    
    .section-dropdown {
        position: absolute;
        padding: 5px;
        background-color: #143b71;
        top: 41px;
        left: 0;
        width: 100%;
        border-radius: 4px;
        display: block;
        box-shadow: 0 14px 35px 0 rgba(9, 9, 12, 0.4);
        z-index: 2;
        opacity: 0;
        pointer-events: none;
        transform: translateY(20px);
        transition: all 200ms linear;
    }
    
    .dropdown:checked~.section-dropdown {
        opacity: 1;
        pointer-events: auto;
        transform: translateY(0);
    }
    
    .section-dropdown:before {
        position: absolute;
        top: -20px;
        left: 0;
        width: 100%;
        height: 20px;
        content: '';
        display: block;
        z-index: 1;
    }
    
    .section-dropdown:after {
        position: absolute;
        top: -7px;
        left: 30px;
        width: 0;
        height: 0;
        border-left: 8px solid transparent;
        border-right: 8px solid transparent;
        border-bottom: 8px solid #111;
        content: '';
        display: block;
        z-index: 2;
        transition: all 200ms linear;
    }
    
    .section-dropdown a {
        position: relative;
        color: #fff;
        transition: all 200ms linear;
        font-family: calibri, sans-serif;
        font-weight: 500;
        font-size: 14px;
        border-radius: 2px;
        padding: 5px 0;
        padding-left: 20px;
        padding-right: 15px;
        margin: 2px 0;
        text-align: left;
        text-decoration: none;
        display: -ms-flexbox;
        display: flex;
        -webkit-align-items: center;
        -moz-align-items: center;
        -ms-align-items: center;
        align-items: center;
        justify-content: space-between;
        -ms-flex-pack: distribute;
    }
    
    .section-dropdown a:hover {
        color: #102770;
        background-color: #ffeba7;
    }
    
    .section-dropdown a .uil {
        font-size: 22px;
    }
    
    .dropdown-sub:checked+label,
    .dropdown-sub:not(:checked)+label {
        position: relative;
        color: #fff;
        transition: all 200ms linear;
        font-family: calibri, sans-serif;
        font-weight: 500;
        font-size: 14px;
        border-radius: 2px;
        padding: 5px 0;
        padding-left: 20px;
        padding-right: 15px;
        text-align: left;
        text-decoration: none;
        display: -ms-flexbox;
        display: flex;
        -webkit-align-items: center;
        -moz-align-items: center;
        -ms-align-items: center;
        align-items: center;
        justify-content: space-between;
        -ms-flex-pack: distribute;
        cursor: pointer;
    }
    
    .dropdown-sub:checked+label .uil,
    .dropdown-sub:not(:checked)+label .uil {
        font-size: 22px;
    }
    
    .dropdown-sub:not(:checked)+label .uil {
        content: '';
        position: absolute;
        right: 6px;
        transform: rotate(0deg);
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-top: 4px solid #fff;
    }
    
    .dropdown-sub:checked+label .uil {
        content: '';
        position: absolute;
        right: 6px;
        transform: rotate(180deg);
        width: 0;
        height: 0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-top: 4px solid #fff;
    }
    
    .dropdown-sub:checked+label:hover,
    .dropdown-sub:not(:checked)+label:hover {
        color: #102770;
        background-color: #ffeba7;
    }
    
    .section-dropdown-sub {
        position: relative;
        display: block;
        width: 100%;
        pointer-events: none;
        opacity: 0;
        max-height: 0;
        padding-left: 10px;
        padding-right: 3px;
        overflow: hidden;
        transition: all 200ms linear;
    }
    
    .dropdown-sub:checked~.section-dropdown-sub {
        pointer-events: auto;
        opacity: 1;
        max-height: 999px;
    }
    
    .section-dropdown-sub a {
        font-size: 14px;
    }
    
    .section-dropdown-sub a .uil {
        font-size: 20px;
    }
    <div class="sec-center">
    <input class="dropdown" type="checkbox" id="dropdown" name="dropdown" />
    <label class="for-dropdown" for="dropdown">Dropdown Menu <i class="uil uil-arrow-down"></i></label>
    <div class="section-dropdown">
        <a href="#">Inside Dropdown Link 1 <i class="uil uil-arrow-right"></i></a>
        <input class="dropdown-sub" type="checkbox" id="dropdown-sub" name="dropdown-sub" />
        <label class="for-dropdown-sub" for="dropdown-sub">Dropdown Sub <i class="uil uil-plus"></i></label>
        <div class="section-dropdown-sub">
            <a href="#">Dropdown Sub Link <i class="uil uil-arrow-right"></i></a>
            <a href="#">Dropdown Sub Link <i class="uil uil-arrow-right"></i></a>
        </div>
        <a href="#">Inside Dropdown Link 2 <i class="uil uil-arrow-right"></i></a>
        <a href="#">Inside Dropdown Link 3 <i class="uil uil-arrow-right"></i></a>
    </div>
</div>

<div class="sec-center1">
    <input class="dropdown" type="checkbox" id="dropdown" name="dropdown" />
    <label class="for-dropdown" for="dropdown">Dropdown Menu <i class="uil uil-arrow-down"></i></label>
    <div class="section-dropdown">
        <a href="#">Inside Dropdown Link 1 <i class="uil uil-arrow-right"></i></a>
        <input class="dropdown-sub" type="checkbox" id="dropdown-sub" name="dropdown-sub" />
        <label class="for-dropdown-sub" for="dropdown-sub">Dropdown Sub <i class="uil uil-plus"></i></label>
        <div class="section-dropdown-sub">
            <a href="#">Dropdown Sub Link <i class="uil uil-arrow-right"></i></a>
            <a href="#">Dropdown Sub Link <i class="uil uil-arrow-right"></i></a>
        </div>
        <a href="#">Inside Dropdown Link 2 <i class="uil uil-arrow-right"></i></a>
        <a href="#">Inside Dropdown Link 3 <i class="uil uil-arrow-right"></i></a>
    </div>
</div>

【问题讨论】:

  • 我创建了一个正常工作的下拉菜单“ - 我删除了其中一个下拉列表,但它似乎无法正常工作 - 页面是空白的。这是删除了第二个下拉列表的代码:jsfiddle.net/dont_panic/8nrktsv1 我没有调查太多,但删除了 2x opacity: 0;至少让页面上出现一些东西。不透明度 0 表示 100% 透明,即不可见。这是您的真实代码,还是您可以编辑您的问题以向我们展示工作下拉列表?
  • 您还有重复的 ID,例如 id="dropdown"id="dropdown-sub"。这实际上不会影响此处的任何内容 AFAICT,因为您没有在 CSS 中使用 ID,但这是无效的 HTML,ID 在页面上必须是唯一的。
  • 我已经更新了代码,请看一下。

标签: html css


【解决方案1】:

对于第二个下拉列表,您需要更改类名称。您的第一个下拉菜单和第二个下拉菜单具有完全相同的类名,这将执行的操作是您单击第二个下拉菜单,但您首先激活。还要为您创建的每个新类名提供 CSS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-25
    • 2019-08-27
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多