【问题标题】:Drop down menu won't work下拉菜单不起作用
【发布时间】:2017-05-25 03:41:39
【问题描述】:

我正在创建一个菜单,其中包含两个项目“新功能”和“类别”,当用户单击类别时,应该有大量的子类别下拉。但是我不知道如何让它们下降。有没有人有任何解决方案?这是小提琴 - https://jsfiddle.net/v3ftk7t0/

<div class='shop-sidebar'>
    <ul class='shop-nav'>
        <li class="active"><a href="#">What's New</a></li>
        <li class='w-sub' data-id='shop-categories'>
            <svg class='s_arrow_down'><use xlink:href="#s_arrow_down"></use></svg>
            Categories
            <ul>
                <li>
                    <a href="#">All</a>
                </li>
                <li>
                    <a href="#" class="active">Category 1</a>
                    <ul>
                        <li>
                            <a href="#">All</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 1</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 2</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 3</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 4</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 5</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 6</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 7</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 8</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 9</a>
                        </li>
                    </ul>

                </li>
                <li>
                    <a href="#" class="active">Category 2</a>
                    <ul>
                        <li>
                            <a href="#">All</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 1</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 2</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#" class="active">Category 3</a>
                    <ul>
                        <li>
                            <a href="#">All</a>
                        </li>
                        <li>
                            <a href="#">Sub Category 1</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

css

.shop-sidebar {
    width: 30%;
    width: calc(295px);
    display: inline-block;
    padding-right: 65px;
    vertical-align: top;
    font-family: 'maison',sans-serif;
    font-weight: 600;
    font-size: 11px;
    color: #000;
    letter-spacing: 1.5px;
    line-height: 18px;
    text-transform: uppercase;
}
ul.shop-nav {
    list-style: none;
    padding: 0;
    margin: 0;
}
ul.shop-nav li.active, ul.shop-nav li:hover {
    color: #000;
    opacity: 1;
    font-weight: bold;
}
ul.shop-nav li {
    transition: all 0.3s;
    cursor: pointer;
    padding: 18px 20px;
    background-color: #f8f8f8;
    margin-bottom: 2px;
}
ul.shop-nav li.active a {
    color: #000;
}
ul.shop-nav a {
    color: #000;
}
ul.shop-nav li.active, ul.shop-nav li:hover {
    color: #000;
    opacity: 1;
    font-weight: bold;
}
ul.shop-nav li svg {
    width: 10px;
    height: 10px;
    vertical-align: text-bottom;
    fill: #000;
    transition: all 0.3s;
    float: right;
}
ul.shop-nav li ul {
    display: none;
    list-style: none;
    padding-left: 0;
    margin: 12px 0 0;
}
ul.shop-nav li ul li {
    color: #000;
    border: 0 !important;
    font-family: 'maison',sans-serif;
    font-size: 12px;
    letter-spacing: 0;
    padding: 0;
    font-weight: normal;
    text-transform: none;
    margin-bottom: 12px;
}
ul.shop-nav li ul ul {
    margin-left: 16px;
}

