JQuery as a chaining conditional / function

I have the following code:

if (current <= total) { $next.css({opacity: 0.0}) .addClass("active") .animate({opacity: 1.0}, 1000, function() { current -= 1; if (current == 1) { jQuery("#next").animate({opacity: 0}, 500); next_isShowing = false; } if (!prev_isShowing) { jQuery("#prev").animate({opacity: 1.0}, 500); } }); 

The only thing I really want to move this block of code before is ".animate":

  current -= 1; if (current == 1) { jQuery("#next").animate({opacity: 0}, 500); next_isShowing = false; } 

Is there a way to take this block of text and β€œsnap” it to my current thread?

+4
source share
2 answers

In this particular case, since the decrement and conditional action acted on another object, I could just move it outside the chain:

 if (current <= total) { $next.css({opacity: 0.0}) .addClass("active") .animate({opacity: 1.0}, 1000, function() { if (!prev_isShowing) { jQuery("#prev").animate({opacity: 1.0}, 500); } }); current -= 1; if (current == 1) { jQuery("#next").animate({opacity: 0}, 500); next_isShowing = false; } } 

But I found this plugin in case someone needs to bind a conditional:

http://benalman.com/code/javascript/jquery/jquery.ba-cond.js

It works like:

0
source

It looks like you are trying to write

 $(...) .filter(function() { return current === 1; }) .animate(...) .end() .thingy(...) 
+1
source

Source: https://habr.com/ru/post/1332378/


All Articles