2017-09-09 1 views
0

이것은 이전 질문과 관련되어 있으며 How to make jQuery animate work only once?입니다. 이제는 제공된 솔루션을 성공적으로 추가했으며 처음에는 작동하는 것 같습니다. 그래서 나는 다음 버튼을 클릭했는데,이 3 클릭에 도달 한 후 경우, JQuery와 스크롤 애니메이션이 적용됩니다. 그러나 이전 버튼을 클릭하려고하면 jquery 애니메이션이 작동하지 않습니다. 이전 이미지로 돌아가는 것으로 가정합니다. 이 문제를 푸는데 친절하게 도와주세요. 또 다른 변수를 설정하고 그것을 같은 방식을 사용하여, 당신은 두 개의 별도의 애니메이션을 제어하려는 당신은 변수 애니메이션에 미칠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

관련 문제