2016-05-03 7 views
0

こんにちは私はスライドショーのようなボタンで画像を切り替える必要がありますが、スライドする必要はありません。何かを試してみて、コードの種類で私を助けてほしいです。私はこの古いjavascriptスライドショーwihtoutスライド(onclick)

<img id="imageswitch" src="image/1.jpg" width="400" height="286" alt="photo1" /> 
<script> 

var image = document.getElementById("imageswitch") 

    function switchImage(){ 
     if(image.src = "image/1.jpg"){ 
      image.src = "image/2.jpg"; 

     }else if(image.src = "image/2.jpg"){ 
      image.src = "image/3.jpg"; 
      console.log("marche") 
     }else{ 
      console.log("dont work man") 
     } 
} 

document.getElementById("boutonright").addEventListener("click", function() { 
     switchImage(); 
    }); 
+2

@RobGいいえ、これはコードレビューのトピックでは遠隔地でさえありません。これは機能リクエストであり、レビューのリクエストではありません。それをここに置く。 – Mast

答えて

1
  • アレイにあなたのイメージのパスを入れてやっています。オンボタン
  • あなたの配列操作クリック:

    var image = document.getElementById("imageswitch"), 
     
        images = [ 
     
         "http://placehold.it/400x286/fb0/?text=1", 
     
         "http://placehold.it/400x286/0bf/?text=2", 
     
         "http://placehold.it/400x286/bf0/?text=3" 
     
        ]; 
     
    
     
    function switchImage(){ 
     
        if(this.id==="next") images.push(images.shift()); 
     
        else /* id==="prev*/ images.unshift(images.pop()); 
     
        image.src = images[0]; 
     
    } 
     
    
     
    document.getElementById("prev").addEventListener("click", switchImage); 
     
    document.getElementById("next").addEventListener("click", switchImage);
    #imageswitch{height:180px;}
    <button id="prev">PREV</button><button id="next">NEXT</button><br> 
     
    <img id="imageswitch" src="http://placehold.it/400x286/fb0/?text=1" alt="photo1" />
    :あなたはPREVNEXTボタンの両方を持っているしたい場合は

var image = document.getElementById("imageswitch"), 
 
    images = [ 
 
     "http://placehold.it/400x286/fb0/?text=1", 
 
     "http://placehold.it/400x286/0bf/?text=2", 
 
     "http://placehold.it/400x286/bf0/?text=3" 
 
    ]; 
 

 
function switchImage(){ 
 
    images.push(images.shift()); 
 
    image.src = images[0]; 
 
} 
 

 
document.getElementById("boutonright").addEventListener("click", switchImage);
#imageswitch{height:180px;}
<button id="boutonright">NEXT</button><br> 
 
<img id="imageswitch" src="http://placehold.it/400x286/fb0/?text=1" alt="photo1" />


関連する問題