【问题标题】:function not executing for the second time after onclickonclick 后第二次没有执行函数
【发布时间】:2014-12-07 07:12:22
【问题描述】:

应该在 JQuery 滑块的第三张幻灯片中运行的动画第一次运行良好,但第二次和连续两次运行良好。动画与滑块的下一个按钮相关联。单击下一个按钮会从当前幻灯片中删除“显示”属性,并将“显示”属性添加到下一张幻灯片。我已经尝试了几天来通过研究类似的场景并尝试不同的解决方案来调试它,但到目前为止这是不行的。任何人都可以帮忙吗?

function dog(){
    var dog = document.getElementById('dog_2');
    TweenMax.to(dog, 2, {left:"325px", repeat:2, yoyo:true});
}

function moveRight() {
    $('#slider ul').animate({
        left: - slideWidth
    }, 300, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

$('#slider').on('click', 'a.control_next', function (){
    moveRight();

    if($("li").next().length > 0){
        var next = $("#slider .slideshow .show").removeClass("show").next("li");
        next.addClass("show");
    } 

    else if  {
        $("li:last").removeClass("show");
        $("li:first").addClass("show");
    }

    if ($("li").eq(2).hasClass("show")){
        dog();
    };
});

【问题讨论】:

  • 如果可能的话,将你的代码发布到 jsFiddle 中......也需要查看 HTML
  • 感谢您查看。
  • 您是否检查了 GAL V 的以下答案.. 是否有效,,? $(document).on('click', '#slider a.control_next', function (){ });应该做的伎俩
  • 已发布工作解决方案.. 请参阅我的问题描述答案。 jsfiddle.net/bbe0Lstu/2

标签: javascript jquery gsap


【解决方案1】:

问题是,每次单击下一个或上一个按钮时,对 dog() 函数的调用都会在目标 div 上创建一个新的 tweenmax 动画实例。多个动画实例使其挂起。

方法一:First jsfiddle 1,维护一个tweenmax动画对象。只需在特定条件下通过调用 dog() 重新启动它;功能。

jQuery(document).ready(function ($) {
  var animate;
  var doggy = document.getElementById('dog_2');  
  animate = TweenMax.to(doggy, 2, {left:"325px", repeat:true, yoyo:true}); 

   function dog(){ 
     animate.restart();    
   } 
  ...........
  .............
  }); 

方法 2:Second jsfiddle。通过使用 jQuery 注入 HTML 创建目标 DIV。适合动态场景。

function dog(){
  var dog = document.getElementById('dog_2');  
  dog.remove();  
  $("li.dog").html("<div id=\"dog_2\"><img src=\"https://pbs.twimg.com/profile_images/604644048/sign051.gif\" style=\"height:50px;width:50px\"/></div>");
  var dog = $('#dog_2');
  //TweenMax.killTweensOf(dog);     
  animate =TweenMax.to(dog, 2, {left:"325px", repeat:true, yoyo:true});  
  animate.play();    
}  


$('#slider').on('click', 'a.control_next,a.control_prev', function (){
    if($(this).hasClass('control_next'))
    {moveRight();}
    else{ moveLeft();}
    var prev = $("#slider .slideshow .show").removeClass("show").next("li");
    prev.addClass("show");
    if ($("li").eq(2).hasClass("show")){
      // call dog(); on some condition if required.
    }; 
    dog();
});

【讨论】:

  • 哇,令人印象深刻。您会推荐一种方法吗?
  • 如果我是你,我会选择方法 1,因为它简单且易于管理滑块,而且它不会再次加载代码,它只会重新启动动画。跨度>
  • 非常感谢您抽出宝贵时间整理这些!
【解决方案2】:

所以这段 sn-p 代码有几个问题。

  1. else if { 必须有(condition),否则只需使用else {
  2. 您在 300 毫秒长的动画中切换幻灯片的顺序,导致 eq(2) 的可靠性非常差。每次单击下一步eq(2) 都会有所不同。事实上,点击它的唯一方法是在这 300 毫秒内重复点击 Next 链接三次。我建议改用classdata-attribute;无论幻灯片处于何种状态都不会改变的东西。
  3. 最后但同样重要的是,回答您的问题……它应该只工作一次。为了使动画多次播放,您需要“倒带”reverse() 才能再次播放。

看看我在下面做了什么。希望对您有所帮助!

var animation = null;

function dog(){
    if (animation !== null) {
        animation.reverse(-2);
    }
    
    var dog = document.getElementById('dog_2');
    animation = TweenMax.to(dog, 2, {left:"325px", repeat:2, yoyo:true});
}

function moveRight() {
    var slideWidth = 325;
    $('#slider ul').animate({
        left: - slideWidth
    }, 300, function () {
        $('#slider ul li:first-child').appendTo('#slider ul');
        $('#slider ul').css('left', '');
    });
};

$('#slider').on('click', 'a.control_next', function (){
    moveRight();

    if($("li").next().length > 0){
        var next = $("#slider .slideshow .show").removeClass("show").next("li");
        next.addClass("show");
    } 

    else {
        $("li:last").removeClass("show");
        $("li:first").addClass("show");
    }

    if ($("li").attr("data-dog") === "true"){
        dog();
    }
});
#dog_2 {
    position: absolute;
}

.control_next {
    position: absolute;
    bottom: 0;
}

ul {
    position: absolute;
}

li {
    display: none;
    width: 325px;
    height: 325px;
    background: lightblue;
}

li.show {
    display: block;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dog_2">dog</div>
<div id="slider">
    <ul class="slideshow">
        <li class="show">a</li>
        <li>b</li>
        <li data-dog="true">c - dog</li>
        <li>d</li>
    </ul>
    <a href="#" class="control_next">Next</a>
</div>

【讨论】:

  • 感谢您对数据属性的建议。我已经注意到动画发生了变化,想知道这是由什么引起的。我实施了更改,但没有让它工作,这可能是因为您只有一个 sn-p 代码。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多