gpt4 book ai didi

javascript - 在悬停和单击时创建下拉导航

转载 作者:行者123 更新时间:2023-11-30 10:15:40 26 4
gpt4 key购买 nike

我有一个使用 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');
});
});

最佳答案

你能不能只针对 .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/

关于javascript - 在悬停和单击时创建下拉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com