【问题标题】:Jquery scrolling thumbnail images animation not working properlyJquery滚动缩略图图像动画无法正常工作
【发布时间】:2017-09-10 00:24:54
【问题描述】:

这与我在How to make jQuery animate work only once? 发布的上一个问题有关。现在,我已经成功添加了所提供的解决方案,而且它似乎一开始就可以工作。所以我尝试点击下一个按钮,然后如果它达到第三次点击,jquery 滚动动画就会生效。但是当我尝试单击 prev 按钮时,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>

【问题讨论】:

  • 你会考虑用你目前的所有代码创建一个fiddle吗?

标签: javascript jquery html animation


【解决方案1】:

你有一个变量 animate 你试图控制两个单独的动画,只需设置另一个变量并以与第一个相同的方式使用它...

<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>

【讨论】:

  • 我还注意到下一个 if(counter == 2) 语句中有一些奇怪的地方。计数器第一次递增,如果计数器为 2 == 2,则不执行动画。但是如果计数器是3 == 2,就执行了动画,真的很诡异。
  • 观察你在哪里增加/减少计数器。
猜你喜欢
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多