【问题标题】:Jquery animate not working with variablesJquery动画不使用变量
【发布时间】:2015-12-04 07:14:34
【问题描述】:

我正在尝试使用 Jquery animate left 函数为框内的小圆圈设置动画。当我传递硬编码值时它工作正常。如下图所示,如果我从框宽中减去圆宽,则结果为 474。

    
        $(document).ready(function(){
             $("#start_a").toggle(function(){
                if ($("#start_a").html() === 'Roll'){
                    $("#start_a").html("Stop"); 
                    function start(){
                        $("#circle").animate({left: "+=474"},"slow",function(){
                            $("#circle").animate({left: "-=474"},"slow",function(){
                                start();
                            });
                        }); 
                    }
                    start();
                }
             },    
             function(){
                if ($("#start_a").html() === 'Stop'){
                    $("#start_a").html("Roll"); 
                    $("#circle").stop();
                    $("#circle").css("left", "initial");
                }
             });
        });       

为了使其动态化,我尝试通过传递变量来制作相同的动画,如下所示。 (仅粘贴新的 Jquery)。

$(document).ready(function(){
  
           var roll = $("#box").width() - $("#circle").width();
                 var effect = {};
                 var reverse = {};
                 effect['left'] = roll;
                 reverse['left'] = -roll;
                 $("#start_a").toggle(function(){
                    if ($("#start_a").html() === 'Roll'){
                        $("#start_a").html("Stop"); 
                        function start(){
                            $("#circle").animate(effect,"slow",function(){
                                $("#circle").animate(reverse,"slow",function(){
                                    start();
                                });
                            }); 
                        }
                        start();
                    }
                 },    
                 function(){
                    if ($("#start_a").html() === 'Stop'){
                        $("#start_a").html("Roll"); 
                        $("#circle").stop();
                        $("#circle").css("left", "initial");
                    }
                 });
            });

圆没有像预期的那样动画,它超出了框的边界。有人可以告诉我可能是什么问题吗?提前致谢!

下面是我的 HTML/CSS

<div id="box">
            <div id="circle"></div>           
        </div>
        <div id="box2">
            <button id="start_a" >Roll</button>
<!--            <button id="stop_a">Stop</button>-->
        </div>

<style>    
        #circle {
	    width: 20px;
	    height: 20px;
	    background: black;
	    border-radius: 50px;
            left: 0px;
            position: absolute;
        } 
        
        #box{
            border: 3px solid green;
            background-color: white;
            margin-left: 400px;
            margin-top: 100px;
            width: 494px;
            min-height: 300px;
            position: relative;
        }
        #box2{
            /*border: 3px solid red;*/
            margin-left: 400px;
            /*margin-top: 100px;*/
            width: 494px;
            min-height: 50px;
            position: relative;
        }
        #start_a{
            margin-left: 200px;
            top: 0px;
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        #stop_a{
            /*margin-left: 720px;*/
            /*top: 0px;*/
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        </style>

【问题讨论】:

  • 能否给我们相应的HTML/CSS。 JSFIDDLE 也会有所帮助。
  • @magreenberg - 我添加了我的 HTML/CSS。请立即检查。

标签: jquery variables animation


【解决方案1】:

好的,请尝试:

$(document).ready(function(){
  
  var roll = $("#box").width() - $("#circle").width();
  var effect = {};
  var reverse = {};
  effect['left'] = roll;
  reverse['left'] = 0;
  $("#start_a").click(function(){
    if ($("#start_a").html() === 'Roll'){
      $("#start_a").html("Stop"); 
      start();
    }else{
      $("#start_a").html("Roll"); 
      $("#circle").stop();
      $("#circle").css("left", "initial"); 
    }
  })   

  function start(){
    $("#circle").animate(effect,"slow",function(){
      $("#circle").animate(reverse,"slow",function(){
        start();
      });
    }); 
  }
});
#circle {
	    width: 20px;
	    height: 20px;
	    background: black;
	    border-radius: 50px;
            left: 0px;
            position: absolute;
        } 
        
        #box{
            border: 3px solid green;
            background-color: white;
            margin-left: 100px;
            margin-top: 100px;
            width: 494px;
            min-height: 300px;
            position: relative;
        }
        #box2{
            /*border: 3px solid red;*/
            margin-left: 400px;
            /*margin-top: 100px;*/
            width: 494px;
            min-height: 50px;
            position: relative;
        }
        #start_a{
            margin-left: 0;
            top: 0px;
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        #stop_a{
            /*margin-left: 720px;*/
            /*top: 0px;*/
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box">
  <div id="circle"></div>           
</div>
<div id="box2">
  <button id="start_a" >Roll</button>
</div>

【讨论】:

  • @Frank - 我刚试过你的代码。它给出的结果与我的相同。请您重新检查一下。
  • 好的,我已经更新了我的答案。您的反向数组width 必须是初始位置。当您添加-roll 时,位置为-474,没有0。请立即尝试
  • 这是有效的。但请帮助我理解,为什么在硬编码时为 -474,为什么在使用宽度时为 0。
  • 因为硬编码你的元素 posA 是 474px 并且你在 posB -474px 之后精确。所以 posA - posB (474 - 474 = 0) 但是使用你的代码你可以精确到 posA 是 474px 而 posB 是 (-posA),所以你的元素从 474px 到 -474px。请为你解决它,谢谢。
猜你喜欢
  • 2012-09-01
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多