Fun with jQuery – toggle() – Easy tips to visually enhance your website
Here’s a quick tip to add some cool effects with jQuery that require very little code to implement. In this example I’m going to so how use of the toggle function can make the simplest functionality and jazz it up.
.toggle()
In a nutshell, the toggle function will call the show() function if your element isn’t visible and the hide() function if it is.
- Hamburger
- Philly Cheese Steak
- Taco
- Pizza
- Something else greasy and unhealthy
<br /> <input onclick="$(‘#preview1′).toggle();" type="button" value="Toggle My Menu" /><br />
So as expected, clicking the button will toggle the menu. Now lets get a little more flashy:
- Hamburger
- Philly Cheese Steak
- Taco
- Pizza
- Something else greasy and unhealthy
<br /> <input onclick="$(‘#preview2′).toggle(’slow’);" type="button" value="Toggle My Menu" /><br />
Now you have a ’sliding’ type effect for showing and hiding contents all with about 40 extra characters of text.
Want to be a bit more ‘dynamic’?
- Hamburger
- Philly Cheese Steak
- Taco
- Pizza
- Something else greasy and unhealthy
<br />
<input class="button" id="menuToggler" onclick="$(‘#preview3′).toggle(’slow’,function(){if($(this).is(‘:visible’)){$(‘#menuToggler’).val(‘Hide My Menu’);}else{$(‘#menuToggler’).val(‘Show My Menu’);}});" type="button" value="Hide My Menu" /><br />
Here is better formatted javascript:
<br />
$(‘#preview3′).toggle(’slow’,<br />
function(){<br />
if($(this).is(‘:visible’)){<br />
$(‘#menuToggler’).val(‘Hide My Menu’);<br />
}else{<br />
$(‘#menuToggler’).val(‘Show My Menu’);<br />
}<br />
}<br />
);<br />
What happens here is I have now created a function that is called once the toggle animation is done, it checks to see if the menu is visible and changes the buttons value based on that check.
As I said before, toggle just does the math and executes a hide or show on the element. Here is an example of the same thing using hide and show seperately.
- Hamburger
- Philly Cheese Steak
- Taco
- Pizza
- Something else greasy and unhealthy
<br /> <input class="button" onclick="$(‘#preview4′).hide(’slow’);" type="button" value="Hide My Menu" /><br /> <input class="button" onclick="$(‘#preview4′).show(’slow’);" type="button" value="Show My Menu" /><br />
Stay tuned for more fun with jQuery, my favorite JavaScript library. http://jquery.com/
1 Comment to Fun with jQuery – toggle() – Easy tips to visually enhance your website
Leave a Reply
Google Ads
Tags
Categories
- Hardware (1)
- Information Security (1)
- Scams (1)
- Programming (12)
- JavaScript (6)
- jQuery (4)
- MySQL (2)
- php (3)
- JavaScript (6)
- Quick Tips (7)
- Ramblings (30)
- System Administration (4)
Recent Posts
Recent Comments
- Issac Maez on Domain Name Search Engine Registration Mail Scam
- Jamie Rosborough on Fun with jQuery – toggle() – Easy tips to visually enhance your website
- Aknosis on Automating MySQL Database Backups on the Command Line via mysqldump
- brittany on Automating MySQL Database Backups on the Command Line via mysqldump
- Aknosis on More jQuery Fun – Auto Populating a Select Box
Twitter Feed...
- Transferred by bros comp into an Antec Nine Hundred Two. Awesome case, (I even fit the 24pin atx pwr cord behind the mobo) 1 day ago
- epic: http://www.youtube.com/watch?v=TQrAOQ4TzQc 1 day ago
- RT @jquery: jQuery 1.4.2 Released http://bit.ly/9ah4IV 2 weeks ago
- I say Colts 24 / Saint 20 2010-02-08
- How to Suck at Facebook http://theoatmeal.com/comics/facebook_suck from @oatmeal 2010-02-04
- More updates...
Powered by Twitter Tools
yummmy:P thx for your tips i’d love to follow you.anyway happy new year ~~~~~~~~~~~