Creating a Slide-In jQuery Contact Form : 10th October 2008



The following tutorial was written by Janko Jovanovic of Janko at Warp Speed.

Contact forms are an indispensable part of every website. They are mostly implemented on a separate page and they rarely display creativity, even though there are many ways to improve their visual style. In this tutorial you will see how to create a dynamic, slide-in contact form using jQuery.

Let’s see what we’re trying to achieve with this tutorial. The image below shows the layout of our goal. In the upper right corner of the content is “Contact us” link. When the user clicks on it, the contact form will slide down. When the user submits the form they will get a message that their message has been sent successfully and within two seconds, the entire form will slide up.

Slide in contact form

Note: You can make this form fully functional by adding validation and some real code that sends the email.

Check out the demo and download full source code here.

The structure

Let’s explain the structure first.


<div class=“box”>
<div id=“contactFormContainer”>
                <div id=“contactForm”>
                    <fieldset>
                        <label for=“Name”>Name *</label>
                        <input id=“name” type=“text” />
                        <label for=“Email”>Email address *</label>
                        <input id=“Email” type=“text” />
                        <label for=“Message”>Your message *</label>
                        <textarea id=“Message” rows=“3” cols=“20”></textarea>
                        <input id=“sendMail” type=“submit” name=“submit” onclick=“closeForm()” />
                        <span id=“messageSent”>Your message has been sent successfully!</span>
                   </fieldset>
                </div>
                <div id=“contactLink”></div>
            </div>
            <div class=“header”>
                <h1>
                Company logo</h1>
            </div>           
            ...
        </div>
</div>

The CSS Code

We have “contactForm” and “contactLink” divs inside the “contactFormContainer” which is positioned absolutely. ContactForm contains form elements, and contactLink will toggle our contact form on click. Simple enough, right? The CSS code will make this clearer:

           #contactFormContainer
            {
               position:absolute;
               left:600px;
               float:right;
            }
            #contactForm
            {
               height:277px;
               width:351px;
               background-image:url(‘bkg.jpg’);
               display:none;
            }
            #contactForm fieldset
            {
               padding:30px;
               border:none;
            }     
            #contactForm label
            {
               display:block;
               color:#ffc400;
            }     
            #contactForm input[type=text]
            {
               display:block;
               border:solid 1px #4d3a24;
               width:100%;
               margin-bottom:10px;
               height:24px;
            } 
            #contactForm textarea
            {
               display:block;
               border:solid 1px #4d3a24;
               width:100%;
               margin-bottom:10px;
            } 
            #contactForm input[type=submit]
            {
               background-color:#4d3a24;
               border:solid 1px #23150c;
               color:#fecd28;
               padding:5px;
            }               
            #contactLink
            {
               height:40px;
               width:351px;
               background-image:url(‘slidein_button.png’);
               display:block;
               cursor:pointer;
            }
            #messageSent
            {
               color:#ff9933;
               display:none;
            }

Let’s slide folks!


