Skip to content Skip to sidebar Skip to footer

Jquery Check When an Element Is Clicked Again

.toggle( [elapsing ] [, consummate ] ) Returns: jQuery

Description: Display or hide the matched elements.

  • version added: 1.0 .toggle( [duration ] [, complete ] )

    • duration (default: 400)

      A string or number determining how long the blitheness will run.

    • complete

      A part to phone call once the animation is complete, chosen once per matched element.

  • version added: 1.0 .toggle( options )

    • options

      A map of boosted options to pass to the method.

      • elapsing (default: 400)

        A cord or number determining how long the blitheness will run.

      • easing (default: swing)

        A string indicating which easing function to use for the transition.

      • queue (default: true)

        A Boolean indicating whether to place the animation in the furnishings queue. If false, the animation volition brainstorm immediately. As of jQuery one.7, the queue choice tin can also have a string, in which case the blitheness is added to the queue represented by that cord. When a custom queue name is used the animation does not automatically outset; you must call .dequeue("queuename") to start information technology.

      • specialEasing

        An object containing one or more than of the CSS properties defined by the backdrop statement and their corresponding easing functions. (version added: 1.iv)

      • step

        A office to be chosen for each animated property of each animated element. This part provides an opportunity to modify the Tween object to change the value of the property earlier it is gear up.

      • progress

        A function to be called after each footstep of the animation, only in one case per animated element regardless of the number of blithe properties. (version added: 1.8)

      • consummate

        A function that is chosen in one case the animation on an element is complete.

      • start

        A function to telephone call when the animation on an element begins. (version added: 1.8)

      • done

        A office to be chosen when the animation on an chemical element completes (its Promise object is resolved). (version added: 1.eight)

      • fail

        A function to be called when the blitheness on an element fails to complete (its Promise object is rejected). (version added: 1.eight)

      • always

        A function to be called when the animation on an chemical element completes or stops without completing (its Promise object is either resolved or rejected). (version added: 1.8)

  • version added: ane.4.three .toggle( duration [, easing ] [, complete ] )

    • duration (default: 400)

      A string or number determining how long the animation volition run.

    • easing (default: swing)

      A string indicating which easing function to use for the transition.

    • consummate

      A function to call once the animation is complete, called once per matched element.

  • version added: 1.iii .toggle( display )

    • display

      Use true to show the element or faux to hibernate it.

Notation: The event treatment suite likewise has a method named .toggle(). Which 1 is fired depends on the set of arguments passed.

With no parameters, the .toggle() method just toggles the visibility of elements:

The matched elements volition be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it volition be hidden; if subconscious, it volition be shown. The brandish belongings is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once once again be displayed inline.

When a elapsing, a plain object, or a single "consummate" function is provided, .toggle() becomes an blitheness method. The .toggle() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0 after a hiding animation, the brandish style belongings is set up to none to ensure that the element no longer affects the layout of the folio.

Durations are given in milliseconds; higher values point slower animations, non faster ones. The strings 'fast' and 'slow' tin exist supplied to betoken durations of 200 and 600 milliseconds, respectively.

Equally of jQuery ane.4.three, an optional string naming an easing office may be used. Easing functions specify the speed at which the animation progresses at different points within the blitheness. The just easing implementations in the jQuery library are the default, called swing, and ane that progresses at a constant footstep, called linear. More than easing functions are available with the use of plug-ins, most notably the jQuery UI suite.

If supplied, the callback is fired once the blitheness is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but this is gear up to the DOM element being animated. If multiple elements are animated, it is important to note that the callback is executed once per matched element, non once for the blitheness as a whole.

We can animate any element, such as a simple image:

1

2

iii

4

                              

<img id="volume" src="book.png" alt="" width="100" tiptop="123">

We will cause .toggle() to be called when some other chemical element is clicked:

one

2

3

iv

five

                              

$( "#clickme" ).click( function() {

$( "#book" ).toggle( "slow", function() {

With the element initially shown, we can hide it slowly with the get-go click:

Figure 1 - Analogy of the toggle() effect when hiding the prototype

A 2nd click will show the chemical element once over again:

Figure 2 - Analogy of the toggle() outcome when showing the paradigm

The second version of the method accepts a Boolean parameter. If this parameter is true, and so the matched elements are shown; if simulated, the elements are subconscious. In essence, the statement:

i

                              

$( "#foo" ).toggle( display );

is equivalent to:

1

2

3

4

5

                              

if ( display === true ) {

} else if ( display === false ) {

Boosted Notes:

  • All jQuery effects, including .toggle(), tin exist turned off globally past setting jQuery.fx.off = truthful, which finer sets the duration to 0. For more information, see jQuery.fx.off.

Examples:

Toggles all paragraphs.

1

2

3

4

5

vi

vii

viii

nine

x

11

12

13

14

fifteen

sixteen

17

xviii

19

20

21

                                

<championship>toggle demo</title>

<script src="https://code.jquery.com/jquery-3.5.0.js"> </script>

<p style="display: none">Practiced Bye</p>

$( "button" ).click( part() {

Demo:

Animates all paragraphs to exist shown if they are hidden and hidden if they are visible, completing the animation within 600 milliseconds.

1

2

iii

4

5

6

7

viii

nine

10

11

12

13

14

fifteen

16

17

18

xix

20

21

22

23

24

25

26

27

28

                                

<championship>toggle demo</title>

<script src="https://code.jquery.com/jquery-3.5.0.js"> </script>

<button>Toggle 'em</push button>

<p>Such interesting text, eh?</p>

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

$( "p" ).toggle( "tiresome" );

Demo:

Shows all paragraphs, then hides them all, back and forth.

1

2

3

iv

5

6

7

8

9

x

eleven

12

13

14

15

sixteen

17

eighteen

19

20

21

22

                                

<title>toggle demo</title>

<script src="https://code.jquery.com/jquery-3.five.0.js"> </script>

<p style="display: none">Expert Bye</p>

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

$( "p" ).toggle( flip++ % two === 0 );

Demo:

rosseivernesse.blogspot.com

Source: https://api.jquery.com/toggle/

Post a Comment for "Jquery Check When an Element Is Clicked Again"