2011-10-24 27 views
1

我的工作an interface有四個主要部分:請投的眼睛在我的初學者的javaScript

  1. 當一個類別鏈接懸停,這一類不是在項目變暗(這似乎是工作確定)
  2. 當單擊類別鏈接時,不在此類別中的項目被隱藏(似乎也可以)
  3. 檢測到瀏覽器窗口大小並選擇適合的樣式表。即舊屏幕或手機。繼續並調整瀏覽器窗口的大小。
  4. 當瀏覽器窗口很窄時,會有一個額外的腳本向下滾動到「主」格。

    <div id="container"> 
    <div id="inner-container"> 
    <div id="tag-selector"> 
        <ul> 
         <li class="all">ALL PROJECTS</li> 
    <li class="graphic-design">graphic design</li> 
         <li class="logo-design">logo design</li> 
         <li class="photography">photography</li> 
         <li class="web-development">web development</li> 
         <li class="web-design">web design</li> 
        </ul> 
    </div> 
    <div id="main" role="main"> 
    <p class="items">There are x items in this category</p> 
    <p class="selected">No category selected</p> 
    <p class="clicked">No category clicked</p> 
        <section class="graphic-design"> 
         <p>graphic-design</p> 
        </section> 
        <section class="logo-design graphic-design"> 
         <p>logo-design</p><p> graphic-design</p> 
        </section> 
        <section class="logo-design graphic-design"><p>etc</p> 
        </section> 
    
    </div> 
    <footer> </footer> 
    

那麼這裏的JavaScript。對不起,如果它有點長。閱讀我希望應該很容易。

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 

    <script> 
$(document).ready(function(){ 
    var xwidth =$(window).width();//get width of user window 
    all_projects_showing_text="All projects showing. There are " + n + " projects, in " + t + " categories."; 
    adjustStyle(xwidth); 
    $("p.items").text(all_projects_showing_text + " Width=" + xwidth); 
    $(".all").addClass("selected"); 
    tag="all" 
}); 
</script> 

    <script> 
var n = $("section").length;//number of section boxes on page 
var t = $("#tag-selector li").length;//categories 
t--; 

$("#tag-selector li").click(function() {//clicking section filter li 
    $("#tag-selector li").removeClass("selected");//removes all filtered class 
    $(this).addClass("selected");//then adds it to the chosen link (li), showing it as current 
    tag=$(this).attr("class");//var tag is the class name of the chosen link, i.e. category 
    var split = tag.split(' '); // this splits the class string and puts each item in an array 
    tag = split[0];//this chooses the first item of the array, hence not including the hilite class 
    var numItems = $('.'+tag).length 
    var numItems=numItems-1;//correct for real number 

    if (tag!="all"){//if the all link is not picked 
    $("section").hide();// hide all the boxes 
    $("#main ."+tag).fadeIn();//show all the boxes with the tag class 
     if(tag=="graphic-design"){ 
      tag="Graphic design" 
     } 
     else if(tag=="logo-design"){ 
      tag="Logo design" 
     } 
     else if(tag=="photography"){ 
      tag="Photography" 
     } 
     else if(tag=="web-development"){ 
      tag="Web development" 
     } 
     else if(tag=="web-design"){ 
      tag="Web design" 
     } 

     $("p.items").text(numItems+" " +tag+ " projects"); 
     $("p.selected").text(tag +" selected."); 
     $("p.clicked").text(tag +" selected."); 
    } 
    else{ 
     $("section").fadeIn();//else show all the boxes 
     $("p.items").text(all_projects_showing_text);// all_projects_showing_text at onReady 
    }  
}); 
</script> 

    <script> 
    $("#tag-selector li").hover(function() { 

    hovered_link=$(this).attr("class");//get the class of the category being hovered 
    var split = hovered_link.split(' '); // this returns an array 
    hovered_link = split[0];//remove any other classes apart from the first i.e. remove hilite 

    if (tag=="all"){// if All are shown 
     if(hovered_link!="all"){ 
      $("section").not("."+hovered_link).addClass("section_darkened");//darken section which does not correspond with hovered category link 
      $("section").not(".section_darkened").addClass("outerglow");//add glow to not darkened sections 
     } 
    } 
    else{ 
    } 
    if (tag==hovered_link){// if the projects are already filtered by this category, say so on hover 
     $("p.selected").text(tag +" already selected."); 
    } 
    else{ 
     var numItems = $('.'+hovered_link).length 
     var numItems=numItems-1;//correct for real number 
     $("p.selected").text("Click to see only "+hovered_link+ " projects. (" +numItems+ " projects)"); 
    } 

    $(this).addClass("hilite");//hilite on hover over link 
    }, function() { 
     $(this).removeClass("hilite"); 
     $("p.selected").text("..."); 
     $("section").removeClass("section_darkened");//darken categories not in the selected category 
     $("section").removeClass("outerglow");//give the selected category items a glow 
    }); 
</script> 

<script> 
$(function() { 
    $(window).resize(function() { 
     adjustStyle($(this).width()); 
    }); 
}); 

