1

How can I make a custom animation?

$('button').toggle(function() {
  anim.go("fadeOut slow", $("mydiv"));
}, function() {
  anim.go("fadeIn slow", $("mydiv"));
});

Toggle is working, but first appears toggle animation, then mine. How can I disable default toggle anim? Like toggle(0) P.S I've tried to add 0 to my code, but then second function isn't working. Any ideas?

3
  • Your issue is because in the modern versions of jQuery toggle() no longer works in the manner you've used. You'll need to use click() and then check the visibility state of the element and then run the required animation Commented Apr 29, 2017 at 9:15
  • Thank you so much, please write answer. if($("mydiv").is(":visible")) { works perfectly! Commented Apr 29, 2017 at 9:21
  • No problem, glad to help. I added it for you. Commented Apr 29, 2017 at 9:25

1 Answer 1

1

Your issue is because in the modern versions of jQuery toggle() no longer works in the manner you've used, ie. two separate functions called alternately.

Instead you'll need to use click(), check the visibility state of the element and then run the required animation based on that, eg:

$('button').click(function() {
  var $div = $('#mydiv');
  var action = $div.is(':visible') ? 'fadeOut' : 'fadeIn';
  anim.go(action + " slow", $div);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.