jQuery水平滑动切换导航

编程入门 行业动态 更新时间:2024-10-26 09:34:38
本文介绍了jQuery水平滑动切换导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一个简单的嵌套导航菜单,该菜单在悬停时会向左水平滑动或飞出".我认为实现此目标的唯一方法是使用jQuery,因此我一直在尝试使用它.

I'm looking to make a simple nested navigation menu that "slides" or "flys" out horizontally-left on hover. I assume the only way to achieve this is with jQuery so I have been playing around with it.

初始状态

悬停状态

编辑:我知道这张图片看起来有点不对劲.我希望整个ul.sub菜单(即人员和方法")一起滑出,而不是顺次滑出.

I realize this picture looks a bit wrong. I want the entire ul.sub-menu (ie: PEOPLE & APPROACH) to slide out together, not sequentially.

一般结构为:

<nav> <ul> <li>ABOUT <ul class="sub-menu"> <li>PEOPLE</li> <li>APPROACH</li> </ul> </li> <li>PROJECTS</li> <li>CONTACT</li> </ul> </nav>

我的jQuery是:

$("nav li").on('hover', function(){ $(this).children(".sub-menu").animate({width: 'toggle'}); });

目前我的李浮动了.

我的问题是,当我将鼠标悬停在父级LI上时,.sub菜单会淡入,但在一行下方,并且由于浮动而一切都在跳来跳去.

My issue is when I hover over the parent LI, the .sub-menu fades in but is down a line and everything is jumping around due to floats.

有人可以指出我正确的方向吗?

Can someone point me in the right direction?

推荐答案

这是您要干的吗?我相信您在CSS中缺少display:inline-block属性.通过使用内联块,具有该属性的每个元素将保留块特征(宽度,高度等),但也保持内联(在前后没有中断).如果没有块部分,则没有必要进行宽度修改.

Is this what you were going for? You were missing a display:inline-block property from your CSS I believe. By using an inline block, each element with that property will retain block features (width, height, etc) but also remain inline (no breaks before or after). Without the block part, it makes no sense to apply a width modification.

<style type='text/css'> nav li { display: inline; list-style: none; } .sub-menu { display: inline-block; white-space: nowrap; padding: 0; margin: 0; } </style>

Javascript:

<script> $(function() { $('.sub-menu').hide(); $('.link').hover( function() { $('.sub-menu', this).stop().animate({ width: 'toggle', opacity: 'toggle' } /* [, duration in ms] */); } ); }); </script>

HTML:

<nav> <ul> <li class="link">ABOUT <ul class="sub-menu"> <li>PEOPLE</li> <li>APPROACH</li> </ul> </li> <li class="link">PROJECTS <ul class="sub-menu"> <li>ONE</li> <li>TWO</li> </ul> </li> <li>CONTACT</li> </ul> </nav>

更多推荐

jQuery水平滑动切换导航

本文发布于:2023-11-27 20:54:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水平   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!