最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery : how to hide div after animation? - Stack Overflow

programmeradmin0浏览0评论

I would like to hide dive after the animation. Is it possible ?

I can show you code here

$(document).ready(function() {
  $('button').click(function() {
    var $lefty = $(this).next();
    $lefty.animate({
      left: parseInt($lefty.css('left'),10) == 0 ?
        -$lefty.outerWidth() :
        0
    });
  });
});​

Demo

I would like to hide dive after the animation. Is it possible ?

I can show you code here

$(document).ready(function() {
  $('button').click(function() {
    var $lefty = $(this).next();
    $lefty.animate({
      left: parseInt($lefty.css('left'),10) == 0 ?
        -$lefty.outerWidth() :
        0
    });
  });
});​

Demo

Share Improve this question asked Apr 2, 2012 at 13:59 Mo.Mo. 27.6k36 gold badges166 silver badges233 bronze badges 2
  • Which <div> do you want to hide? #main? Or do you want to remove the <div> that was animated entirely? – Ry- Commented Apr 2, 2012 at 14:01
  • @minitech i need to hide div1 to show div2 – Mo. Commented Apr 2, 2012 at 14:04
Add a ment  | 

3 Answers 3

Reset to default 2

Pass a callback to animate as the second argument. Also, you'll need to change .next() to something else to find only the first child, and there are a few other optimizations that can be made. Your CSS is also wrong. Here's the updated jsFiddle.

$(document).ready(function() {
    $('button').click(function() {
        var $lefty = $(this).next().children().eq(0);

        $lefty.animate({
            left: -$lefty.outerWidth()
        }, function() {
            $lefty.next().show();
            $lefty.remove();
        });
    });
});​

As a fourth param, animate can take a callback to execute on plete. Alternatively, just use a plete attribute on your options object.

$(document).ready(function () {
    $('button').click(function () {
        var $lefty = $(this).next();
        $lefty.animate({
            left: (parseInt($lefty.css('left'), 10) == 0 ? -$lefty.outerWidth() : 0),
            plete: function () {
                /* Do hide here */
            }
        });
    });
});

Hide the div in there perhaps by calling hide() on this.

You can pass a callback to be called on pletion:

$(document).ready(function() {
  $('button').click(function() {
    var $lefty = $(this).next();
    $lefty.animate({
      left: parseInt($lefty.css('left'),10) == 0 ?
        -$lefty.outerWidth() :
        0
    }, function () {
      $lefty.hide();
    });
  });
});​
发布评论

评论列表(0)

  1. 暂无评论