Simple "Call To Action" Button With CSS & jQuery Read more: Simple "Call To Action" Button With CSS & jQuery

Posted Posted by hacking tips in Comments 0 comments

Call to action in web design is a term used for elements in a web page that solicit an action from the user (clicking, hovering, etc). Today we’re going to create an effective and awesome call to action button with some CSS and jQuery that grab the user’s attention and entice him to click .
preview Simple "Call To Action" Button With CSS & 
jQuery
Throughout this tutorial we’ll explain every line of used code with details and hope it will be useful for you. The following tutorial uses HTML, CSS and jQuery with difficulty level Beginner and an estimated completion time of 45 minutes.


Download Tutorial (.zip) or Demo

Part I – Create The Button Image

In this first part we will show you how to create the needed images with Photoshop in simple easy steps. Let’s start.
Create a new Photoshop document with a width of 580px and a height of 130px. Go to View > New Guide then, set the Orientation to Horizontal and the Position to 65px.
set new guide Simple "Call To Action" Button With CSS 
& jQuery
Create more guides; each for the top, bottom, left and right. Your image should have these following guides once you are done:
all guides Simple "Call To Action" Button With CSS & 
jQuery
The guides appear to split your canvas into top and bottom halves. Select the Rounded Rectangular Tool, set the Radius to 5px and draw a rectangular shape on the top half of the canvas.
draw first rectangular Simple "Call To Action" Button 
With CSS & jQuery
Change the Styles for Gradient Overlay and Stroke.
box1 gradient overlay Simple "Call To Action" Button With
 CSS & jQuery
box1 stroke Simple "Call To Action" Button With CSS &
 jQuery
Select the Type Tool and type “Download” for as sample text into the box that you have created. Align the text to the center middle of the box and your output should look something like this:
first download button Simple "Call To Action" Button With
 CSS & jQuery
We finished the creation of the first state of the download button. Let’s create a new group and move all layers into it. Duplicate the group and then position it under the first group. we’ve created.
duplicate group Simple "Call To Action" Button With CSS 
& jQuery
Head over to the duplicated group and change the Gradient Overlay and Stroke style of the our second rectangular box (the hovered one) with the following setttings:
box1 hover gradient overlay  Simple "Call To Action" 
Button With CSS & jQuery
box1 hover stroke Simple "Call To Action" Button With CSS
 & jQuery
With the second group selected, use Move tool to move the entire rectangular box down to the second half of the canvas.
both download box Simple "Call To Action" Button With CSS
 & jQuery
That’s it! We finished with the first part. Save your image as download.png and fire up your favorite code editor.
Download PSD

Part II – The HTML

Step 1 – Prepare the necessary Files

Create a folder, and give it a name. We’ll name it jQueryCallToaction for this tutorial. Inside jQueryCallToaction folder, create these following files/ folders:
    1. Blank HTML file, index.html
    2. Blank CSS file, style.css
    3. Blank JavaScript file, script.js
    4. Folder, named "images"
    5. Place download.png inside images folder.

Step 2 – Link index.html with CSS & JS

Let’s link our CSS and JavaScript to index.html. Place them inside . We begin with the CSS file:

  1. <link href="style.css" rel="stylesheet" type="text/css" />  
then the latest version of jQuery from Google’s AJAX Libraries repository:

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">script>  
and finally our JavaScript file :

  1. <script type="text/javascript" src="script.js">script>  
Now our should look something like this :

  1. <head>  
  2. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">script>  
  3. <script type="text/javascript" src="script.js">script>  
  4. <link href="style.css" rel="stylesheet" type="text/css" />  
  5. head>  
Let’s put codes for the our buttons inside tag :

  1. <p><a href ="#" class="button1">a>p>   
  2. <p class="button2"><a href="#">a>p>   
Explanation:
We’ve created paragraphs for two buttons, each wrapped with

with hyperlink inside.
Line 1: class="button1" is placed inside , while
Line 2: class="button1" is placed inside


Step 3.1 – CSS only Button

We will create our first button, using CSS only. Open up style.css and paste the following codes inside.

Explanation:
Our first button is a 100% HTML/CSS button. CSS property background loads the download.png image with exactly the image’s width 580px but only half the height 65px (130/2) so only one of the two buttons in download.png is displayed. Button’s position is determined and controlled by the last value of background property. Think of the last value of background property as where (in terms of height position in pixel) the image should start from.

Step 3.2 – CSS + jQuery Button

We’ll be using the same image download.png for our second button. The difference here is: our second button will be injected with jQuery effect to make the animation smoother. Let’s start with the CSS. Place the follow codes inside style.css.
Explanation:
Basically both .button2 {} and .button2 a {} shares the same style except for the last value in background property. .button2 {} displays the blue color button while.button2 a {} displays white color button.

image7 Simple "Call To Action" Button With CSS & 
jQuery
CSS part is done. We’ll use jQuery to swap between both states to create a smooth transition effect. Open up script.js and put the following code inside.
  1. $(document).ready(function(){  
  2.    $('.button2 a').hover(function(){  
  3.    $(this).stop().animate({'opacity' : '0'}, 500);  
  4.    }, function(){$(this).stop().animate({'opacity' : '1'}, 500);});  
  5. });  
Explanation:
$(this) refer to .button2 a and when it is hovered, we are going to use the jQuery animation to set the opacity of this element to 0 so we can see the .button2 element (blue button). And when the mouse is out we are going to setback the opacity to 1 with 500 milliseconds for the animation speed.



blog comments powered by Disqus