当用户点击链接时,我想让我的JQuery菜单有一个简单的动画。我有一个箭头指向点击的链接,但我希望箭头在点击之间设置动画。如果用户单击Link1,则箭头向右滑动,如果用户单击链接2,则箭头向左滑动。我知道我可以使用“.animate”。但是我无法让它发挥作用。
HTML / CSS
<!doctype html>
<html>
<head>
<title>Demo</title>
<meta http-equiv="X-UA-Compatible" content="chrome-1">
<meta name="viewport" content="width=1240">
<style type="text/css">
#flyout
{ margin: 0;
padding: 0}
#flyout li
{ float: left;
list-style: none;
font: 12px Tahoma, Arial}
#flyout li a
{ display: block;
background: #20548E;
padding: 5px 12px;
text-decoration: none;
border-right: 1px solid white;
width: 80px;
color: #EAFFED;
white-space: nowrap}
#flyout li a:hover
{ background: #1A4473}
#flyout li ul
{ margin: 0;
padding: 0;
position: absolute;
left: 8px;
display: none;
padding-top: 10px;
border-top: 1px solid white}
#flyout li ul li
{ float: none;
display: inline;}
#flyout li ul li a
{ width: auto;
background: #9F1B1B;
width: 400px;}
#flyout li ul li a:hover
{ background: #7F1616}
</style>
</head>
<body>
<img class="arrow" src="img/arr.png" alt="arrow" style="position:absolute; top:32px; display:none;">
<ul id="flyout">
<li class="me a"><a href="#">Link 1</a>
<ul class="ul_a">
<li><a href="#">Sub A</a></li>
<li><a href="#">Sub B</a></li>
<li><a href="#">Sub C</a></li>
</ul>
</li>
<li class="me b"><a href="#">Link 2</a>
<ul class="ul_b">
<li><a href="#">Sub D</a></li>
<li><a href="#">Sub E</a></li>
<li><a href="#">Sub F</a></li>
</ul>
</li>
</ul>
JQuery的
(function(){
var closetimer = 0;
var menuitem = 0;
var arrow = $('.arrow');
function animate_right () {
var ele = $('.a');
var pos = ele.offset();
var left = pos.left;
var total = left+48;
arrow.show().css('left', total);
}
function animate_left () {
var ele = $('.b');
var pos = ele.offset();
var left = pos.left;
var total = left+48;
arrow.show().css('left', total);
}
function arrow_hide () {
arrow.hide()
}
function flyout_open(event)
{
flyout_close();
var submenu = $(this).find('ul');
menuitem = submenu.fadeIn(200);
if(submenu.hasClass('ul_a')){
animate_right();
return false;
} else if (submenu.hasClass('ul_b')) {
animate_left();
return false;
}
return true;
}
function flyout_close()
{ arrow_hide(); if(menuitem) menuitem.fadeOut(200);}
$(document).ready(function()
{ $('#flyout li').bind('click', flyout_open);
});
(document).onclick = flyout_close;
})();
答案 0 :(得分:0)
您可能希望将#flyout
职位更改为亲戚:
#flyout{
position : relative;
}