Home >Web Front-end >JS Tutorial >A brief analysis of the difference between toggle and slideToggle in jquery
jQueryThe
toggle and slideToggle methods can both display and hide an element. Powerful~
The difference is:
toggle: the dynamic effect is from right to left. Lateral movements.
slideToggle: Dynamic effect from bottom to top. Vertical action.
So, for example, if you want to achieve a dynamic effect of a tree shrinking from bottom to top, just use slideToggle.
In addition, there are some parameters for toggle and slideToggle that can be set. For details, just look at the jQuery API~
Usage of toggle:
<SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></SCRIPT> <SCRIPT LANGUAGE=" JavaScript "> $(function(){ $("#z").toggle( function(){ alert(1); }, function(){ alert(2); }, function(){ alert(3); } ); $("#x").click(function(){ $("#z").toggle( function(){ alert(1); }, function(){ alert(2); }, function(){ alert(3); } ); }); }) </SCRIPT> <p style=" width :100px; height :100px; background-color :red;" id="z"></p> <p style="width:100px; height:100px; background-color:blue;" id="x"></p>
Usage of slideToggle:
<title></title> <script src="jquery-1.9.1.js" type="text/javascript"></script> <style type="text/css"> .imgclass { width: 300px; height: 300px; border: solid 1px red; } </style> <script type="text/javascript"> $(function () { $('#Button1').bind('click', function () { $('img').slideUp(2000); }); $('#Button2').bind('click', function () { $('img').slideDown(2000); }); $('#Button3').bind('click', function () { $('img').slideToggle(2000); }) }) </script> </head> <body> <p> <p> <input id="Button1" type="button" value="上拉" /><input id="Button2" type="button" value="下拉" /><input id="Button3" type="button" value="自动上拉下拉" /></p> <p> <img alt="" src="images/1.jpg" class="imgclass" /></p> </p> </body>
The above is the detailed content of A brief analysis of the difference between toggle and slideToggle in jquery. For more information, please follow other related articles on the PHP Chinese website!