function adjustStyle(width) { 
    width = parseInt(width); 
    if (width < 600) { 
     $("#tag-selector li").click(function() {// SCroll function for handhelds 
     $('html,body').animate({ 
       scrollTop: $("#main").offset().top}, 
       'slow'); 
     }); 
     $("#size-stylesheet").attr("href", "css/nav-style-narrow.css");//style sheet for handhelds 
    } else if ((width >= 440)&&(width < 1040)){ 
     $("#size-stylesheet").attr("href", "css/nav-style-med.css"); 
    } else { 
     $("#size-stylesheet").attr("href", "css/nav-style-wide.css"); 
    } 
} 
</script> 

如果你已經得到這麼多,看看,謝謝!

所以我的問題是;

  1. 我是否在我的循環中丟失了 break;?不太確定如何使用休息。
  2. 當我的CSS文件被選中時,第一個樣式會在其更改前閃現。有沒有辦法避免這種情況?
  3. 當瀏覽器處於最窄的樣式表中,並且我點擊了我的鏈接後,再次滾動時出現問題。幫幫我?! :-)
  4. 任何明顯的錯誤或遺漏,會使這更容易?
  5. 我開始覺得我的頁面上有很多腳本。也許我應該把它放在一個單獨的文件中。這會有幫助嗎?
  6. 可以發佈這樣的問題或者他們應該是個人嗎?

在此先感謝任何看過的人。

+0

我不禁爲你的嘗試和張貼代碼+1。謝謝! –

+1

'break'不是一個函數。 – SLaks

+3

單獨發佈每個問題可能會更好。儘量保持它儘可能的窄和容易重現,以便人們更容易回答。對於不是直接問題的更一般問題,您可能需要考慮http://codereview.stackexchange.com/ –

回答

1

OK,我會咬,雖然我不會嘗試提供一個全面的列表:

  • 有多種方法來處理CSS閃光燈問題。最簡單的方法是隱藏所有內容,直到您加載了正確的樣式表,然後在加載完成後顯示所有內容。

  • 是的,總的來說,將Javascript放在單獨的文件中總是一個好主意 - 它使得管理代碼更加容易,特別是如果您想在多個頁面上重複使用它。

  • 您錯過了很多var陳述,例如,爲all_projects_showing_text。這在語法上是正確的,但在Javascript中是一個糟糕的想法,因爲它使這些變量成爲全局變量,將它們附加到window對象。如果你需要一個全局變量,你仍然應該用var來聲明它。

  • 我沒有看到任何地方break將是適當的,甚至可能的。您通常在forwhile循環內使用break停止循環;我在代碼中沒有看到任何類似的循環。 JQuery代碼經常使用.each()來代替,或者只是通過選擇中的所有項目隱式循環;我很少在使用jQuery的代碼中看到break,儘管當然有些時候它可能是合適的。

  • 緩存或鏈接jQuery選擇器通常是一個好主意。例如,

    $("section").removeClass("section_darkened"); 
    $("section").removeClass("outerglow"); 
    

    可能是

    var $section = $section; 
    $section.removeClass("section_darkened"); 
    $section.removeClass("outerglow"); 
    

    $("section") 
        .removeClass("section_darkened") 
        .removeClass("outerglow"); 
    

    或者甚至(在這種情況下,由於.removeClass()也可以一次移除幾個類):

    $("section") 
        .removeClass("section_darkened outerglow"); 
    
  • 你長else if部分開始if(tag=="graphic-design"){可以爲地圖+查找結構更好:

    var tagTitles = { 
        "graphic-design":"Graphic design", 
        "logo-design":"Logo design", 
        // etc 
    }; 
    tag = tagTitles[tag]; 
    
+0

感謝尼克。那麼''.click'函數中使用的'tag'變量怎麼樣,因爲它也被我的'.hover'函數使用。在這種情況下可以成爲全球嗎? –

+0

可以有一個全局變量,但是你想讓它*顯式* - 或者在全局作用域中用'var tag;'聲明它,將其稱爲'window.tag',或者將它放在一個自定義名稱空間在全球範圍內。 – nrabinowitz

+0

非常感謝。謝謝您的意見。 –

1

關於break答:

break停止電流回路或switch的執行。您應該在循環中使用它,以便在當前迭代結束之前停止循環,或者在循環語句本身未檢查的條件下停止循環。您應該在caseswitch塊的末尾使用它,以便不執行後續的case s。

在您的具體代碼中,似乎沒有任何循環或switch es,所以任何地方都沒有任何break的地方。

+0

謝謝,這裏將刪除這部分 –

1
  1. break不是功能。這是一個聲明,所以你不要添加括號。
  2. 在選擇css之前加載頁面。如果您想要定位不同的屏幕尺寸,可以查看css3 media queries。在頁面的開頭添加樣式應該不會閃爍。您仍然可以使用js選擇樣式作爲備份方法。
  3. 我想你在上每增加一個新的點擊處理程序 resize事件!這是一個點擊運行的很多動畫,嘗試只設置一次處理程序。
  4. 缺失var,正如nrabinowitz所述。縮進可能會更好/更一致。
  5. JS in Separate files is better。
    • 客戶端緩存 - >頁面獲得第一次訪問後更快
    • 可重複使用不同的頁面
    • 更易於管理(版本控制)
  6. 單(以及研究)的問題一般都較好。
+0

還在學習基礎知識。謝謝。 –