Today we focus on the toggle(fn, fn,….) function, which is mainly used to switch the rotation operation of a certain event. For example: for a button click event and a DIV background, click the button once to display the DIV background as blue. When clicking the second time, change the DIV's background color to black. The HTML code we defined is as follows:
In toggle(fn,fn,….); first understand the toggle() function, which is mainly used to switch elements display status. If an element is displayed, it becomes hidden after calling .toggle().
After clicking the button, the DIV is hidden, click again to display the DIV, and then loop.
Here we can manually set the display and hiding of elements, display: toggle(true); hide: toggle(false);
We can also set the speed for the hiding and display of elements.
$("#DivToggle").toggle(600);
Of course at this point we can think of what we often encounter in web pages: move the mouse up to display it, and then The effect will be hidden when the mouse is moved away.
Of course a hover(fnover,fnout) event is used here. We only need to set fnover and fnout to the above toggle(600);
The fnover here is the event function when the mouse moves up, and fnout is the event function when the mouse moves away.
We set toggle(600) as a separate function to call, which makes it easier to read.
In this way we move the mouse to the button, and then Turn it on, and you can see the hiding and display effects of DivToggle.
In order to demonstrate the rotation operation of toggle(fn, fn,...), we use the above function, and then add a Click event using btnShow to first hide and display the DIV, and then the style of the table DIV.
You can display hidden code like this (in fact, this code is problematic, but it can achieve the desired effect if written like this):
Looking back at this code, I think it is very problematic. toggle(fn,fn,…) itself is used to add Click events to specified elements. Then loop through the functions in toggle on this element. Suddenly I felt a little confused. Like twists. I hope Niuniu can explain it. In order to demonstrate the effect of the toggle round-robin function, there is no way to change the style of the Div by clicking the button. The code to change the DIV style is as follows:
At this point, it suddenly occurred to me that to achieve the above effect, load the event directly to the button, that is, "btnShow", which means first hiding the display, and then changing the style of the DIV. The detailed code is as follows:
This can achieve the above desired effect, that is, first hide, then show, and then change the style.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn