我从CSSTRICKS得到了一段代码,神奇的行导航,它将在我的菜单项下放置一条线,并将移动到我悬停的菜单项上。我希望它是这样的,当我单击菜单时,行将停留在该菜单项,而不是像往常一样返回到第一个菜单项。
该脚本使用一个类选择器来告诉我们在默认情况下该行应该放在哪里。这样,我用jQuery创建了一个toggleClass,它将类传递给单击的菜单项,但它不会更新。我必须重新加载页面才能更新。再一次,它被卡在相同的位置。我希望有人知道怎么做。下面是我的代码:
$(window).bind("load", function () {
var $el, leftPos, newWidth;
$mainNav2 = $("#example-two");
/*
EXAMPLE ONE
*/
/* Add Magic Line markup via JavaScript, because it ain't gonna work without */
$("#line-menu").append("<li id='magic-line'></li>");
/* Cache it */
var $magicLine = $("#magic-line");
$magicLine.width($(".current_page_item").width())
.css("left", $(".active a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#line-menu li").find("a").hover(function () {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function () {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
});
发布于 2013-03-30 18:42:59
将magicline移到它自己的函数中。编辑内容注释如下。
$(document).ready(function(){
magicline();
});
function magicline() {
var $el, leftPos, newWidth,
$mainNav = $("#example-one");
//remove the old magic line before creating a new one.
$("#magic-line").remove();
$mainNav.append("<li id='magic-line'></li>");
var $magicLine = $("#magic-line");
$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#example-one li a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
}
//on click on a new menu item iterate through and remove the class
//then add the class back to the parent of the a clicked and recall the
//magicline function
$('li a').on('click',function(){
$('li').each(function(){
$(this).removeClass('current_page_item');
});
$(this).parent().addClass('current_page_item');
magicline();
});
演示:http://jsfiddle.net/calder12/vtuKH/1/
PS。我使用了CSSTRICKS中的代码,您必须更改id以匹配您的id。
发布于 2013-03-30 19:11:17
我或多或少猜到了CSS和标记,但这要简洁得多:
$(window).bind("load", function () {
var $lineMenu = $("#line-menu"),
$active = $lineMenu.children('.current_page_item'),
$magicLine = $("<li id='magic-line'></li>");
$magicLine
.width($active.width())
.css("left", $active.position().left)
.appendTo($lineMenu);
$lineMenu.on('mouseenter mouseleave click', 'li:not(#line-menu)', function (e) {
var $el = e.type == 'mouseleave' ? $active : $(this);
if (e.type == 'click') {
$el.addClass('active').siblings().removeClass('active');
$active = $el;
} else {
$magicLine.stop().animate({
left: $el.position().left,
width: $el.width()
});
}
});
});
发布于 2013-03-30 18:18:14
这行返回到原来的位置,因为有回调附加到hover
。在这种情况下,您需要做的是,当单击菜单项时,您需要避免该行转到原始位置。
我是这么做的-
附加点击处理程序,当用户点击菜单项时,设置一个标志来显示菜单被选中。在hover的回调函数中检查此标志,如果设置则返回。否则,如果不是平面设置,则将直线移至前一位置。
var $magicLine = $("#magic-line");
var isClicked = false; // to show whether menu is clicked or not
$magicLine.width($(".current_page_item").width())
.css("left", $(".active a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#line-menu li").find("a").hover(function () {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function () {
if(!isClicked){ // if no item is clicked, take line to old position
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
}else{ // else, just keep it there
isClicked = false;
return false;
}
}).click(function(e){ // added click listener
isClicked = true; // flag is set to indicate menu item is clicked.
$magicLine.css({
left: leftPos,
width: newWidth
});
});
首先,我认为unbinding hover
会阻止回调被调用,但这并没有实现。所以我用旗帜试了试。请阅读代码注释,我没有将所有内容都放在答案中。
https://stackoverflow.com/questions/15721499
复制相似问题