3

我正在为我的 wordpress 网站使用浮动行菜单,我得到一些帮助。

现在我的问题是如何使选定的导航浮动线与将鼠标悬停在菜单项上时的颜色相同?此时鼠标一关闭,就会变成默认的红色。

这就是我想要实现的目标:

选择菜单 1 时(单击后),浮线颜色应为#0f0

选择菜单 2 -#ee0

已选择菜单 3 -#05f

任何帮助表示赞赏。

4

1 回答 1

1

var currentSelected;在上面添加 var sse1 = function () {并添加currentSelected=k;到下面的块

  for (var i = 0; i < a.length; i++) {
            if (url.indexOf(a[i].href.toLowerCase()) != -1 && a[i].href.length > nLength) {
                k = i;
                nLength = a[i].href.length;
            }
        }

将当前选定列表的索引推送到currentSelected.

所以在你的customHandleMenu函数中使用它

var navigations=[
/* Nav 1 style */
{floatColor: '#0f0',
borderColor: '#0f0'},
/* Nav 2 style */
{floatColor: '#ee0',
borderColor: '#ee0'},
/* Nav 3 style */
{floatColor: '#05f',
borderColor: '#05f'},
];

function customHandleMenu() {
  // get nav selector
  var nav = $('#sses1 > ul');

  // get float line selector
  var floatLine = $('.highlight'); // .hightlight must exist at this point

  // get colors for the current page
  var defaultBGcolor = navigations[currentSelected]["borderColor"];
  var defaultBorderColor = navigations[currentSelected]["borderColor"];
  var defaultNavBorderColor = navigations[currentSelected]["borderColor"];
  // change background-color and border-color on mouseenter event

  $('.nav-item-1').on({
    mouseenter: function () {
      setColors({
        floatColor: navigations[0]["floatColor"],
        borderColor: navigations[0]["borderColor"]
      });
    }
  });

  $('.nav-item-2').on({
    mouseenter: function () {
      setColors({
        floatColor: navigations[1]["floatColor"],
        borderColor: navigations[1]["borderColor"]
      });
    }
  });

  $('.nav-item-3').on({
    mouseenter: function () {
      setColors({
        floatColor: navigations[2]["floatColor"],
        borderColor: navigations[2]["borderColor"]
      });
    }
  }); 

    /*
       ...
    */

    // put back default colors on the mouseleave event
    $('#sses1 > ul > li').on({
        mouseleave: function() {
          setColors({floatColor:defaultBGcolor, borderColor:defaultNavBorderColor});
        }
    });
          setColors({floatColor:defaultBGcolor, borderColor:defaultNavBorderColor});

    function setColors(args) {
        if (typeof args.floatColor != "undefined") {
            floatLine.css('background-color', args.floatColor);
        }

        if (typeof args.borderColor != "undefined") {
            floatLine.css('border-color', args.borderColor);
            nav.css('border-bottom-color', args.borderColor);
        }
    }
}

现场演示| 演示源

为了防止浮动线动画加载,只需按照代码中注释的说明进行操作

//slip.style.left = items[k].offsetLeft + "px";
sse1.move(items[k]); //comment out this line and uncomment the line above to disable initial animation

创造:

slip.style.left = items[k].offsetLeft + "px";
// sse1.move(items[k]); comment out this line and uncomment the line above to disable initial animation.

此外,您可以选择添加

$('#sses1 li, #sses1 a').on('click',function(){ 
  $('#sses1, #sses1 ul, #sses1 li, #sses1 a').unbind("mouseout");
  $('#sses1, #sses1 ul, #sses1 li, #sses1 a').unbind("mouseleave");
  $('#sses1, #sses1 ul, #sses1 li, #sses1 a').unbind("mouseenter");
  $('#sses1, #sses1 ul, #sses1 li, #sses1 a').unbind("mousein");
  $('#sses1, #sses1 ul, #sses1 li, #sses1 a').unbind("mouseover");
  sse1={};
});

对于您的customhandlemenu功能,在页面准备加载时完全禁用任何不需要的动画。

现场演示| 演示源

于 2013-01-12T22:31:30.790 回答