gpt4 book ai didi

javascript - 自定义下拉菜单不适用于链接

转载 作者:行者123 更新时间:2023-11-28 00:10:21 25 4
gpt4 key购买 nike

我正在尝试使用我找到的教程制作自定义菜单。

我遇到的问题不是实现,而是阻止我点击链接的问题。当我“打开”菜单时,当我点击一个链接时,它只是关闭菜单,很快我看到它突出显示了整个框而不是链接。

这里是一些截图。您可以将鼠标悬停在链接上(悬停时它们会变成紫色),但是当您单击单个链接时,它往往只是单击整个菜单而不是将您带到所述页面。但是当我将鼠标悬停在它上面时,我可以看到它在我浏览器的左下角显示了链接。我似乎无法弄清楚点击而不是将您发送到页面的网址会发生什么。

http://cl.ly/image/0S2F2c3h3g1o(悬停屏幕截图)

http://f.cl.ly/items/3P1h27323B203E2J0N41/clickout.png(点击)

当我尝试点击任何链接时,我只会看到这个蓝色框,它会关闭菜单而不是将您带到链接页面。

这是我连接的 HTML

   <!-- MOBILE MENU / -->
<nav id="mobile-menu">
<div id="mobmenu" class="dropdown-menu" tabindex="1">
<ul class="dropdown" tabindex="1">
<li><a href="/about">ABOUT</a></li>
<li><a href="/work">WORK</a></li>
<li><a href="/notes">NOTES</a></li>
</ul>
</div>
</nav>
<!-- / MOBILE MENU -->

这是 Javascript。

        function DropDown(el) {
this.dd = el;
this.opts = this.dd.find('ul.dropdown > li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;

obj.dd.on('click', function(event){
$(this).toggleClass('active');
return false;
});

obj.opts.on('click',function(){
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
}

$(function() {

var dd = new DropDown( $('#mobmenu') );

$(document).click(function() {
// all dropdowns
$('.dropdown-menu').removeClass('active');
});

});

</script>

还有 CSS。

#mobile-menu {
width: 320px;
}
.dropdown-menu {
background: #050607 url(images/menu_closed.png) no-repeat center top;
border: none;
color: #FFF;
cursor: pointer;
height: 50px;
font-weight: bold;
margin-top: 17px;
outline: none;
position: fixed;
-webkit-appearance: none;
width: 320px;
z-index: 9999;
}
.dropdown-menu::after {
content: "";
width: 0;
height: 0;
position: absolute;
right: 16px;
top: 50%;
margin-top: -6px;
}
/* ACTIVE STATE */
.dropdown-menu .dropdown {
background: #050607;
height: 1000px;
list-style: none;
overflow: hidden;
pointer-events: none;
position: absolute;
top: 100%;
left: 0;
right: 0;
opacity: 0.0;
}
.dropdown-menu .dropdown li {
position: relative;
top: 10px;
z-index: 99;
}
.dropdown-menu .dropdown li a {
border-top: 1px solid #1f1f1f;
color: #FFF;
display: block;
font-family: 'Open Sans', sans-serif;
font-size: 30px;
font-weight: 200;
letter-spacing: 8px;
pointer-events: visible;
padding: 15px 25px 15px 25px;
text-align: center;
text-decoration: none;
z-index: 9999999999999999999999;
}
.dropdown-menu .dropdown li:nth-child(5) {
font-size: 15px;
font-weight: 100;
letter-spacing: 2px;
text-align: center;
}
.dropdown-menu .dropdown li:hover a { background: #050607; color: #605e90; }
.dropdown-menu.active .dropdown {
-moz-animation: fadein .3s linear;
-webkit-animation: fadein .3s linear;
-ms-animation: fadein .3s linear;
animation: fadein .3s linear;
pointer-events: auto;
opacity: 1.0;
z-index: 999999 !important;
}
.dropdown-menu.active:active {
pointer-events: auto;
}
.dropdown-menu.active::after {
border-color: #000;
margin-top: -3px;
}
.dropdown-menu.active {
background: #050607 url(images/menu_open.png) no-repeat center top;
outline: none;
}

谢谢! :3

最佳答案

obj.opts 单击事件处理程序触发后,obj.dd 单击事件处理程序也将触发。为防止这种情况,请调用传递给处理程序的事件对象的 stopPropagation 函数。

有关工作示例,请参见此处:http://jsfiddle.net/hwTUZ/1/

关于javascript - 自定义下拉菜单不适用于链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15595745/

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