【问题标题】:though jQuery .toggle() dropdown (less than 991px width)虽然 jQuery .toggle() 下拉(小于 991px 宽度)
【发布时间】:2015-09-09 17:29:44
【问题描述】:

我正在尝试切换小屏幕的菜单/子菜单。 (max-width: 991px) When you click the "product" link, the .dropdown menu should show, and when the "accessories" link is selected, the .subdropdown class should show.轻按菜单上的任意位置以在任意深度释放它们会很不错。

注意:在屏幕min-width: 991px(桌面)上,:hover 将发挥作用。

注意我的 JavaScript 代码不会触发子“accessories”菜单。如果调整窗口大小以进行测试,则必须刷新。

这是一个实时示例:Click Here

这是我的js代码

$(function() {

    if ($(window).width() < 991) {

        $('#product-link').click(function() {
            $('.dropdown').toggle();
        });

        if ('.dropdown' === true) {
            $('#accessories-link').click(function() {
                $('.subdropdown').show();
            });
        } else {
            $('.dropdown').hide();
        }
    }

});

编辑: 虽然不完美,但它确实有效。我仍然想使用 .click 而不是 .hover ,但这可以完成工作。

$(document).ready(function() { $('.top-menu').hover( function(){ $(this).children('.sub-menu').fadeIn(200); }, function(){ $(this).children('.sub-menu').fadeOut(200); } ); })

【问题讨论】:

    标签: javascript jquery drop-down-menu menu submenu


    【解决方案1】:

    您的 if 语句将始终为 false。

    第 9 行:if ('.dropdown' === true)

    【讨论】:

      【解决方案2】:

      在 JQuery 中,您正在为特定元素设置事件侦听器,但是您当前的设置没有利用产品链接的 on 方法。尝试将您的代码切换到类似

      $(function() {
          $('#product-link').on('click mouseover', function(){
              $('.dropdown').toggle();
              if($('#product-link').hasClass('.dropdown')){
                  //something else here 
              }
          });
      });
      

      【讨论】:

      • 谢谢!我尝试了代码,但这并不是我想要的。我希望这是 991 及以下屏幕的移动优先方法。没有鼠标悬停。我希望我的代码仅在屏幕尺寸小于 991 像素时运行。 if ($(window).width() &lt; 991) 另外...我不知道如何在单击附件时触发 .subdropdown 类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2014-04-03
      相关资源
      最近更新 更多