Learn how to create a sliding dropdown effect jQuery div in this easy to follow tutorial

UBL Designs Blog

HOW TO CREATE A JQUERY SLIDEDOWN TOGGLE JQUERY EFFECT TUTORIAL

Intro

Within this tutorial we are going to create a toggle effect slide down div which will open a close on the same button.

Step 1: The Css

First we will write some css to style it a little. This part is not important within this tutorial as you can style it how you want but we just want to show you how we styled this tutorial!

html,body{
 width:100%;
 padding:0;
 margin:0;
}
 
.dropdownwrap{
 height:auto;
 float:left;
 margin:0px 0px 20px 0px;
 background-color:#434343;
 display:none;
 padding:20px;
}
 
.container{
 width:100%;
 float:left;
}
 
.container h2{
 width:100%;
 float:left;
 padding:40px 0px;
 margin:0;
 text-align:center;
 font-family: 'Patrick Hand SC', cursive;
 font-size:36px;
 color:#434343;
}
 
.dropdownwrap h3{
 width:100%;
 float:left;
 padding:40px 0px;
 margin:0;
 text-align:center;
 font-family: 'Patrick Hand SC', cursive;
 font-size:36px;
 color:white;
}
 
.container p{
 font-family: 'Open Sans', sans-serif;
 font-size:10pt;
 color:#B7B7B7;
 text-align:center;
 padding-bottom:50px;
}
 
.dropdownwrap p{
 font-family: 'Open Sans', sans-serif;
 font-size:10pt;
 color:white;
 text-align:center;
 padding-bottom:50px;
}
 
.container a,
.container a:visited{
 padding:8px 14px;
 font-size:12pt;
 border:1px solid #CCCCCC;
 background-color:#DDDDDD;
 text-decoration:none;
 text-transform:uppercase;
 color:#424242;
 border-radius:3px;
}
 
.container a:hover{
 padding:8px 14px;
 font-size:12pt;
 border:1px solid #F2F2F2;
 background-color:white;
 color:#CCCCCC;
}

Lets run you throught the main css which helps with the magic.

The html,body css is not needed, this is just styling the body of the html document.

The dropdownwrap can be styled to how it fits your site, the only inportant bit is the display:none;

This tells the browser not to show it, we will show this using jQuery a little later within the tutorial.

To be honest that is the only bit of the css you really need to know, the other bits are just styling the page.

Step 2: The Html

Now we have created the css all we have is the html and jQuery section, in this section we will be looking at the html.

First we need the dropdown div.

<div class="dropdownwrap"></div>

You can put all your information you want to show within the drop down within this div!

Now where ever you want the button to toggle this div, we have done it using a simple link but you can do it with a text, div, button or image but for this tutorial we have done it with a link.

<p><a id="dropdown" href="#" title="Click This Button">X</a></p>

The only important information you need to know is to set an id, we have created an id and called it dropdown, this is not relevant but I like to just call things for what they are!

Step 3: The jQuery

This is the last part of the tutorial.

You can either put the jQuery within your own or use the body, within this tutorial we have put it within the html.

If you have not already you will need to put the jquery link in, either from Google or straight from jQuery itself, I prefer using google.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Now for the custom jQuery!

<script>
$(document).ready(function(e){
 
 $('#dropdown').on('click',function(){
 
 $('.dropdownwrap').slideToggle();
 
 });
 
})
</script>

We first call the #dropdown when the #dropdown is clicked.

Basically its pinging the website every so many 100's of milliseconds and as soon as the link of #dropdown is clicked it then starts a function.

In our function it uses the jQuery's standard function of slideToggle.

We assign this function to the .dropdownwrap which means whenever the link is clicked it will dropdown and up the dropdownwrap div.

That concludes the tutorial, if this helped please share using the social buttons below!

CHECKOUT THE DEMO HERE

Comments

avatar

Alex 7 Apr, 2013

This is perfect, just what I was looking for. Thank you so much for posting this.

avatar

Admin 7 Apr, 2013

Your welcome!

avatar

anahita 16 May, 2013

how to add text to thw dropdown

avatar

Admin 17 May, 2013

Not sure what you mean?

avatar

Pixel 2 Jul, 2013

Nice script! How can I use more as one of them on one site?

avatar

Admin 2 Jul, 2013

Just do the same again :)

avatar

R 24 Jul, 2013

I tried using more than one. The first link/label opens all of them and the rest of the links don't seem to do anything. Maybe it's got something to do with the id="dropdown"?

avatar

Admin 24 Jul, 2013

You will need to create different names for each one.

avatar

karen 5 Jul, 2013

hi,admin.Do u know how to create Sliding Panel Menu using jQuery?been searching for lots of tutorial but none of them is useful.

avatar

Admin 5 Jul, 2013

Hi, can you please explain a little more, the reason is this is kind of a sliding panel. What are you after exactly? Thanks UBL

avatar

Ben 9 Jul, 2013

I have a sidebar and I want it to slide to the left and stop about 40px so that there are room in order to re-click the sidebar back out, How do I do this?

avatar

Admin 10 Jul, 2013

We will do a tutorial on something simular soon :)

avatar

Karlijn 21 Jul, 2013

Is it also possible to open it under the button, instead of above like this tutorial?

avatar

Admin 21 Jul, 2013

