2017-09-09 3 views
0

これは前に投稿した質問How to make jQuery animate work only once?に関連しています。今、私は正常に提供されたソリューションを追加し、それは最初に働いているようだ。だから私は次のボタンをクリックしてみて、それが3番目のクリックに達すると、jqueryスクロールアニメーションが有効になります。しかし、私は前のボタンをクリックしようとすると、jqueryのアニメーションは動作していません。それは前の画像に戻ると仮定します。この問題を解決するために親切に私を助けてください。ちょうど別の変数を設定して、それを同じように使用し、あなたが持つ2つの別々のアニメーションを制御しようとしているあなたは、変数アニメーションに持ってJqueryスクロールサムネイル画像のアニメーションが正常に動作しない

<html> 
<head> 
<title>TEST</title> 
<link rel="stylesheet" type="text/css" href="style.css"/> 
<script type="text/javascript" src="Image1.js"></script> 
<script type="text/javascript" src="Image2.js"></script> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script> 
</head> 
<body> 

    <img id="defaultPic" src="microsoftLogo1.jpg" /><br/> 
    <button id= "prev" class="portfolioNavigation">Previous</button> 
    <div id="container"> 
     <ul id="imageList"> 
      <li><img id="subPic1" src="microsoftLogo1.jpg" /></li> 
      <li><img id="subPic2" src="microsoftLogo2.jpg" /></li> 
      <li><img id="subPic3" src="microsoftLogo3.gif" /></li> 
      <li><img id="subPic4" src="microsoftLogo4.jpg" /></li> 
      <li><img id="subPic5" src="microsoftLogo5.png" /></li> 
      <li><img id="subPic6" src="microsoftLogo6.png" /></li> 

     </ul> 
    </div> 
    <button id= "next" class="portfolioNavigation">Next</button> 
    <script type="text/javascript"> 
    var animation = true; 
    var counter = 0; 
    var srcArray = ["microsoftLogo1.jpg", "microsoftLogo2.jpg", 
    "microsoftLogo3.gif", "microsoftLogo4.jpg", "microsoftLogo5.png", 
    "microsoftLogo6.png"]; 
    var numImages = srcArray.length; 

    if(counter == 0){ 
     document.getElementById('prev').disabled = 'true'; 
     document.getElementById('prev').style.opacity = '0.5'; 
    } 

    prev.onclick = function(){ 

    document.getElementById("defaultPic").src = srcArray[(counter - 1) % 
    numImages]; 
    counter--; 

    if(counter == 0){ 
     document.getElementById('prev').disabled = true; 
     document.getElementById('prev').style.opacity = '0.5'; 
     document.getElementById('next').disabled = true; 
    document.getElementById('next').style.opacity = '0.5'; 
    } 

     if(counter == 2){ 
     $(function() { 
      $('#prev').on('click', function() { 
        if (animation) { 

         $('#imageList').stop().animate({ 
          left: '+=285px' 
         }, 1000, function() { 
          animation = false; 
         }); 
       } 
      }); 
     }); 
     } 
    }; 

next.onclick = function() { 

document.getElementById("defaultPic").src = srcArray[(counter + 1) % 
numImages]; 
counter++; 

if (counter == 5) { 
    document.getElementById('next').disabled = true; 
    document.getElementById('next').style.opacity = '0.5'; 
} 

if (counter == 2) { 
    document.getElementById('prev').disabled = false; 
    document.getElementById('prev').style.opacity = '1'; 
    $(function() { 
      $('#next').on('click', function() { 
        if (animation) { 

         $('#imageList').stop().animate({ 
          left: '-=285px' 
         }, 1000, function() { 
          animation = false; 
         }); 
       } 
      }); 
    }); 
} 
}; 


</script> 
</body> 

+0

これまでのところ、すべてのコードで[fiddle](https://jsfiddle.net/)を作成することを検討しますか? – SpyderScript

答えて

1

:ここ

は完全なコードです最初のもの...

<html> 
<head> 
<title>TEST</title> 
<link rel="stylesheet" type="text/css" href="style.css"/> 
<script type="text/javascript" src="Image1.js"></script> 
<script type="text/javascript" src="Image2.js"></script> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script> 
</head> 
<body> 

    <img id="defaultPic" src="microsoftLogo1.jpg" /><br/> 
    <button id= "prev" class="portfolioNavigation">Previous</button> 
    <div id="container"> 
     <ul id="imageList"> 
      <li><img id="subPic1" src="microsoftLogo1.jpg" /></li> 
      <li><img id="subPic2" src="microsoftLogo2.jpg" /></li> 
      <li><img id="subPic3" src="microsoftLogo3.gif" /></li> 
      <li><img id="subPic4" src="microsoftLogo4.jpg" /></li> 
      <li><img id="subPic5" src="microsoftLogo5.png" /></li> 
      <li><img id="subPic6" src="microsoftLogo6.png" /></li> 

     </ul> 
    </div> 
    <button id= "next" class="portfolioNavigation">Next</button> 
    <script type="text/javascript"> 
    var animation = true; 
    var backAnimation = true; 
    var counter = 0; 
    var srcArray = ["microsoftLogo1.jpg", "microsoftLogo2.jpg", 
    "microsoftLogo3.gif", "microsoftLogo4.jpg", "microsoftLogo5.png", 
    "microsoftLogo6.png"]; 
    var numImages = srcArray.length; 

    if(counter == 0){ 
     document.getElementById('prev').disabled = 'true'; 
     document.getElementById('prev').style.opacity = '0.5'; 
    } 

    prev.onclick = function(){ 

    document.getElementById("defaultPic").src = srcArray[(counter - 1) % 
    numImages]; 
    counter--; 

    if(counter == 0){ 
     document.getElementById('prev').disabled = true; 
     document.getElementById('prev').style.opacity = '0.5'; 
     document.getElementById('next').disabled = true; 
    document.getElementById('next').style.opacity = '0.5'; 
    } 

     if(counter == 2){ 
     $(function() { 
      $('#prev').on('click', function() { 
        if (backAnimation) { 

         $('#imageList').stop().animate({ 
          left: '+=285px' 
         }, 1000, function() { 
          backAnimation = false; 
         }); 
       } 
      }); 
     }); 
     } 
    }; 

next.onclick = function() { 

document.getElementById("defaultPic").src = srcArray[(counter + 1) % 
numImages]; 
counter++; 

if (counter == 5) { 
    document.getElementById('next').disabled = true; 
    document.getElementById('next').style.opacity = '0.5'; 
} 

if (counter == 2) { 
    document.getElementById('prev').disabled = false; 
    document.getElementById('prev').style.opacity = '1'; 
    $(function() { 
      $('#next').on('click', function() { 
        if (animation) { 

         $('#imageList').stop().animate({ 
          left: '-=285px' 
         }, 1000, function() { 
          animation = false; 
         }); 
       } 
      }); 
    }); 
} 
}; 


</script> 
</body> 
+0

私はまた、次のif(counter == 2)文で何か変わったことに気付きました。カウンターが最初にカウントアップしていて、カウンタが2 == 2の場合、アニメーションは実行されていません。しかし、カウンタが3 == 2の場合、アニメーションが実行されますが、これは本当に奇妙です。 –

+0

カウンターを増減する場所を監視します。 – Adriani6

関連する問題