• RSS
  • Facebook
  • Twitter
Comments


Created by Martin Angelov on May 26th, 2010
A Pure CSS3 Button - Gradients, Rounded Corners & Text Shadow
Earlier this week, while working on ZineScripts‘ coupon code system, I found the need to be able to dynamically create and present a message box to Zine’s visitors. The usual routine would include digging through jQuery’s plugin repository and ending up with using a lightbox alternative, but I decided to take the time and build an one-off notification solution, which I am sharing with you today.

Step 1 – XHTML

Going straight to the point, what do you need to create this effect?
The only thing you need is to create a div on your page and put some content inside it. Something like this:
1<div id="box">
2<p><b>Title!b>Boring explanation.p>
3div>
In our example the title of the message, the message body, and the warning icon are all created by using a single
tag with some CSS wizardry. The warning icon is its background, and the title is a regular bold tag contained inside the paragraph.
BounceBox jQuery Plugin
BounceBox jQuery Plugin

Step 2 – CSS

The plugin, we are doing today, adds its own CSS rules for the box positioning, which make the bounce effect possible, but we still need to code the design of the box in our stylesheet file.

styles.css – Part 1

01/* The bouncing box */
02 
03#box{
04    background:url('img/box_bg.jpg') repeat-x center top #fcfcfc;
05    height:115px;
06    padding:20px;
07    margin-top:-10px;
08    padding-top:30px;
09    width:400px;
10    border:1px solid #fcfcfc;
11    color:#494848;
12    text-shadow:1px 1px 0 white;
13    font-family:'Myriad Pro',Arial,Helvetica,sans-serif;
14}
15 
16#box p{
17    font-size:25px;
18    background:url('img/warning.png') no-repeat 10px center;
19    padding-left:90px;
20}
21 
22#box p b{
23    font-size:52px;
24    display:block;
25}
26 
27#box,
28#main,
29a.button{
30    -moz-border-radius:10px;
31    -webkit-border-radius:10px;
32    border-radius:10px;
33}
Here we are styling the design of the bounceBox. There are also a couple of rules that are applied inline by jQuery, which assign a ‘fixed’ positioning to the box and center it in the middle of the page, which is required for the animation. This way there is a clear division between the styles for design and those for functionality.

styles.css – Part 2

01/* Styling the big button */
02 
03a.button{
04    color:white;
05    letter-spacing:-2px;
06    padding:20px;
07    display:block;
08    text-shadow:1px 1px 0 #145982;
09    font-family:'Myriad Pro',Arial,Helvetica,sans-serif;
10    font-size:80px;
11    font-weight:bold;
12    text-align:center;
13    width:350px;
14    border:1px solid #60b4e5;
15 
16    margin:60px auto;
17 
18    /*
19        CSS3 gradients for webkit and mozilla browsers,
20        fallback color for the rest:
21    */
22 
23    background-color: #59aada;
24    background-image: -moz-linear-gradient(#5eb2e2, #4f9cca);
25    background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#5eb2e2), to(#4f9cca));
26}
27 
28a.button:hover{
29    /* Lighter gradients for the hover effect */
30    text-decoration:none;
31    background-color: #5eb2e2;
32    background-image: -moz-linear-gradient(#6bbbe9, #57a5d4);
33    background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#6bbbe9), to(#57a5d4));
34}
In the second part of the code we apply a number of CSS3 rules to the button to achieve that polished look. Notice the two gradient rules which are targeted at Mozilla Firefox and the Webkit browsers (Safari & Chrome). Unfortunately, unlike with other CSS3 rules, they don’t share a common syntax for displaying a gradient, which raises the burden on the developer in some degree.
It is also important to specify a fallback background color in case the browser does not support CSS gradients.
A Pure CSS3 Button - Gradients, Rounded Corners & Text Shadow
A Pure CSS3 Button - Gradients, Rounded Corners & Text Shadow

Step 3 – jQuery

First lets start by creating our bounceBox plugin. As we’ve seen before, creating a jQuery plugin is just a matter of extending the $.fn object with a new function. The ‘this’ of the new function is equivalent to the jQuery set of elements that the method was called on.

bouncebox-plugin/jquery.bouncebox.1.0.js

01(function($){
02 
03    /* The plugin extends the jQuery Core with four methods */
04 
05    /* Converting an element into a bounce box: */
06    $.fn.bounceBox = function(){
07 
08        /*
09            Applying some CSS rules that center the
10            element in the middle of the page and
11            move it above the view area of the browser.
12        */
13 
14        this.css({
15            top     : -this.outerHeight(),
16            marginLeft  : -this.outerWidth()/2,
17            position    : 'fixed',
18            left        : '50%'
19        });
20 
21        return this;
22    }
23 
24    /* The boxShow method */
25    $.fn.bounceBoxShow = function(){
26 
27        /* Starting a downward animation */
28 
29        this.stop().animate({top:0},{easing:'easeOutBounce'});
30        this.data('bounceShown',true);
31        return this;
32    }
33 
34    /* The boxHide method */
35    $.fn.bounceBoxHide = function(){
36 
37        /* Starting an upward animation */
38 
39        this.stop().animate({top:-this.outerHeight()});
40        this.data('bounceShown',false);
41        return this;
42    }
43 
44    /* And the boxToggle method */
45    $.fn.bounceBoxToggle = function(){
46 
47        /*
48            Show or hide the bounceBox depending
49            on the 'bounceShown' data variable
50        */
51 
52        if(this.data('bounceShown'))
53            this.bounceBoxHide();
54        else
55            this.bounceBoxShow();
56 
57        return this;
58    }
59 
60})(jQuery);
We are defining four separate methods which convert the div to a bounceBox (and apply the positioning CSS rules), show it, hide it or toggle between the two by using the animate() jQuery method.
For the toggling we are keeping an internal variable with the data method, which marks whether the box has been shown or hidden.
All of these methods are available to you after you include the jQuery library and the jquery.bounce.1.0.js files to the page. For the neat bounce effect, you will need the jQuery easing plugin as well, which is included in the plugin directory in the zip.
It is really easy to use the plugin, as you can see from the code below.

script.js

01$(document).ready(function(){
02 
03    /* Converting the #box div into a bounceBox: */
04    $('#box').bounceBox();
05 
06    /* Listening for the click event and toggling the box: */
07    $('a.button').click(function(e){
08 
09        $('#box').bounceBoxToggle();
10        e.preventDefault();
11    });
12 
13    /* When the box is clicked, hide it: */
14    $('#box').click(function(){
15        $('#box').bounceBoxHide();
16    });
17});
The code above is executed when the document ready event is fired so we are sure that all the page elements are available to jQuery. The first thing we then do is to covert the #box div to a bounceBox, and bind listeners to the click event on the button and the box itself.
You can put whatever HTML code you want in the box div and it will be properly converted to a bounceBox. You can also have more than one bounce box on the page in the same time.
With this our BounceBox plugin is complete!

Conclusion

You can use this jQuery plugin to present notifications to the user in an eye-catching manner. You can easily put a registration form,  newsletter signup or even some kind of advertisement as the content of the box div. Feel free to experiment and share what you’ve done in the comment section.





Postado por Fernando Schimit - Fox Creative

Leave a Reply