2014-05-05 29 views
0

我將添加哪些腳本使該腳本在滾動時將類活動添加到菜單項?我應該在下面的代碼中添加哪些腳本,以便在滾動時添加活動類?

$(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; 
    }); 
}); 
+0

也許你可以創建一個[jsfiddle](http://www.jsfiddle.net)。 – creimers

+1

'(a = document.getElementsByTagName(「link」)[i])'無限循環 –

+0

@ Derek我應該在哪裏添加這個 (a = document.getElementsByTagName(「link」)[i])? –

回答

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; 
     }); 
    }); 
+0

上面的代碼我覺得它不工作 –

相關問題