【问题标题】:Push content from absolute menu with Jquery使用 Jquery 从绝对菜单推送内容
【发布时间】:2016-12-09 09:09:47
【问题描述】:

您好,我需要制作一个 3 级菜单。

当您将鼠标悬停时,第一级子菜单显示在 1 级内联块下方。稍后我将使用 flex 使其具有响应性。

那么就和第三层一样。

这就是它应该的样子:

enter image description here

所以现在我差不多好了,但我可以弄清楚为什么我的菜单出错了。

你可以在这里找到 jsfiddle:

https://jsfiddle.net/z1cepma4/

知道如何解决这个问题吗?

提前感谢您的帮助


      var $level1 = $('#nav li');
		  var $level2 = $('#nav li ul');
		  var $level3 = $('#nav li ul li ul');
		  var $pagecontent = $('#fakecontent');
		  var hoverTimeout = '';
      var leaveTimeout = '';
    
    
    
		  $level1.mouseenter(function() {
		  	
		  
		  var $thislevel2 = $(this).children('ul').first();
		  	
		  	console.log('hover level 1 val : ' + $(this).text());
		  	
		  	
		  	clearTimeout(leaveTimeout);
	        // 1.
	        hoverTimeout = setTimeout(function() {
	          // 2. Another submenu open
	          if( $thislevel2.is(':visible') ) {
	          console.log('level 2 visible');
	            // if new hovered li has megamenu, hide old menu and show the new, otherwise slide everything back up
	            if( $thislevel2.length ) {
	              // stop any other hoverTimeouts triggered through guick back-and-forth hovering
	              clearTimeout(hoverTimeout); 
	              $thislevel2.filter(':visible').stop(true, true).hide();
	              $thislevel2.stop(true, true).show();
	              
	              console.log('level 2 - parti 1');
	            } else {
	              $thislevel2.filter(':visible').stop(true, true).slideUp(500);
	              $pagecontent.stop(true, true).animate({ paddingTop: '0'}, 500);
	                 console.log('level 2 - parti 2');
	            }
	          } else {
		          
	            console.log('level 2 not visible');
	            if( $thislevel2.length ) {
		          
		          
	              // stop any other hoverTimeouts triggered through guick back-and-forth hovering
	              clearTimeout(hoverTimeout); 
	              $thislevel2.stop(true, true).show();
	              
	           
	              /* 16.5em is the set height of the megamenu + negative margin of nav ul */
	              $pagecontent.stop(true, true).animate({ marginTop: '10em'}, 500);
	            }
	          }
	        }, 200);
    	}); //$level1 mouseenter
	      
	     $level1.mouseleave(function() {
	      clearTimeout(hoverTimeout);
	      leaveTimeout = setTimeout(function() {
	        if( $level2.is(':visible') ) {
	          $level2.filter(':visible').stop(true, true).slideUp(500);
	          $pagecontent.stop(true, true).animate({ marginTop: '0'}, 500);
	        }
	      }, 200);
	    });
   #fakeheader,
#fakecontent{
  width:100%;
   height:150px;
  background:red;
  position:relative
  }
#nav {position: relative;}
#nav li {
    list-style:none;
    float: left;
}
#nav li a {
    display: block;
    padding: 8px 12px;
    text-decoration: none;
}
#nav li a:hover {
  color:red;
	color:#FFF;
	opacity:1;
}

/* Targeting the first level menu */
#nav {  
   
    min-width:850px;
    background:#fff;
    display: block;
    height: 34px;
    z-index: 100;
    position: relative;
    left: 0;
    margin: 0;
    padding: 0;
    width:100%;
}
#nav > li > a {
  color:#000;
}


/* Targeting the second level menu */
#nav li ul {
    color: #333;
    display: none;
    position: absolute; 
    left: 0;
    margin: 0;
    padding: 0;
    width:100%;
}
#nav li ul li {
    display: inline;
}
#nav li ul li a {
    background: #fff;
    border: none;
    line-height: 34px;
    margin: 0;
    padding: 0 8px 0 10px;
}
#nav li ul li a:hover {
    color:red;
    opacity:1;
}

#nav li ul li:hover>a{color:red;}

/* Third level menu */
#nav li ul li ul{
    top: 0;
    left: 0;
    margin: 0;
    padding: 0;
    width:100%;
}
ul.child {
background-color:#FFF;  
}
/* A class of current will be added via jQuery */
#nav li.current > a {
    color:red;
    float:left;
}

/*
#nav li:hover > ul.child {
    left:0;
    top:34px;
    display:inline;
    position:absolute;
    text-align:left;
}
#nav li:hover > ul.grandchild  {
    display:block;
}*/
#nav > li:hover > a{color:red;}
#nav > li > a > li:hover > a{color:red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fakeheader">Header
</div>
<div id="#menu-categories">

<ul id="nav">
    <li>
        <a href="#">Quoi manger/Ou dormir...</a>
        <ul class="child">
            <li><a href="#">Associations</a>
                <ul class="child">
                    <li><a href="#">Associations 3</a></li>
                    <li><a href="#">Associations 3</a></li>
                    <li><a href="#">Associations 3</a></li>
                </ul>
            </li>
            <li><a href="#">Autre</a>
                <ul class="child">
                    <li><a href="#">Autre 3</a></li>
                    <li><a href="#">Autre 3</a></li>
                    <li><a href="#">Autre 3</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li>
        <a href="#">Quoi faire...</a>
        <ul class="child">
            <li><a href="#">Associations</a>
                <ul class="child">
                    <li><a href="#">Culturelle</a></li>
                    <li><a href="#">Culturelle</a></li>
                    <li><a href="#">Culturelle</a></li>
                </ul>
            </li>
            <li><a href="#">Associations</a>
                <ul class="child">
                    <li><a href="#">Culturelle</a></li>
                    <li><a href="#">Culturelle</a></li>
                    <li><a href="#">Culturelle</a></li>
                </ul>
            </li>
        </ul>
    </li>
   
</ul>
</div>
<div id="fakecontent">
Content
</div>

【问题讨论】:

    标签: jquery css menu submenu absolute


    【解决方案1】:

    结合使用切换和动画:

    $('#nav').addClass('active');
    
    $('#nav li').hover(function() {
      $(this).find('> ul').toggleClass('active');
      var visible = $('#menu-categories').find('ul.active').length;
      $('#menu-categories').animate({
        'height': visible * 35
      }, {
        queue: false
      });
      $(this).find('> ul').slideToggle();
    
    })
    

    演示: https://jsfiddle.net/rw6L4nqq/1/

    【讨论】:

    • 您好,谢谢,但它不起作用,必须使用 jquery 来推送内容。这是唯一的方法,因为它是绝对定位。
    • 谢谢,如果我切换到不同的类别,我只想使用显示和隐藏,并且如果我离开菜单 slideUp,这几乎是我想要的动画来打开子菜单吗?非常感谢您的帮助
    • 可能,但需要更多编码,重构 html 以删除绝对位置会容易得多
    • 你需要将你的 ul 移到任何其他 ul 之外
    • 是的,但它不适用于响应式菜单我需要它在移动设备上时是父级
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多