Jquery Tutorial : Toggle Effect on Hover/Click

Posted by on March 31st in Tutorials.

toggleeffect

Today we will learn how to get simple toggle effect on hover/click with jQuery. We will use Css and Jquery to get our final output. Check out the demo before getting into the tutorial.

Demo

Step 1 : HTML

So now we will document our page. First of all we need heading and toggle box for hide and show. Check out the below html code to get it clear.

<h2>Move Cursor</h2>
<div class="togglebox">
<div class="content">
<h3>Content Here</h3>
<!--Content Here--></div>
</div>

Step 2 : Styling our HTML document

Now we need to style our html elements like heading, togglebox and content. See the codes below.

h2 {
	padding:10px;
	font-size:20px;
	color:#243953;
	background-color:#fafafa;
	border: 1px solid #a9a9a9;
	-moz-border-radius: 7px; /* Rounder Corner */
       -webkit-border-radius: 7px;
	-khtml-border-radius: 7px;
	text-align:center;
	font-family:Georgia, "Times New Roman", Times, serif;
	margin-bottom:5px;
}
.togglebox {
	background-color:#CCC;
	border: 1px solid #a9a9a9;
	/* Rounder Corner */
	-moz-border-radius: 7px;
        -webkit-border-radius: 7px;
	-khtml-border-radius: 7px;
	overflow: hidden;
	font-size: 1.2em;
	width: 295px;
	clear: both;
	margin-bottom:10px;
}
.togglebox .content {
	padding: 20px;
}

Step 3 : Jquery Time

Create a new JS file and then Link Jquery and new js file to Html document

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"><!--mce:0--></script>
<script src="toggle.js" type="text/javascript"><!--mce:1--></script>

Now to give some smoth toggle effect we need to write some simple jquery code to give it a life. Below are the codes with explanation.

Toggle effect on hover

$(document).ready(function(){
//Hide the tooglebox when page load
$(".togglebox").hide();
//slide up and down when hover over heading 2
$("h2").hover(function(){
// slide toggle effect set to slow you can set it to fast too.
$(this).next(".togglebox").slideToggle("slow");
return true;
});
});

Toggle effect on click

$(document).ready(function(){
//Hide the tooglebox when page load
$(".togglebox").hide();
//slide up and down when click over heading 2
$("h2").click(function(){
// slide toggle effect set to slow you can set it to fast too.
$(this).next(".togglebox").slideToggle("slow");
return true;
});
});

Demo

Try out our free gmat study guide, ccie security questions and latest PMI-001 training courses to get high flying success in finals. Beside mcitp dumps, 640-863 dumps, and 000-978 are the wings for flying.

Vikas ghodke, 21 Male from india, now working as freelance web designer. You can follow him on twitter @acrisdesign

