我有一個導航欄,它使用列表中的另一個<li>
元素創建一個下劃線效果,該效果在懸停元素下面進行動畫處理,並在鼠標移出時回到活動元素上。
小提琴:http://jsfiddle.net/jP59W/
似乎在Chrome和Firefox,但在IE工作偉大的(8)我得到一個invalid argument error
,但我看不出有什麼可以導致它。
即使它工作在Firefox,它仍然拋出這個錯誤藏漢:
$el.position(...) is undefined
這是這一行:
leftPos = $el.position().left;
HTML
<div id="navbar">
<ul class="clearfix">
<li class="active">
<a id="myoeHome" href="#">Welcome</a>
</li>
<li>
<a id="myAccount" href="#">Your Profile</a>
</li>
<li>
<a id="referAFriend" href="#">Refer A Friend </a>
</li>
<li>
<a id="referralReport" href="#">View Rewards </a>
</li>
<li>
<a id="shoutandShare" href="#">Write a Review </a>
</li>
</ul>
</div>
CSS
#navbar {
position: relative;
background-color: #ffffff;
width: 990px;
margin: -21px 0 0 0;
padding: 20px 0 0 0;
height: 35px;
zoom: 1;
}
#navbar ul {
width: 945px;
list-style: none;
padding: 0 0 0 40px;
margin: 0 auto;
text-align: center;
position: relative;
top:-13px;
*margin: -25px auto 0 auto;
}
#navbar ul li {
display: inline-block;
text-align: left;
/* padding: 0 1px 0 1px; */
/* margin: 0 60px 0 5px; */
margin: 0 0 0 0;
padding: 10px 0 24px 0;
width: 185px;
*display: inline;
*zoom: 1;
*margin: 25px -11px 0 0;
}
#navbar ul li a {
font-family: Helvetica, Arial;
font-size: 18px;
color: #002e55;
text-align: center;
padding-bottom: 24px;
*display: inline;
*float: left;
}
#magic-line {
position: absolute;
bottom: 8px;
left: 0;
height: 3px;
background: url("http://www.4playtheband.co.uk/assets/nav-magic-line.png") no-repeat center bottom;
margin: 0 !important;
padding: 0 !important;
*bottom: 7px;
*z-index: 999;
*height: auto;
}
#navbar ul li a:hover {
height: 4px;
/*border-bottom: 3px solid #002e55;*/
zoom: 1;
*margin-top: -39px;
}
#navbar ul li a.active {
background: url("../images/psd/active-nav-bg.png") no-repeat 49% 2px;
height: 4px;
border-bottom: 3px solid #002e55;
zoom: 1;
}
的jQuery
// nav
var $el, leftPos, newWidth,
$mainNav = $("#navbar ul");
$mainNav.prepend("<li id='magic-line'></li>");
var $magicLine = $("#magic-line");
function navBar() {
// console.log('navBar start');
function checkActive() {
// console.log('check active');
// Hide magic line if nav bar has no active element
if($('#navbar ul').children('.active').length < 1) {
$magicLine.hide();
//console.log($('#navbar ul').children('.active').length < 1);
//console.log($('#magic-line'));
//console.log('hide');
}
else {
$magicLine.stop().animate({
left: $magicLine.css('left', $(".active a").css('left')),
width: $magicLine.width($(".active a").width())
});
}
}
checkActive();
$magicLine
.width($(".active a").width())
.css("left", $(".active a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $(".active a").width());
// $("#navbar ul li a").hover(function() {
// apply hover function to li instead and just just el to it's child
$("#navbar ul li").hover(function() {
// $el = $(this);
$el = $(this).children('a');
leftPos = $el.position().left;
newWidth = $el.width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
}, 600);
}, function() {
if($('#navbar ul').children('.active').length < 1) {
$magicLine.stop();
checkActive();
} else {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
//width: $magicLine.data("origWidth")
width: $magicLine.width($(".active a").width())
}, 600);
}
});
}
$(document).ready(function() {
$magicLine.width($(".active a").width());
navBar();
});
這對我無能爲力 - 我看不到任何類型的動畫:Ubuntu 12.04上的Firefox 19.0。 Firebug報告這個錯誤:'TypeError:$ el.position(...)未定義' – 2013-02-28 17:53:50
謝謝,它是你嘗試過的小提琴還是你在測試腳本上實現代碼?小提琴爲我工作在鉻,但該網站本身似乎並不想加載,即對我來說。將檢查在Firefox中的錯誤,但這是一個很好的幫助,實際上知道發生了什麼,可能應該已經嘗試過之前,即錯誤測試。 – martincarlin87 2013-03-01 09:06:55
我只試過小提琴。我也剛剛嘗試了使用Chrome的小提琴(再次,在我的Ubuntu 12.04桌面上),我看不到動畫。我改變了小提琴使用你的答案中的代碼,仍然沒有任何動畫。 – 2013-03-01 14:06:45