【问题标题】:Changing hover to click for header navigation flyout menus将悬停更改为单击标题导航弹出菜单
【发布时间】:2016-09-14 21:59:48
【问题描述】:
  • Given:带有导航元素的简单标题。
  • 何时:用户将鼠标悬停在标题导航元素上并出现一个迷你浮出控件
  • 然后:用户必须单击元素才能显示浮出控件

下面的代码非常适合悬停,但我希望在单击而不是悬停时出现迷你弹出窗口。当我将 .hover 更改为 .click 时,没有任何反应。

有什么想法吗?

jQuery

jQuery(document).ready(function () {
function displayFlyout(main_id, content_id) {
    jQuery(main_id).hover(function () {
            jQuery(content_id).stop(true, true).slideDown('fast');
            jQuery(this).addClass("fly-dropdown");
        },
        function () {
            jQuery(content_id).stop(true, true).slideUp(200);
            jQuery(this).removeClass("fly-dropdown");
        }
    );
}

displayFlyout("#example_one", "#example_one_content");
displayFlyout("#example_two", "#example_two_content");
displayFlyout("#example_three", "#example_three_content");
});

Html(只是浮出控件的一个示例)

<ul class="header_flyout col-md-10 pull-right">
        <li class="contact_us">
            <div id="example_one" class="no-dropdown">
                <span class="menu"><?php echo $this->__('Example'); ?></span>
                <div class="header-dropdown-content" id="example_one_content">
                    <span class="blue-arrow">&nbsp;</span>
                    <?php echo $this->getChildHtml('contact_mini'); ?>
                </div>
            </div>
        </li>
        <li>.....</li> (example two)
        <li>.....</li> (example three)
    </ul>

【问题讨论】:

    标签: javascript jquery hover click


    【解决方案1】:

    问题是.hover()方法接受两个参数from the docs

    .hover( handlerIn, handlerOut )
    

    另一方面,.click() 函数只接受一个参数,你可以做的是监听文档上的click 事件,如果它达到你想要的目标,你就激活下拉菜单,否则你会隐藏下拉菜单,如下所示:

    jQuery(document).ready(function () {
        function displayFlyout(main_id, content_id) {
        jQuery(document).click(function (ev) {      
          if (jQuery(ev.target).parent(main_id).is(jQuery(main_id))) {
            jQuery(content_id).stop(true, true).slideDown('fast');
            jQuery(this).addClass("fly-dropdown");      
          } else {
            jQuery(content_id).stop(true, true).slideUp(200);
            jQuery(this).removeClass("fly-dropdown");
          }
       });
        }
    
        displayFlyout("#example_one", "#example_one_content");
    });
    

    您也可以在jsFiddle 上查看。

    希望对你有帮助, 干杯。

    编辑: 请记住,使用此解决方案意味着点击的目标将是 &lt;span class="menu"&gt;Example&lt;/span&gt;,这就是我使用 .parent() 函数向上遍历 dom 的原因找到您要绑定的元素。

    【讨论】:

    • 哇,谢谢@Lucas Lazaro,这很好用。我是否需要在 else 语句中添加更多内容以便能够单击任意位置以关闭弹出窗口,或者这会有所不同吗?也许是一些布尔逻辑?目前,您必须单击 content_id 才能关闭弹出窗口。谢谢
    • @HawortDe 不用担心!嗯...在我的 Fiddle 上似乎工作正常,单击画布上的任意位置都会关闭弹出窗口,您在哪个浏览器中预览?
    • 我在本地 Magento 环境中使用 Chrome。您的小提琴正是我想要的,但是,我的弹出窗口的反应完全相反。现在测试任何冲突。
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多