Example of toggle method in jquery

Source: Internet
Author: User

(1) toggle

The switchover effect of hidden display should be quite common. There are multiple implementation methods for this effect:

The code is as follows: Copy code

Var phpersnote = document. getElementById ('phpernote ');
If (phpersnote. style. display = 'block '){
Phpersnote. style. display = 'none ';
} Else {
Phpersnote. style. display = 'block ';
}

Var phpersnote = $ ('# phpernote ');
If (phpersnote. is (': Den ')){
Phpersnote. show ();
} Else {
Phpersnote. hide ();
}

$ ('# Phpernote'). toggle ();

The above show hide toggle method has some parameters that can be called to achieve some animation effects, which is not described here.

(2) slideToggle

This is the pull-down or up effect, implementation method:

The code is as follows: Copy code

Var phpersnote = $ ('# phpernote ');
If (phpersnote. is (': Den ')){
Phpersnote. slideDown ();
} Else {
Phpersnote. slideUp ();
}

$ ('# Phpernote'). slideToggle ();

SlideDown slideUp slideToggle has corresponding parameters to achieve better animation effects for users.

(3) fadeToggle

Fade in the div and fade out the hidden effect:

The code is as follows: Copy code

Var phpersnote = $ ('# phpernote ');
If (phpersnote. is (': Den ')){
Phpersnote. fadeIn ();
} Else {
Phpersnote. fadeOut ();
}

$ ('# Phpernote'). fadeToggle ();

FadeIn fadeOut fadeToggle has corresponding parameters to achieve better animation effects for users.

(4) toggleClass

To add or remove a class to or from an object:

The code is as follows: Copy code

Var phpersnote = $ ('# phpernote ');
If (phpersnote. hasClass ('com ')){
Phpersnote. removeClass ('com ');
} Else {
Phpersnote. addClass ('com ');
}

$ ('# Phpernote'). toggleClass ('com ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.