【问题标题】:Custom Dropdown menu not working with links自定义下拉菜单不适用于链接
【发布时间】:2013-03-24 14:40:06
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript html css drop-down-menu


    【解决方案1】:

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

    查看这里的工作示例:http://jsfiddle.net/hwTUZ/1/

    【讨论】:

    • 甜蜜!谢谢!我知道它必须是 JS 中的东西。我只是想不通。我一直在修改返回 false,并执行 preventDefault,但没有任何效果!非常感谢您花时间解决这个问题:)
    • 嘿,你不知道如何隐藏菜单链接?嗯,即使菜单没有展开也可以点击。
    • 您可以通过修改下拉类的 CSS 使其默认隐藏(“display: none;”),然后在用户单击 div 时切换可见性来实现此目的。此外,如果您只是想要一个链接的下拉列表,则某些 javascript 代码是多余的。这是去除了多余 javascript 的固定版本:jsfiddle.net/7YS3W/2
    猜你喜欢
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2017-11-11
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多