May 22, 2012, 11:12:17 AM
Pages: [1]   Go Down
  Send this topic  |  Print  
Share this topic on FacebookShare this topic on DiggShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter Topic: Stylish Show/Hide Button in JQuery  (Read 201 times)
0 Members and 2 Guests are viewing this topic.
mikethedj4
Full Member
Resident Badass
*****

Reputation: +9/-1
Offline Offline

Posts: 548


Train Insane or Remain The Same


View Profile WWW
« on: April 21, 2011, 08:11:50 AM »

Click Here: To see a demo.

Hey Guys! Today I'll be showing you guys how to make a button/link you can press that'll open, and close in a nice animation, and will show/hide whatever it is you decide to put into it.
It's a very easy, and simple effect to create.
So lets get started!

The Setup:
So first off make a new folder on your desktop. I don't care what it says, just make sure it's relevant to what were creating. I named mine "JQuery-show-hide-button".
Now go into that folder, and make a new folder that says "js" (js = JavaScript)

Now make sure you download JQuery from here, and make sure the jquery.js file you just downloaded is in the "js" folder we created earlier.

Now open up Notepad (or whatever text editor you'd like to use) and put down the following code.

Code:
<html>
<head>
<title>Show/Hide Button</title>
</head>
<body>

</body>
</html>

TIP: This is the standard code you use when beginning to start coding a website. Feel free to change the title of the site to whatever you'd like.

Below the title tags put down the following code (make sure it's still in your head tags)
Code:
<script type="text/javascript" src="js/jquery.js"></script>

This code tells us were adding the Jquery Javascript file into the head of our website, so that when the site loads this will load with it.

The Show/Hide Button:
We now need to add the following code below the JQuery file we added in.
Code:
<script type="text/javascript">
$(document).ready(function() {
$(".showcode").click (function() {
$(this).next().toggle(300);
return false;
});
});
</script>

All this code tells us is that when we click the .showcode class (Inwhich we haven't made yet) that the next thing it'll do is toggle whatevers inside it, and the 300 is equal to 300ms.

Now inside your body tags you wanna add the following code.
Code:
<a href="#" class="showcode">Show/Hide Code:</a>
<p style="display:none;">
Whatever You Wanna Put In Here
</p>

The top line is a link that we'll click to open/close whatever's after it, that's in that div/table/etc: Will start to toggle. I made a paragraph, and added css to that to make it not display. So that when we click the link it'll toggle being shown, and hidden.

Now if you wanna change the link to a button, just replace this code.
Code:
<a href="#" class="showcode">Show/Hide Code:</a>

With this code.
Code:
<button type="button" class="showcode">Show/Hide:</button>

Now you're done all you need to do is save it as a "index.html" file in your "JQuery-show-hide-button" folder, and open it in your web browser to test out the effect.

The Code:
This is what your final code should look like.

Code:
<html>
<head>
<title>Show/Hide Button</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".showcode").click (function() {
$(this).next().toggle(300);
return false;
});
});
</script>
</head>

<body>
<button type="button" class="showcode">Show/Hide:</button>
<p style="display:none;">
Whatever You Wanna Put In Here
</p>
</body>
</html>

That's it for this tutorial, feel free to download the project file below.

* showhide.zip (63.63 KB - downloaded 0 times.)
Logged







B.SkiLLs
Xtreme Administrator
Resident Badass
*****

Reputation: +56/-0
Offline Offline

Posts: 2365



View Profile WWW
« Reply #1 on: April 22, 2011, 10:14:21 PM »

 thumbs up yay yay
Logged




mikethedj4
Full Member
Resident Badass
*****

Reputation: +9/-1
Offline Offline

Posts: 548


Train Insane or Remain The Same


View Profile WWW
« Reply #2 on: April 23, 2011, 07:16:45 AM »

I love JQuery!
Logged







B.SkiLLs
Xtreme Administrator
Resident Badass
*****

Reputation: +56/-0
Offline Offline

Posts: 2365



View Profile WWW
« Reply #3 on: April 23, 2011, 01:08:18 PM »

me too.
Logged




mikethedj4
Full Member
Resident Badass
*****

Reputation: +9/-1
Offline Offline

Posts: 548


Train Insane or Remain The Same


View Profile WWW
« Reply #4 on: April 23, 2011, 09:54:57 PM »

Especially combining it with CSS3, and HTML5 (still haven't figured canvas out yet)
Logged







Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to: