如何切换动画

时间:2013-01-21 17:53:21

标签: jquery css jquery-animate

我有一个很好的动画。但我不能扭转它。有人可以给我一个提示吗?

$(".arrowclose").click(function() {
    $(".arrow").animate({ left: "218px" }, 1000, "easeOutQuad");                
    setTimeout(function() {
        $(".arrow").addClass("arrow_right arrowopen");
        $(".arrow").removeClass("arrow_left arrowclose");
}, 1000);
});

$(".arrowopen").click(function() {
    $(".arrow").animate({ left: "486px" }, 1000, "easeOutQuad");
    setTimeout(function() {
        $(".arrow").addClass("arrow_left arrowclose");
        $(".arrow").removeClass("arrow_right arrowopen");
    }, 1000);
});

3 个答案:

答案 0 :(得分:2)

jsBin demo

尝试一下,摆脱不必要的课程

$(".arrow").click(function() {     
   $(this).stop().animate({ left: $(this).offset().left > 218 ? 218 : 486 }, 1000, "easeOutQuad", function() {
       $(this).toggleClass("arrowopen, arrowclose");
   });
});

修改

到目前为止,我知道你想要一个箭头来切换容器,这样你可能会想要这个:

jsBin demo 2

将箭头放在容器内:

  <div class="wrapcontent">
    Test
    <div class="arrow arrowopen"></div>
  </div>

相应的风格:

.arrowopen{
  position:absolute;
  right:-20px;
  top:0px;
  cursor:pointer;
  width:20px;
  height:20px;
}
.arrowopen  {background:#cf5;}
.arrowclose {background:#f00;}

.wrapcontent {

 position:absolute;
 width: 300px;
 height:100px;
 background:#888;
 left:-300px;

}

在箭头上 - &gt;单击父容器的动画:

$(".arrow").click(function() {  

   $wrapcontent = $(this).closest('.wrapcontent');
   wrapOffset = $wrapcontent.offset().left;

   $wrapcontent.stop().animate({ left: !wrapOffset ? -300 : 0 }, 1000, "easeOutQuad");
   $(this).toggleClass("arrowopen, arrowclose");

});

答案 1 :(得分:1)

这是因为您在DOM就绪(您的arrowopen类)上不存在的元素上使用click事件。你应该使用.on()jQuery方法 - &gt; http://api.jquery.com/on/

答案 2 :(得分:0)

试试这个:

$(".arrowclose").click(function() {
   $(".arrow").animate({
        left: "218px"
      }, 1000, "easeOutQuad",function(){
        $(".arrow").addClass("arrow_right arrowopen").removeClass("arrow_left arrowclose");
    });
});


$(".arrowopen").click(function() {
    $(".arrow").animate({
        left: "486px"
    }, 1000, "easeOutQuad", function(){
       $(".arrow").addClass("arrow_left arrowclose").removeClass("arrow_right arrowopen");
    });
});

尝试一下,看看是否有帮助。