2013-05-30 6 views
0

내 문제에 대한 답변을 찾는 데 어려움이 있습니다. 나는 다음에 가지고있는 간단한 슬라이드 쇼와 이전 버튼을 가지고 있지만, 이전 버튼을 누르면 첫 번째 이미지에서 멈 춥니 다. 첫 번째 이미지에서 마지막 이미지로 이동하지만 올바른 결과는 찾을 수 없습니다.슬라이드 쇼 만들기 img 처음부터 끝까지 이동

window.onload = function() { 
    var listNode = $("image_list"); 
    var captionNode = $("caption"); 
    var imageNode = $("image"); 

    var links = listNode.getElementsByTagName("a"); 

    // Process image links 
    var i, linkNode, image; 
    var imageCache = []; 
    for (i = 0; i < links.length; i++) { 
     linkNode = links[i]; 

     // Preload image and copy title properties 
     image = new Image(); 
     image.src = linkNode.getAttribute("href"); 
     image.title = linkNode.getAttribute("title"); 
     imageCache.push(image); 
    } 

    //next button handler 
    var nextButton = $("next"); 
    var imageCounter = 0; 
    nextButton.onclick = function() { 
     imageCounter = (imageCounter + 1) % imageCache.length; 
     image = imageCache[imageCounter]; 
     imageNode.src = image.src; 
     captionNode.firstChild.nodeValue = image.title; 
    } 

    //previous button handler 
    var prevButton = $("previous"); 
    var imageCounter = 0; 
    prevButton.onclick = function() { 
     imageCounter = (imageCounter - 1) % imageCache.length; 
     image = imageCache[imageCounter]; 
     imageNode.src = image.src; 
     captionNode.firstChild.nodeValue = image.title; 
    } 
+0

왜'imageCounter'를 두 번 정의합니까? – Schleis

답변

0

여기에 내가 (내가 구문에 대한 완전히 확실하지 않다) 할 것이 무엇 : 이것은 내가 무엇을 가지고

//previous button handler 
var prevButton = $("previous"); 
var imageCounter = 0; 
prevButton.onclick = function() { 
    imageCounter = (imageCounter - 1) % imageCache.length; 

    //------- INSERTED CODE --------// 
    imageCounter = imageCounter >= 0 ? imageCounter : imageCache.length - 1 ; //Meaning you've reached the first image and want to go to the last 
    //------- INSERTED CODE --------// 

    image = imageCache[imageCounter]; 
    imageNode.src = image.src; 
    captionNode.firstChild.nodeValue = image.title; 
} 

설명 : 그래서 imageCounter를 설정 한 후

, 당신은 그 가치를 확인합니다. 0보다 크거나 같으면 값을 유지하십시오. 그렇지 않다면 (마지막 이미지로 가고 싶다는 의미), 그 값을 imageCache에서 1을 뺀 값으로 설정하십시오. 배열 인덱스는 0부터 시작합니다. :)

+0

감사합니다. 설명 주셔서 감사합니다! – user1890525

+0

문제 없습니다. 기꺼이 도와 드리겠습니다! –