57 Comments

  1. Social comments and analytics for this post…

    This post was mentioned on Twitter by CreativityGeek: Acris Design – Jquery Tutorial : Toggle Effect on Hover/Click http://bit.ly/9Dqa7z...

  2. [...] Con­tin­ued here: Jquery Tuto­r­ial : Tog­gle Effect on Hover/Click | AcrisDesign [...]

  3. You should solve the problem with queue effect , .stop() I think will help

  4. wow… it is really cool. Good work, mate :)

  5. [...] Here is the original post: Jquery Tutorial : Toggle Effect on Hover or Click [...]

  6. [...] View post: Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  7. [...] Jquery Tutorial: Toggle Effect on Hover/Click – http://bit.ly/cvFq1q [...]

  8. [...] Go here to read the rest: Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  9. [...] (64)  JQuery tutorial: efecto hover activar / clic [...]

  10. [...] Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  11. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  12. Jquery Tutorial : Toggle Effect on Hover/Click …

    Today we will learn how to get simple toggle effect on hover/click with jQuery. We will use Css and Jquery to get our final output….

  13. [...] Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  14. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  15. [...] Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  16. Great tutorial, the .hover() function in jQuery is really useful. In some instances, I’ve found it best to use the jQuery hoverintent plugin to hide/show things since sometimes if a user hovers over lots of elements it can be confusing and distracting to them.

    http://cherne.net/brian/resources/jquery.hoverIntent.html

  17. Good Job

  18. [...] Link: Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign [...]

  19. [...] jQuery Tutorial : Toggle Effect on Hover or Click [...]

  20. [...] jQuery Tutorial : Toggle Effect on Hover or Click jQuery Tutorial : Toggle Effect on Hover or Click [...]

  21. [...] Tutorial by Acris Design. Works great for FAQ [...]

  22. [...] jQuery Tutorial : Toggle Effect on Hover or Click Filed in Tutorial, Uncategorized, jQuery   Posted by Jon Phillips Tagged Design, jQuery, toggle, Tutorial blog.lanche86.com is powered by WordPress using theme Tribune [...]

  23. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  24. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  25. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  26. rocking information, friend thanks for this
    Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign


  27. Maykel Barcala

    Hi,

    I use your toggle effect onClick to show and hide a small gallery with a couple of pictures inside and it work perfect but i was wondering if you can help me to make done one small thing more. and it’s that would like to move up/down too the footer of the page so it doesn’t look so big when all is close it? any idea or help on this?

    regards

    mbarcala

  28. Jquery Tutorial : Toggle Effect on Hover or Click…

    Today we will learn how to get simple toggle effect on hover/click with jQuery. We will use Css and Jquery to get our final output….

  29. I like the way you have it laid out, Diabete Diabete

  30. Great tutorial, the .hover() function in jQuery is really useful. In some instances, I’ve found it best to use the jQuery hoverintent plugin to hide/show things since sometimes if a user hovers over lots of elements it can be confusing and distracting to them.

  31. vítores Malo, muy usar la información completa. Gracias.
    Jquery Tutorial : Toggle Effect on Hover or Click | AcrisDesign

  32. Dude! thanks a bunch. i tried so many jquery effects to hide a certain things in my blog but this is only the one tht worked perfectly.
    I have some question.. how can I make the content when i mouseover on it it will stays in place?

  33. Thanks for your toggle menu. Its very simple and easy to use.

  34. This is great…

    Can anyone help me to make the ‘Toggle Effect On Click’ toggle.js slide the old content away when a new header is clicked.

    I tried:

    $(document).ready(function(){
    //Hide the tooglebox when page load
    $(“.togglebox”).hide();
    //slide up and down when click over heading 2
    $(“h2″).click(function(){

    //I ADDED THIS BIT
    $(“.togglebox”).hide();

    // slide toggle effect set to slow you can set it to fast too.
    $(this).next(“.togglebox”).slideToggle(“slow”);
    return true;
    });
    });

    But it hide instantly and looked a bit amateur!
    Hope someone figures it.
    Thanks

  35. Great tutorial; This is really great…keep it up !!

  36. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  37. [...] Jquery Tutorial : Toggle Effect on Hover/Click [...]

  38. This is a great tutorial! Everything works and it looks great! Thanks for sharing.

  39. Thanks so much! I have been trying to learn this!

  40. [...] with jQuery( Demo | Download )Tabbed Content with Navigation using jQuery UI( Demo | Download )Toggle Effect on Hover/Click( Demo | Download )Create animated vertical Menu Using Jquery( Demo | Download )Creating an [...]


  41. Tim

    Thanks for sharing the tutorial, helped me a lot for hover click effect.

  42. Hi

    Nice tutorial.
    I have to ask one thing that can we show first(top) or all links on page load? I tried with this

    $(“.togglebox”).show();

    but it didn’t work.

  43. this is very great script of Jquery, i was wanted this hover script.
    This blog is Awesome


  44. Kenty Le

    Pretty good. tks :-)

  45. This is a great blog posting and very useful. I really appreciate the research you put into it.

Leave a Reply