And to make this work, we have to add a touch of jQuery.

           $(document).ready(function(){
                $(”#contactLink”).click(function(){
                    if ($(”#contactForm”).is(”:hidden”)){
                        $(”#contactForm”).slideDown(“slow”);
                    }
                    else{
                        $(”#contactForm”).slideUp(“slow”);
                    }
                });
            });
            function closeForm(){
                $(”#messageSent”).show(“slow”);
                setTimeout(’$(”#messageSent”).hide();$(”#contactForm”).slideUp(“slow”)’, 2000);
           }

Let me briefly explain the code above. Clicking on #contactLink div will toggle contact form visibility. Once user submits the form, a message “Your message has been sent successfully!” will appear and the entire form will slide up to its original state.

Check out the demo and download full source code here.

Why all of this?

This is just a sample of how you can improve user experience by adding dynamic content to your pages. Besides a contact form, you can create a slide-in login form, search form or anything that you think will improve the user experience.

Is there anything that could be done better? Do you know any other way of doing this? Share some examples!


Don't miss the next article - subscribe!







Comments and Discussion


On 11th October 2008 , Jason Kershaw said:

This is great... seen it done on lots of sites and always wondered how it's done. Thanks!

On 11th October 2008 , Sasa Bogdanovic said:

It is good realization of a good idea. However, it must be improved to fall back if JavaScript is disabled. This can be done by setting the display: none in the css file and setting display:block by JavaScript. Also, the background of the contact form can be improved in at least three ways. Either by setting the background color of the contact form box (not to use the background image) or by making the background image 1px x 1pa and tiling it both horizontally and vertically. And finally, if you use the big background image, it can be preloaded.

On 11th October 2008 , Steven said:

The demo could be clearer. A user who just scanned the first few lines of the article should be able to eval the demo. THEREFORE the Contact button should say, "Click me". It took me a while to figure out what to do, including debugging the NoScript plugin settings...

On 11th October 2008 , Alan said:

I wonder how long Steven stumbled around in that big form to figure out what to click on?

On 11th October 2008 , Janko said:

Sasa, you made good points. Steven, I really don't understand - isn't 'Contact us' link BIG enough? And second: do you really think that 'Click me' is semantically better than 'Contact us' in this case? What would 'click me' mean to anyone?

On 13th October 2008 , mbh said:

Brilliantly simple! Thanks for sharing the Jquery. The only comment I might make... while the form is indeed simple it wasn't immediately clear how I could close the form if I decided not to send information. Yes, after some poking around I did determine the Contact Us portion of the form was a toggle switch. Clever though I'm wondering if at least a jquery hover comment of some kind wouldn't be helpful in this instance. Thanks for sharing!

On 14th October 2008 , Dave Adams said:

I implemented a similar jquery contact form on my site a few months ago, and it seems to work ok. The only thing I've done differently is that the link to the form is within the normal navigation.

On 22nd October 2008 , Nate said:

Nice tut! Is it possible to make this come out of the RIGHT side of the page? Still displaying the form the same, of course (would be kinda hard to read and fill out, hah). ?

On 22nd October 2008 , Janko said:

@mbh - yeah, a small "hide" button would do the work. Nate, you can slide it out of any side, and you can do it by using .animate(...) method.

On 24th October 2008 , Andy said:

This is a nice implementation but so obstrusive! There should be a fallback solution but not hiding via css since there would be no contact form at all. Why not linking to a separate contact page if JS is disabled... And yes I had a hard time to find the link in the upper right, a single paragraph of lorem ipsum should be enough.

On 28th October 2008 , Moris said:

Thank you, you code was just what I was looking for and using it has explained a few things. I am very new to jquery and javascript Could you possible direct me as to how to add a dark overlay as background .when the form slides in

On 5th November 2008 , Bret Truchan said:

Hello! It could just be me being dumb, but do you have an extra closing DIV tag in your demo above?

On 9th November 2008 , Joe said:

Great stuff. I think I'm in love with the internet.



Add your thoughts



Subscribe


Design Shack CSS Gallery RSS Feed Design Shack News RSS Feed Design Shack Email Newsletter Design Shack Twitter


Tutorial Archives






Sponsored By


PSD to HTML

Gooey Templates

Freshbooks Billing for Freelancers

13 Styles Free CSS Menus

Standard Advert

I'd like to be
inspired by:



Colour


Red Designs Blue Designs Green Designs Yellow Designs Brown Designs Orange Designs Brown Designs Grey Designs Multi Coloured Designs


Category




Layout


One Column CSS Layout Two Column CSS Layout Three Column CSS Layout Grid CSS Layout Other CSS Layout


I've got a
question...



What is Design Shack?

We showcase stunning CSS and Web 2.0 design, alongside resources and inspiration for you to succeed in the same way. We only offer the cream of great design, perfect for getting that spark of creativity going again.

Can you feature my site?

Of course - if it's up to the mark! Just fill in our simple form.

Who's behind the site?

Design Shack is brought to you by David Appleyard, a freelance designer from the UK who is available for work.