【问题讨论】:

    标签: javascript html css list drop-down-menu


    【解决方案1】:

    这里你有办法,我已经删除了所有我不需要的,你将能够再次添加它。只需点击类别即可享受!!!

    .shop-sidebar {
    	width: 30%;
    	width: calc(295px);
    	display: inline-block;
    	padding-right: 65px;
    	vertical-align: top;
    	font-family: 'maison',sans-serif;
    	font-weight: 600;
    	font-size: 11px;
    	color: #000;
    	letter-spacing: 1.5px;
    	line-height: 18px;
    	text-transform: uppercase;
    }
    ul.shop-nav {
    	list-style: none;
    	padding: 0;
    	margin: 0;
    }
    ul.shop-nav li.active, ul.shop-nav li:hover {
    	color: #000;
    	opacity: 1;
    	font-weight: bold;
    }
    ul.shop-nav li {
    	transition: all 0.3s;
    	cursor: pointer;
    	padding: 18px 20px;
    	background-color: #f8f8f8;
    	margin-bottom: 2px;
    }
    ul.shop-nav li.active a {
    	color: #000;
    }
    ul.shop-nav a {
    	color: #000;
    }
    ul.shop-nav li.active, ul.shop-nav li:hover {
    	color: #000;
    	opacity: 1;
    	font-weight: bold;
    }
    ul.shop-nav li svg {
    	width: 10px;
    	height: 10px;
    	vertical-align: text-bottom;
    	fill: #000;
    	transition: all 0.3s;
    	float: right;
    }
    ul.shop-nav li ul {
    	display: none;
    	list-style: none;
    	padding-left: 0;
    	margin: 12px 0 0;
    }
    ul.shop-nav li ul li {
    	color: #000;
    	border: 0 !important;
    	font-family: 'maison',sans-serif;
    	font-size: 12px;
    	letter-spacing: 0;
    	padding: 0;
    	font-weight: normal;
    	text-transform: none;
    	margin-bottom: 12px;
    }
    ul.shop-nav li ul ul {
    	margin-left: 16px;
    }
    
    input[type='checkbox'] {
      display: none;
    }
    
    #subList, #subListCategory1 {
      display: none;
    }
    
    #categories:checked ~ #subList {
      display: block;
    }
    
    #category1:checked ~ #subListCategory1 {
      display: block;
    }
    <div class='shop-sidebar'>
      <ul class='shop-nav'>
    	<li class="active"><a href="#">What's New</a></li>
    	<li class='w-sub' data-id='shop-categories'>
    	  <svg class='s_arrow_down'><use xlink:href="#s_arrow_down"></use></svg>
    	  <input type="checkbox" id="categories" />
    	  <label id="label" for="categories">Categories</label>
    	  <ul id="subList">
    		<li>
    		  <input type="checkbox" id="all" />
    		  <label id="allLabel" for="all">All</label>
    		</li>
    		<input type="checkbox" id="category1" />
    		<label id="category1Label" for="category1">Category 1</label>
    		<ul id="subListCategory1">
    		  <li>
    			<a href="#">All</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 1							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 2							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 3							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 4							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 5							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 6							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 7							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 8							</a>
    		  </li>
    		  <li>
    			<a href="#">						Sub Category 9							</a>
    		  </li>
    		</ul>
    		<li>
    		  <a href="#" class="active">
    							Category 1					</a>
    
    
    		</li>
    		<li>
    		  <a href="#" class="active">
    							Category 2					</a>
    
    		  <ul>
    
    
    			<li>
    			  <a href="#">
    								All
    							</a>
    			</li>
    
    			<li>
    			  <a href="#">						Sub Category 1							</a>
    			</li>
    			<li>
    			  <a href="#">						Sub Category 2							</a>
    			</li>
    		  </ul>
    
    		</li>
    		<li>
    		  <a href="#" class="active">
    							Category 3					</a>
    
    		  <ul>
    
    
    			<li>
    			  <a href="#">
    								All
    							</a>
    			</li>
    
    			<li>
    			  <a href="#">						Sub Category 1							</a>
    			</li>
    		  </ul>
    
    		</li>
    
    	  </ul>
    	</li>
    
      </ul>
    </div>

    编辑:再次添加 css 类。

    Here 完整的小提琴。

    【讨论】:

    • 不幸的是,这段代码不起作用。 “类别”不可点击。
    • 您点击了类别吗?也许你想要点击整个列表元素,不是吗?告诉我,我会重写它
    • 我最初检查时一定有错误。当我单击类别时,它现在可以拉下子菜单。但是现在,当您单击它们时,其中的类别不会下拉它们的子类别。
    • 当然,你必须对所有这些都应用相同的技术,基本上对所有应该切换的子列表元素使用一个复选框和一个标签。!!!
    • 我添加了一个带有一个子菜单的示例,单击类别,然后在类别 1 中。让我知道它是否是您要查找的内容。谢谢
    【解决方案2】:

    添加这个css:

    ul.shop-nav li:hover ul {
        display: block;
    }
    

    这将执行以下操作:

    当您将鼠标悬停在 ul.shop-nav li 元素上时,您会将嵌套在其中的 ul 设置为显示。

    【讨论】:

    • 谢谢!但是,菜单会在悬停时显示,而不是在单击时显示。我希望它仅在您单击类别时才显示在子菜单中。
    • 我的错,对于点击事件,您应该使用 Javascript 或 jQuery。这是一个关于如何做到这一点的链接:stackoverflow.com/questions/21111946/…如果您需要这方面的帮助,请告诉我,我很乐意提供帮助。
    • 在你的类“w-sub”所在的位置添加一个id,然后添加这个jQeury代码:&lt;script&gt; $( "#idHere" ).click(function() { $('#idHere ul').css('display', 'block'); }); &lt;/script&gt;
    • 请使用edit链接解释这段代码是如何工作的,不要只给出代码,因为解释更有可能帮助未来的读者。另见How to Answer。 source
    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    相关资源
    最近更新 更多