2013-10-16 30 views
1

我有這個功能(transComplete),它執行突出顯示相關指示器的任務,顯示用戶它們在哪個頁面上,這些控制器/指示器的每個元素代表一個頁面,並將適當突出顯示。Javascript邏輯在一個循環/ if語句

這個工作是獨立的,但是當我引入一個點擊功能,它允許與指示器進行交互以在頁面之間移動時,它可以正確導航,但不會根據需要進行突出顯示(只能每兩次點擊),這導致我相信它的邏輯問題我的代碼。

的布爾邏輯真/假是原因,突出顯示只發生在該變量的「真」的情況下「isOnSecond」,所以我基本上需要一個解決方案點擊

的時總是突出的相關控制器主要功能是如下:

功能transComplete(){

slideTransStep = 0; 
crtSlideIndex = nextSlideIndex; 
// for IE filters, removing filters re-enables cleartype 
    if (nextSlide.style.removeAttribute) { 
     nextSlide.style.removeAttribute("filter"); 

    // show next slide 
    showSlide((crtSlideIndex >= totalSlides) ? 1 : crtSlideIndex + 1); 

    //Highlights a nav circle every two transitions as the boolean alternates 
     if (isOnSecond == true) { 

     //unhighlight all controls 
      for (var i = 0; i < slidesControllersCollection.length; i++) { 
       if (slidesControllersCollection[i].className === slideHighlightClass) { 
        slidesControllersCollection[i].className = ""; 
       } 
      // highlight the control for the next slide 
      document.getElementById("slide-control-" + crtSlideIndex).className = slideHighlightClass; 
      } 
      isOnSecond = false; 
     } 
     else { 
     isOnSecond = true; 
     } 
    } 

的onclick功能:

function clickSlide(control) { 

     showSlide(Number(control.id.substr(control.id.lastIndexOf("-")+1)),true); 
    } 
+0

我應該不是寫在這裏。當你調用showSlide時,你有這個檢查'((crtSlideIndex> = totalSlides)''''''''''''''''''''.getElementById(「slide-control-」+(crtSlideIndex> = totalSlides?1: crtSlideIndex + 1))。className' – Atle

回答

0

我認爲當你仍然從一個頁面迭代到另一個頁面時,你已經創建了反式函數,現在用戶可以使用任何框架,每次需要清除任何亮點,然後再次將其放在當前頁面上。

或者說,出於性能考慮,存儲上次突出顯示的內容,然後突出顯示新內容。

但是......爲什麼不放棄'onSecond'邏輯呢?它沒有多大意義的用戶,如果你把它的onSecond主意,在僅有的兩個獲得的一大亮點一次......

無論如何,邏輯是:

if (lastHighlighted) lastHighlighted.className = ""; 

if (isOnSecond) { 
    lastHighLighted = document.getElementById("slide-control-" + crtSlideIndex); 
    lastHighLighted.className = slideHighlightClass; 
} else { 
    lastHighLighted = null; 
} 

isOnSecond = ! isOnSecond; 

但事實上我不知道你想要的是不是版本不onSecond邏輯:

if (lastHighlighted) lastHighlighted.className = ""; 

lastHighLighted = document.getElementById("slide-control-" + crtSlideIndex); 
lastHighLighted.className = slideHighlightClass; 

(RQ聲明lastHighlighted爲VAR所以它可以在transComplete的範圍)