Yes, this is all to do with your css. Thanks UBL

avatar

R 25 Jul, 2013

I tried it with changing the ID tags. The script was

avatar

jen 6 Aug, 2013

thanks a lot. after many tries and quite some time searching, this has been the first script on that subject, that has worked for my beginner's knowledge on javascript and hence also properly within my html.

avatar

Admin 8 Aug, 2013

Your welcome :)

avatar

Nicole 15 Aug, 2013

How can we add multiple divs on the page that drop down individually? Right now I have two different div containers each with a button (X and Y) but when I click either, it drops down both containers instead of the corresponding one.

avatar

Admin 22 Aug, 2013

We will try and do another tutorial which will cover this soon Thanks UBL

avatar

Thach Pham 18 Aug, 2013

Nice short tips and very useful.

avatar

katonlof 22 Aug, 2013

thanks.. )

avatar

Halong Bay 14 Oct, 2013

Perfect tip. It help me alot. Many thanks.

avatar

Admin 17 Oct, 2013

Your Welcome

avatar

Martin 6 Dec, 2013

Hi! That's an awesome tutorial, good job :). Unfortunately I do have one question. I'm using your script to hide/show certain part of the text which is quite deep down on the page & each time someone closes it, they are sent to the top of the entire page. Could you provide some tip to fix this issue? Thanks!

avatar

Admin 9 Dec, 2013

You can use the preventDefault function, please see http://api.jquery.com/event.preventDefault/ Hope this helps Thanks UBL

avatar

Elzette 30 May, 2014

This is so awesome, simple and effective. How would you go about though, to add a class to My new text here when the dropdown wrap is open? For example, I would like to use it as a toggle bar with an arrow pointing down. Then with an image sprite, when the dropdown wrap is open to have an up arrow show, to indicate it should be closed again. With the extra class on open I can move the background image sprite to move to the up arrow?

avatar

Admin 9 Jun, 2014

Hi sorry that would take a whole new tutorial, I will of course take a look into bring a tutorial for this though :)

avatar

mark roberts 13 Jun, 2014

HI, You should try jquery accordion widget. This would be the easiest way of doing most things, and best of all it is supported in all browsers. I prefer to use css3, but as a new technology, it has it's limits., but still fun to work with! Try this link.http://jqueryui.com/accordion/

avatar

Jasper 3 Jun, 2014

Hi UBL, this is an awesome thing, I do have a question. When referencing the CSS ".dropdownwrap" in the jquery script, it opens ALL dropdownwrap class DIV's. Do we need to create different CSS sections for each individual DIV? e.g. .dropdownwrap1 .dropdownwrap2 .dropdownwrap3 or can you link the function to a DIV ID?

avatar

Admin 9 Jun, 2014

You would need to add different names like you have said and then call it like you have already.

avatar

bartender 20 Jul, 2014

thanks a lot this it very useful and your theme is awesome !

avatar

Michael 11 Sep, 2014

Cool stuff thanks. On a note for those looking to have several "drops" on a page, if your page follows a template, in the head <> of that template just duplicate the downdrop css and give them different names like this .dropdownwrap1{ height:auto; float:center; margin:0px 0px 0px 0px; background-color:#ffffff3; display:none; padding:0px; } .dropdownwrap2{ height:auto; float:center; margin:0px 0px 0px 0px; background-color:#ffffff3; display:none; padding:0px; } Then on your individual page that is based off of that template, wherever your drops go, give each instance a unique ID name like "dropdown1" and "dropdown2" and finally, in each instance, make a div class match to one of the classes you placed in the template. for example and It works well.

avatar

KenM 15 Jan, 2015

Thanks for this tutorial. This works great on iOS, but on Android (both stock browser and Chrome), the animation is choppy and slow. Any suggestions?

avatar

Admin 30 Mar, 2015

Unfortunately there is an issue with the Android which jQuery have stated they know about it...

avatar

billboquet 21 Jan, 2015

Just what I was looking for ! You're the king ! And it's really clear for a newbie. Many thanks !

avatar

aicungdep 11 Dec, 2015

thank you, That is what I was looking for

avatar

HarmO 21 Jan, 2015

Great, just what i was looking for! Added a line to the script to prevent default link action

avatar

OScar 5 Mar, 2015

You my man, are what I call ' AWESOMMMEEEEE'. It's a perfectly written code without any glitch, down to the T. A double thumbs up

avatar

cambodian hair 27 Mar, 2015

thanks, your content is useful to me

avatar

internet việt nam 31 Aug, 2015

Hello all member! .Do u know how to create Sliding Panel Menu using jQuery? been searching for lots of tutorial but none of them is useful. Thank's a lot!

avatar

frank 10 Feb, 2016

thanks alot you guys saved me a create deal

avatar

cambodian hair 23 May, 2016

Thanks you!

avatar

Bao cao su GOLD BAO 7 Oct, 2016

Thank for sharing! ;)

avatar

Jitesh Manaktala 21 Jul, 2017

jQuery is easy and simple to learn.Your tutorial is very helpful for those who really want to learn something and improve their skills.I saw many tutorials but find your's best.Thank you for sharing such a knowledgeable post.Keep sharing.Hope to see more tutorials from you.

Comments are now closed for this post... – UBL Designs
TOP