2013-04-18 77 views
0

對不起,我對JS很陌生。我有一個基本的html頁面,我在幻燈片中放置了一個幻燈片,每次點擊都會提升幻燈片。我想要發生的事情是,當用戶點擊下一張並且位於最後一張幻燈片上時,它會變爲新頁面。我的HTML:javascript slideshow在上一張幻燈片後加載新頁面

<div id="images"> 
    <img id="image1" src="http://www.lorempixel.com/960/580/sports" /> 
    <img id="image2" src="http://www.lorempixel.com/960/580/cats" /> 
    <img id="image3" src="http://www.lorempixel.com/960/580/food" /> 
    <img id="image4" src="http://www.lorempixel.com/960/580/people" /> 
</div> 

我的JS是這樣的:

var max = 4; 

function goToNext() { 
    var hash = String(document.location.hash); 
    if (hash && hash.indexOf(/image/)) { 
     var newh = Number(hash.replace("#image", "")); 
     (newh > max - 1) ? newh = 0 : void(null); 
     document.location.hash = "#image" + String(newh + 1); 
    } else { 
     document.location.hash = "image1"; 
    } 
} 

function goToPrevious() { 
    var hash = String(document.location.hash); 
    if (hash && hash.indexOf(/image/)) { 
     var newh = Number(hash.replace("#image", "")); 
     (newh == 1) ? newh = 5 : void(null); 
     document.location.hash = "#image" + String(newh - 1); 
    } else { 
    var url = window.location.href; 

if (url.search("#image") > 0) { 
    window.location.href = "strategy.html"; 
    }  
} 
} 

回答

0

改變這樣的:

(newh > max - 1) ? newh = 0 : void(null); 
     document.location.hash = "#image" + String(newh + 1); 

這樣:

(newh > max - 1) ? window.location.href = "strategy.html" : 
     document.location.hash = "#image" + String(newh + 1); 

jsfiddle

0

只是檢查是否newh爲零,

if(newh == 0) 
     document.location.href = "strategy.html"; 

這裏是所有的代碼,DEMO

var max = 4; 

function goToNext() { 
    var hash = String(document.location.hash); 
    if (hash && hash.indexOf(/image/)) { 
     var newh = Number(hash.replace("#image", "")); 
     (newh > max - 1) ? newh = 0 : void(null); 

     if(newh == 0) 
      document.location.href = "strategy.html"; 
     else 
      document.location.hash = "#image" + String(newh + 1); 
    } else { 
     document.location.hash = "image1"; 
    } 
} 

function goToPrevious() { 
    var hash = String(document.location.hash); 
    if (hash && hash.indexOf(/image/)) { 
     var newh = Number(hash.replace("#image", "")); 
     (newh == 1) ? newh = max+1 : void(null); 
     document.location.hash = "#image" + String(newh - 1); 
    } else { 
     document.location.hash = "image4";  
} 
} 
相關問題