我应该在下面的代码中添加什么脚本,以便在滚动时添加活动类?

时间:2014-05-05 18:54:29

标签: javascript jquery css

我将添加什么脚本以使此脚本在滚动时将类激活为菜单项?

$(document).ready(function () {
    function setActiveStyleSheet(title) {
        var i, a, main;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
                a.disabled = true;
                if (a.getAttribute("title") == title) {
                    a.disabled = false;
                }
            }
        }
    }
    $(".main-nav a").click(function () {
        $(".main-nav a.active").removeClass("active");
        $(this).addClass("active");
        var elementClicked = $(this).attr("href");
        var destination = $(elementClicked).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination - 0
        }, 700);
        return false;
    });
});

1 个答案:

答案 0 :(得分:1)

我在setActiveStyleSheet中进行了更改。

    ----------------------------------------------



 $(document).ready(function () {

  function setActiveStyleSheet(title) {
     var i, links, main;
     links = document.getElementsByTagName("link");

 for (var l in links) {
    if (! links.hasOwnProperty(l))
         continue; // this goes straight to the next property

    if (links[l].getAttribute("rel").indexOf("style") != -1  && links[l].getAttribute("title")) 
               {
          links[l].disabled = true;
                  if (links[l].getAttribute("title") == title) {
                  links[l].disabled = false;
               }
    }
      }
       }

        $(".main-nav a").click(function () {
            $(".main-nav a.active").removeClass("active");
            $(this).addClass("active");
            var elementClicked = $(this).attr("href");
            var destination = $(elementClicked).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: destination - 0
            }, 700);
            return false;
        });
    });