JQuery 水平手风琴 CSS
我正在尝试在 Jquery 中构建一个简单的多级 UL 水平手风琴(或滑动菜单)。 Hunter Daley 慷慨地提供了 Jquery 代码,但我似乎无法弄清楚 css。 我知道这是新手,但我真的被困住了。
当 Ul li Ul 滑出时,它会产生换行符,我希望所有内容都内联显示,没有中断。 我已经尝试过空白:nowrap、显示内联等它似乎不会这样做。 有任何想法吗?
根据 Glavic 的回答:我试图使用浮动,但如果我在动画期间执行 Safari 错误并闪烁子级别 UL:
使用浮动:
是的,我正在尝试这样做没有浮动。 我正在尝试坚持使用动画功能。
Safari 在动画期间出现错误并闪烁 sub ul。
<style type="text/css">
<!--
body {
font-size: 1em;
line-height: 1em;
}
ul {
background-color: yellow;
list-style: none;
margin: 0;
padding: 0;
height: 1em;
float: left;
}
ul li {
background-color: aqua;
float: left;
}
ul li ul {
background-color: blue;
}
ul li ul li {
background-color: green;
}
a, a:link, a:hover, a:visited, a:active {
color: black;
text-decoration: none;
float: left;
}
-->
</style>
原帖:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<title>untitled</title>
<style type="text/css">
<!--
ul{
list-style: none;
background-color: yellow;
margin: 0;
padding: 0;
white-space: nowrap;
}
ul li {
background-color: aqua;
display: inline;
}
ul li ul {
background-color: blue;
}
ul li ul li {
background-color: green;
}
a, a:link, a:hover, a:visited, a:active {
color: black;
text-decoration: none;
}
-->
</style>
<script type="text/javascript">
var $current = null;
$(document).ready(function(){
$("ul li ul").hide(); // hide submenus by default on load
$("ul li a").click(function(){
var $sub = $(this).next();
if ($sub.css("display") == "none")
{
if ($current != null)
$current.animate({ width: 'hide' }); // if you want to only show one sub at a time
$sub.animate({ width: 'show' });
$current = $sub;
}
else
{
$sub.animate({ width: 'hide' });
$current = null;
}
});
});
</script>
</head>
<body>
<ul>
<li>
<a href="#">Top-level 1</a>
</li>
<li>
<a href="#">Top-level 2</a>
<ul>
<li><a href="#">Bottom Level A1</a></li>
<li><a href="#">Bottom Level A2</a></li>
<li><a href="#">Bottom Level A3</a></li>
<li><a href="#">Bottom Level A4</a></li>
</ul>
</li>
<li>
<a href="#">Top-level 3</a>
<ul>
<li><a href="#">Bottom Level B1</a></li>
<li><a href="#">Bottom Level B2</a></li>
</ul>
</li>
<li>
<a href="#">Top-level 4</a>
</li>
</ul>
</body>
</html>
I'm trying to build a simple multi-level UL Horizontal Accordion (or slide menu) in Jquery. Hunter Daley graciously provided the Jquery code, but I can't seem to figure out the css. I know this is newb, but I'm really stuck.
When Ul li Ul slides out it creates a linebreak, I'd like to have everything display inline, with no breaks. I've tried whitespace: nowrap, display inline etc It just won't seem to do it. Any ideas?
As per, Glavic's answer: I was trying to to use floats, but if I do Safari bugs out and flash the sub-level UL during the animation:
Using Floats:
Right, I was trying to do it without floats.
I'm trying to stick with the animation function.
Safari bugs out and flashes the sub ul during the animation.
<style type="text/css">
<!--
body {
font-size: 1em;
line-height: 1em;
}
ul {
background-color: yellow;
list-style: none;
margin: 0;
padding: 0;
height: 1em;
float: left;
}
ul li {
background-color: aqua;
float: left;
}
ul li ul {
background-color: blue;
}
ul li ul li {
background-color: green;
}
a, a:link, a:hover, a:visited, a:active {
color: black;
text-decoration: none;
float: left;
}
-->
</style>
Original Post:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<title>untitled</title>
<style type="text/css">
<!--
ul{
list-style: none;
background-color: yellow;
margin: 0;
padding: 0;
white-space: nowrap;
}
ul li {
background-color: aqua;
display: inline;
}
ul li ul {
background-color: blue;
}
ul li ul li {
background-color: green;
}
a, a:link, a:hover, a:visited, a:active {
color: black;
text-decoration: none;
}
-->
</style>
<script type="text/javascript">
var $current = null;
$(document).ready(function(){
$("ul li ul").hide(); // hide submenus by default on load
$("ul li a").click(function(){
var $sub = $(this).next();
if ($sub.css("display") == "none")
{
if ($current != null)
$current.animate({ width: 'hide' }); // if you want to only show one sub at a time
$sub.animate({ width: 'show' });
$current = $sub;
}
else
{
$sub.animate({ width: 'hide' });
$current = null;
}
});
});
</script>
</head>
<body>
<ul>
<li>
<a href="#">Top-level 1</a>
</li>
<li>
<a href="#">Top-level 2</a>
<ul>
<li><a href="#">Bottom Level A1</a></li>
<li><a href="#">Bottom Level A2</a></li>
<li><a href="#">Bottom Level A3</a></li>
<li><a href="#">Bottom Level A4</a></li>
</ul>
</li>
<li>
<a href="#">Top-level 3</a>
<ul>
<li><a href="#">Bottom Level B1</a></li>
<li><a href="#">Bottom Level B2</a></li>
</ul>
</li>
<li>
<a href="#">Top-level 4</a>
</li>
</ul>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,您希望将第一级和第二级菜单放在一行(水平)中吗?
尝试这个:
If I understand you correctly you would like to have first and second menu level in one line (horizontal) ?
Try this:
我认为“display:inline”可以解决问题 - 但动画功能将显示设置为“块”而不是“内联”。
如果可以“捕捉”到位而不是动画,那么您可以这样做。
I think that "display:inline" would do the trick - but the animate function is setting the display to "block" instead of "inline".
If it is okay to "snap" into place instead of animating, you could do this instead.
打造自己很有趣。 使用这个神奇功能也很有趣:
http://jqueryui.com/demos/accordion/
It's fun to build yourself. It's also fun to use this awesomeness:
http://jqueryui.com/demos/accordion/