banner



How To Stop An Animation In Javascript

The animations or effects make the content presentable for cease users. jQuery offers various methods such as animate(), fadeIn(), fadeout() etc., to add blitheness or issue. What if, you want to stop animation or event? Yous can do so too, let's see how?

The finish() method assists to finish animation or event in jQuery. The stop method provides manifold functionality, like stopping the animation/outcome instantly or sequentially. This article demonstrates the ways to terminate the animation or an result in jQuery.

How to stop animation or effect in jQuery

The stop() method in jQuery helps in stopping the animation or effect that is running. The syntax of the finish() method is shown below.

$(selector).cease (clearQueue, jumpToEnd) ;

The selector could be any HTML element or course/id of the element. Moreover, the stop() method offers two parameters (that are optional but not necessary)

  • clearQueue: It accepts Boolean values (either true or false) and decides virtually the stopping of upcoming animations. The false(default value) value directs that merely the current animation will be stopped, and other queued animations volition exist started afterwards. Whereas the true value terminates the animation instantly.
  • jumpToEnd: Its default value is false, if the truthful value is assigned then it ends the animations and the queue is cleared every bit well.

The higher up syntax works for various jQuery methods such as fading(), sliding(), evidence(), hide() as well.

How to stop animations in jQuery

This department practices a few examples that guide to end animations in various scenarios using the stop() method.

Example one: using stop() method without parameters

<script>

$(document).ready ( function ( ) {

 $( ".get-go" ).click ( office ( ) {

  $( "div" ).breathing ( {

   width: "1250px" ,

} , 5000 ) ;

} ) ;

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

  $( "div" ).stop ( ) ;

} ) ;

} ) ;

</script>

The above code animates the width of the div with a speed of milliseconds= "5000". Moreover, the stop() method stops the ongoing blitheness.

Output

Before animating

After applying the stop() method randomly (stopping anywhere)

Example 2: using stop() method with parameters

The post-obit code practices cease() method by using both parameters. And the value of the parameter is set to truthful.

<script>

$(document).ready ( function ( ) {

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

    $( "div" ).breathing ( {

      width: "1250px" ,

} , 5000 ) ;

} ) ;

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

    $( "div" ).stop ( true , truthful ) ;

} ) ;

} ) ;

</script>

The to a higher place code animates the width property and then the stop(true,true) method is applied.

Output

Earlier doing any action

Later starting the animation, when the cease-animation button is clicked the animation ends immediately.

How to terminate the fading() event in jQuery

The cease() method in jQuery tin can be used to terminate an effect equally well. The code provided below exercises the fading effect and and then the stop() method to stop that event.

<script>

$ (document).prepare ( office ( ) {

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

$ ( "div" ).fadeOut ( 2500 ) ;

} ) ;

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

$ ( "div" ).stop ( ) ;

} ) ;

} ) ;

</script>

The above code fades-out the div at speed of 2500 milliseconds and the stop method is used to stop the fading out method.

Output

Before commencement/end of fading procedure

After the fading-out process is started, when the cease button is clicked the fading process will be stopped as shown in the image below(in our case).

Conclusion

The stop() method of jQuery is used to cease the animation or an outcome. The stop() method accepts two parameters, and it can be applied without parameters likewise. Both parameters are Boolean in nature and thus true/simulated values are accepted merely. You would have learned the application of the stop() method to finish blitheness in jQuery. Moreover, the stop() method is besides exercised on a fadeOut() method in jQuery.

About the author

Source: https://linuxhint.com/stop-animation-effect-jquery/

Posted by: horneables1985.blogspot.com

0 Response to "How To Stop An Animation In Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel