我有一个使用CSS和jQuery创建的下拉菜单,我试图在悬停和点击上进行下拉菜单。
默认情况下,我希望在点击时显示下拉列表,但是在给定班级.dropdown-hover
的情况下,我希望它在悬停时显示。
这是我目前所拥有的JSFiddle: http://jsfiddle.net/dR8hL/1/
现在,无论哪个类应用于下拉列表,它都会在悬停时显示,我不确定如何修复它。
HTML
<div class="dropdown dropdown-hover">Dropdown on hover
<ul class="dropdown-menu">
<li><a href="#">Profile</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Log out</a></li>
</ul>
</div>
<div class="dropdown">Dropdown on click
<ul class="dropdown-menu">
<li><a href="#">Profile</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Log out</a></li>
</ul>
</div>
CSS
.dropdown {
cursor: pointer;
outline: none;
position: relative;
width: auto;
}
.dropdown .dropdown-menu {
background-color: #fff;
border: 1px solid #eee;
border-radius: inherit;
font-weight: inherit;
left: 0;
margin-left: 0px;
opacity: 0;
pointer-events: none;
position: absolute;
right: 0;
text-transform: none;
width: 200px;
z-index: 99999;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-ms-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
transition: all 0.3s ease-in;
}
.dropdown .dropdown-menu a { text-decoration: none; }
.dropdown .dropdown-menu p {
margin: 0;
padding: 10px 15px;
}
.dropdown .dropdown-menu span { line-height: inherit; }
.dropdown ul.dropdown-menu { list-style-type: none; }
.dropdown .dropdown-menu li {
display: block;
padding: 5px 10px;
}
.dropdown .dropdown-menu li:hover { background-color: #f3f8f8; }
.dropdown.dropdown-active .dropdown-menu {
opacity: 1;
pointer-events: auto;
}
的jQuery
function DropDown(el) {
this.dd = el;
this.initEvents();
}
DropDown.prototype = {
initEvents: function () {
var obj = this;
// Determine if dropdown is on hover or on click
if ($(".dropdown").hasClass("dropdown-hover")) {
// Toggle .dropdown-active on hover
$(".dropdown").mouseenter(function (event) {
$(this).addClass("dropdown-active");
event.stopPropagation();
});
$(".dropdown-menu").mouseleave(function () {
$(".dropdown").removeClass("dropdown-active");
});
} else {
// Toggle .dropdown-active on click
obj.dd.on('click', function (event) {
$(this).toggleClass('dropdown-active');
event.stopPropagation();
});
}
}
}
$(function () {
var dd = new DropDown($('.dropdown'));
$(document).click(function () {
// Remove class from all dropdowns
$('.dropdown').removeClass('dropdown-active');
});
});
答案 0 :(得分:3)
你能不能只定位.dropdown-hover类。你不应该需要if语句
$(".dropdown-hover").mouseenter(function (event) {
event.stopPropagation();
$(this).addClass("dropdown-active");
});
$(".dropdown-hover").mouseleave(function () {
$(".dropdown").removeClass("dropdown-active");
});
// Toggle .dropdown-active on click
obj.dd.on('click', function (event) {
event.stopPropagation();
$(this).toggleClass('dropdown-active');
});
更新了JSFiddle http://jsfiddle.net/47mSS/
答案 1 :(得分:1)
您可以通过简单的CSS&amp; amp;一点点JS -
工作演示 - http://codepen.io/nitishdhar/pen/rpqct
要使用的HTML代码段
<ul class="drop">
<li>Dropdown on Hover
<ul>
<li>Profile</li>
<li>Settings</li>
<li>Log out</li>
</ul>
</li>
</ul>
<ul class="drop-on-click">
<li>Dropdown on Click
<ul>
<li>Profile</li>
<li>Settings</li>
<li>Log out</li>
</ul>
</li>
</ul>
您需要将类“drop”分配给应在悬停时打开的菜单。此外,只需将“点按下载”类分配给您要在点击时打开的那个。
重要的CSS
ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
display: none;
opacity: 0;
visibility: hidden;
}
ul.drop li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
演示链接中有更多的CSS,但主要处理UI方面。
现在处理点击方案的JS -
$(document).ready(function() {
$('.drop-on-click').click(function(){
$(this).toggleClass('drop');
});
$('.drop-on-click ul li').hover(
function() {
$('.drop-on-click').addClass('drop');
}, function() {
$('.drop-on-click').removeClass('drop');
}
);
});
所以基本上JS正在添加已处理的类'drop',它将按照编写的CSS为菜单提供属性下拉列表。现在当我们移动下拉菜单时,我们需要保留drop类,直到我们超过li元素,一旦我们移出,我们删除了类。