jQuery toggle() Method: Hide and Show Elements with Animations
The jQuery 'toggle()' method is used to toggle between hiding and showing an element. It can be used to create a simple animation effect by toggling the visibility of an element.
Here's an example of how to use the 'toggle()' method:
HTML:
<button id='toggleButton'>Toggle</button>
<div id='content'>This is some content.</div>
JavaScript (using jQuery):
$(document).ready(function(){
$('#toggleButton').click(function(){
$('#content').toggle();
});
});
In this example, when the 'Toggle' button is clicked, the 'content' div will be toggled between being hidden and shown.
You can also specify the duration of the animation by passing a parameter to the 'toggle()' method. For example, you can use the 'toggle('slow')' or 'toggle('fast')' to create a slow or fast animation effect, respectively.
Here's an updated version of the JavaScript code with a slower animation:
$(document).ready(function(){
$('#toggleButton').click(function(){
$('#content').toggle('slow');
});
});
In this case, the 'content' div will be toggled with a slow animation effect.
原文地址: https://www.cveoy.top/t/topic/qga6 著作权归作者所有。请勿转载和采集!