如何在动画之前延迟.css功能

时间:2014-09-10 09:13:34

标签: javascript jquery html css

我检查了类似于我的其他帖子,但没有一个代码有效, 那么我怎样才能在动画之前为.css添加一些延迟

在这里你可以找到我的代码:

  $(document).ready(function() {

        $('.div2')
        .css('visibility', 'visible') <-- // I WANT TO ADD HERE A DELAY BEFORE THE ANIMATION THAT IS BELOW STARTS !!!!
          .animate({opacity: 1.0, left: '600px'}, 2000);


        $('.div2').animate({opacity: 0.0, left: '600px'}, 2000, setInvisible);
      });

      function setInvisible() {
        $('.div2').css('visibility', 'hidden');
      }

我想延迟css,所以div会出现延迟,动画会稍后开始。

这里是小提琴: http://jsfiddle.net/Rroni/4b56n6p1/

1 个答案:

答案 0 :(得分:3)

如图所示使用.delay(): -

$('.div2')
    .css('visibility', 'visible')
     .delay(1000)  //this time is in milliseconds increase or decrease as required.
      .animate({opacity: 1.0, left: '600px'}, 2000);

Fiddle Demo

根据提问评论,请尝试使用setTimeout(),如下所示: -

$(document).ready(function() {
    setTimeout(function() {
        $('.div2')
            .css('visibility', 'visible')
              .animate({opacity: 1.0, left: '600px'}, 2000);
    },1500)  //this time is in milliseconds increase or decrease as required.
});

Fiddle Demo