srijeda, 3. ožujka 2010.

How To Make a Drop Shadow Changer With jQuery and CSS3

This is a Guest post by Ahmed Hussein, If you want to contribute feel free to contact me.

Today we have a very cool and step-by-step tutorial on how to make a CSS3 drop shadow changer using Ajax and little bit of PHP/MySQL. You may ask yourself why I’m using PHP/MySQL in a CSS3 tutorial? and the answer is simple, to save the settings and fetch again.

Demo
The Animation will only be viewable on Google Chrome and Safari 4+ because its based on WebKit.

What’s WebKit?

WebKit is an impressive open source web browser engine. WebKit is also the name of the Mac OS X system framework version of the engine that’s used by Safari, Dashboard, Mail, and many other OS X applications. WebKit’s HTML and JavaScript code began as a branch of the KHML and KJS libraries from KDE. And it is currently supported by Google Chrome and Safari4+.

Here’s an image about what we will have at the end of this tutorial:

Now let’s begin by building the main layout:

The Body Styling

body{
margin:0;
padding:0;
}

We did nothing except, set the padding as zero as well as the margin.

The Success Message Styling

#success{
background: #c8ffb4;
width:500px;
height:50px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
font-weight:bold;
padding-left:50px;
font-family:Arial, Helvetica, sans-serif;
color:#333;
padding-top:25px;
text-align:left;
}

What we did up there is first, change the background to light green then, the width and the height next, the border radius and this is only available in CSS3, and basically what it does, that it’s making rounded corners, so if you’re going to increase the value of the radius, you will get more rounded corners. after that, nothing too fancy just basic stuff like font and padding.

The Saving Button

#save{
background: url(../images/save.png) no-repeat;
height:60px;
width: 171px;
border:none;
cursor:pointer;
cursor:hand;
outline:none;
}

First, We set the background, height, width and border Then, we set the cursor to pointer to let the mouse change when it’s over the button, next set the cursor again to hand to change the mouse to hand icon. After that, change the outline to none, because a lot of browsers shows outlines on buttons.

The HTML5 Work





Drop Shadow Changer



























Posission:

Blur:










Saving...






Here’s the funny part, First i start the HTML page as HTML5 page by adding the !DOCTYPE html tag, then inside the head you will see two scripts tags, one of them is for including the jQuery and the other one is for including the main JavaScirpt file that we will made. And I included the style file we’ve made earlier.

Now take a look inside the body tag, Fist thing you will see is the success message div and we will hide this using jQuery, Next take a break line and you will see the image div that we will do all the work on just give it image as id to apply the style also we will need the id in the jQuery work. after that, take a break line and you will see a normal table but look closer you will find the magic.

First, we will ad a little td with one word inside Position underneath it you going to find normal html input but the type is different this time and it called Range and that gives you a slider controller with numeric value from -0 to 100 as default. So we will give this range input an id as slide because we will need this id in the JavaScript work. Now make another td and put a little div inside it and give it “nums” as id actually i don’t know why i called it like that but it’s okay, and inside this div we will but the numeric value of the position range input, then close the first TR.

Next, start another TR and make couple of TDs and inside the first one you going to see Blur because the CSS3 drop shadow is very customizable so you can adjust the position, blur and even the color. After that, make another td and inside it we will make another “Range/Slider” input and give it blure as id and i know it’s blur not “blure” :P . Then, do the same as the last time by making a little div and give it an id of blures. Close the TR and The whole table and let’s do something new.

Now make a little div and give it load as id inside this div we will add loading image that i created at http://www.ajaxload.info , and “saving” word. and we will hide this div using jQuery and will show it when we press save.

Now just add normal button input but give it save as id to apply the style and we will need this in the JavaScript, too.
Now after doing all of this you’re done with the main layout. Now Let’s do the JavaScript work

The JavaScript Work

//----------------------------------------+
//Code by Ahmed Hussein For CssReflex.com |
//Follow on Tiwtter @Valodes |
//Check Blog @ http://www.zingyso.com/b |
//----------------------------------------+
$(document).ready(function(){
var pos = $("#slide").val();
var blu = $("#blure").val();
$('#load').hide();
$("div#nums").html(pos);
$("div#blures").html(blu);
$("#success").hide();
$("#image").css({'-webkit-box-shadow': pos+"px "+pos+"px "+blu+"px "+"#666"});
$("#slide").change(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$("div#nums").html(num);
$("#image").css({'-webkit-box-shadow': num+"px "+num+"px "+blu+"px "+"#666"});
});
$("#blure").change(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$("div#blures").html(blu);
$("#image").css({'-webkit-box-shadow': num+"px "+num+"px "+blu+"px "+"#666"});
});
$('#save').click(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$.ajax({
url: "action.php",
type: "POST",
data: {pos: $("#slide").val(), blu: blu = $("#blure").val()},
success: function(data){
$("#success").html(data);
$("#success").slideDown(400).delay(2000).slideUp(400);
}
});
});
$("#load").ajaxStart(function(){
$(this).show();
});
$("#load").ajaxSuccess(function(){
$(this).hide();
});
});

Let me explain what we are doing here, first of all this is jQuery not pure JavaScirpt. Now to start jQuery code you may do this inside “script” or inside normal .js page and this’s what i’m doing here.
At the start of any jQuery code you have to put the following code:

$(document).ready(function(){//Your code here });

replace “//your code here” with your own code, now:

var pos = $("#slide").val();
var blu = $("#blure").val();

Here we made two variables the first one “pos” = to the slide value, “#slide” is like document.getElementById but we replace all of this with just a little hash tag “#”. the second variable blu = to the blure val. just to know “.val()” is returning the value of an element.
$("div#nums").html(pos);
$("div#blures").html(blu);

Now the first line here we changing what’s inside the div nums to html contents using the .html() function which is showing html elements normal without changing them to text. But you can show the contents as text by using .text() function.

$("#success").hide();

Now we just hid the success message using .hide() function.

$("#image").css({'-webkit-box-shadow': pos+"px "+pos+"px "+blu+"px "+"#666"});

Now i’m applying a css3 styling using .css function, and here’s how it works:
.css({“attributes”:”value”, “attributes2″:”value”, …}); so it’s just normal css but inside jQuery. But what i’m actually doing is i’m applying CSS3 drop shadow to the image using the default pos and blu values and they’re 50, so to do that i’m using the variables inside the function and i can do that just like that:
The normal css will be ‘-webkit-box-shadow’:'50px’, but we want to use the variables’ values so we will do it by adding plus signs between the words to separate them, i mean to make this work you have to write it like that:

css({'-webkit-box-shadow': variable+"normal text"+variable+"normal text"+variable+"normal text"})

So we put the normal text inside quote and separate between the different words and variables by plus sign.

The following code will be executed once you move the position slider:

$("#slide").change(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$("div#nums").html(num);
$("#image").css({'-webkit-box-shadow': num+"px "+num+"px "+blu+"px "+"#666"});
});

Now by using .change() we saying if the value of this element changed do the following, so if the value of slide changed the code inside .change() will be executed.
inside the .change() we redefining the variables again, then, changeing the nums div content to make it change when you move the slider.
After that we apply css3 drop shadow to the image with the new positioning,

num+"px "+num+"px "+blu+"px "+"#666"

the first num+”px” and the second one just repositioning the shadow and the blu+”px ” is still the same and it points to the blur value and finally the #666 is the shadow color.

The following code will be executed once you move the blur slider:

$("#blure").change(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$("div#blures").html(blu);
$("#image").css({'-webkit-box-shadow': num+"px "+num+"px "+blu+"px "+"#666"});
});

As you can see i’m doing the same thing but this time with the blure slider and the blures div.

The following code will be executed once you hit the save button:

$('#save').click(function(){
var num = $("#slide").val();
var blu = $("#blure").val();
$.ajax({
url: "action.php",
type: "POST",
data: {pos: $("#slide").val(), blu: blu = $("#blure").val()},
success: function(data){
$("#success").html(data);
$("#success").slideDown(400).delay(2000).slideUp(400);
}
});
});

First, the .click() function is very simple it says, if you click on the element the following code will be executed.
Inside the .click() we redefining the variables again, then, starting ajax request using $.ajax({}) so let me explain this:
using $.ajax() you will be able to send HTTP request without reloading the page, you can send both POST or GET requests to any type is files PHP, ASP, etc…

And it work just like this:

url: “your action file”,

type: “post or get”,

data: {name: variable or "text value", name2: variable or "text value", name3: variable or "text value"},

success: could be function or alert or whatever you want, this will be shown in case the request is succeeded.

function(data){
$("#success").html(data);
$("#success").slideDown(400).delay(2000).slideUp(400);}

This function shows the success message by slideDown then delay 2 seconds after that slideUp.

$("#load").ajaxStart(function(){
$(this).show();
});
$("#load").ajaxSuccess(function(){
$(this).hide();
});

Now we will show the load div once we send the request then hide it again once the request is done, using the $.ajaxStart() we will say when the request is sent show $(this) while $(this) stand for $(“load”). and using the $.ajaxSuccess() we will say when the request is send success message hide $(this) while $(this) stand for $(“load”).
And that’s it this is everything JavaScript

The PHP Work

The full code:

Explanation

First anything starts with “//” is a comment , and anything starts with “$” is a variable.
//Database connect
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
//Connection user and pass
$select = mysql_select_db("shadow", $connect);
//Selecting the Database

mysql_connect is the database connection function and it’s separated to 3 parts and sometimes 4, (“Your Host — Usually localhost”, “Database UserName”, “Database Password”, “Database Port”)

mysql_select_db is the the database selector function and it’s separated to 2 parts, (“Database Name”, “Database Connection Link And this is the connecting variable”)

Now before i continue explaining the rest of the code let me show you how to make MySQL Database.

The MySQL Database

Go to your phpmyadmin and do the follow the instructions below:





Click save and you done, but let me explain, something the AUTO_INCREMENT for the id is to let it increase by itself without having to insert a value for it everytime.

Let’s continue:
$pos = $_POST['pos']; //Position Value
$blu = $_POST['blu']; //Blur Value inside

Here you can see two variables first one $pos = to the post pos from the ajax request and the other one is $blu = to the post blu from the ajax request.
$Query = "INSERT INTO shad_set (`position`,`blur`) VALUES('".$pos."', '".$blu."')";
//The SQL Query
$Insert= mysql_query($Query) or die(mysql_error());
//Insert to database
echo "The new settings are saved!";

Here you can see two variables, too. the first one $Query = to the SQL query and inside it we saying ” insert the following data inside shade_set table into fields `position` and `blur` and the actual values are the variables $pos for position and $blu for blur”

Then using mysql_query() function we execute the $Query… Next after all of this print or echo message and this is the success message “The new settings are saved!”…

but to fetch the saved settings from the database you can do it this way:

It’s the same thing about database connect, but the query is changed now and we are saying select all data from shad_set where the id field = 1 or whatever id you want to fetch.
Then $res = mysql_fetch_array($insert) which transform data from database to array.
$res['pos'] = the pos from the database and same for $res['blu'];

Now we’ve done everything… So now you have the drop down shadow changer with php/mysql saving option . Hope you enjoyed the tutorial. If you you have any questions, feel free to ask by commenting below.

Recreating CNN’s Beveled Navigation Buttons with Pure CSS

When CNN recently redesigned their website, they created a strongly-branded and beautiful header section that includes a navigation bar with beveled buttons. Although CNN’s navigation bar utilizes CSS sprites for highlighted and active buttons, the primary design of the buttons (the beveled effect) is done with pure CSS.

I think this is a commonly-known technique among experienced CSS developers, but many beginning web designers will probably resort to images to create this effect, which is not necessary. So, in this brief tutorial I’ll show you how to create a navigation bar with beveled edges using pure CSS, identical to that on CNN.

View the demo page to see exactly what we’ll be creating.

First, the markup, which is nothing unusual, just an unordered list:

  1. <ul id="navigation">
  2. <li class="first"><a href="#">Homea>li>
  3. <li><a href="#">Videoa>li>
  4. <li><a href="#">NewsPulsea>li>
  5. <li><a href="#">U.S.a>li>
  6. <li><a href="#">Worlda>li>
  7. <li><a href="#">Politicsa>li>
  8. <li><a href="#">Justicea>li>
  9. <li><a href="#">Entertainmenta>li>
  10. <li><a href="#">Techa>li>
  11. <li><a href="#">Healtha>li>
  12. <li><a href="#">Livinga>li>
  13. <li><a href="#">Travela>li>
  14. <li><a href="#">Opiniona>li>
  15. <li><a href="#">iReporta>li>
  16. <li><a href="#">Moneya>li>
  17. <li><a href="#">Sportsa>li>
  18. ul>

The only thing to note in that code is the class “first”, which will help us round out the effect.

Here’s the CSS for the

    element, which has an id of navigation:

    1. #navigation {
    2. list-style: none;
    3. background: #b60002;
    4. overflow: hidden;
    5. width: 962px;
    6. }

    The primary navigation element above is styled with a set width, a background color, and a set height. The width is not that important, but it ensures that the element can be centered using margin: auto.

    Next, we need to style the individual

  • elements. Here’s the code:

    1. #navigation li {
    2. float: left;
    3. border-right: solid 1px #ca0002;
    4. height: 35px;
    5. }

    The height matches that of the

      element, and each
    • element is given a solid right border, the latter of which constitutes the first part of the beveled effect.

      Next are the anchors inside the

    • elements. Here’s the CSS:

      1. #navigation li a:link, #navigation li a:visited {
      2. text-decoration: none;
      3. display: block;
      4. height: 35px;
      5. color: #fff;
      6. line-height: 35px;
      7. padding: 0 9px 0 9px;
      8. border-right: solid 1px #990000;
      9. font-family: Arial, Helvetica, sans-serif;
      10. font-size: 12px;
      11. font-weight: bold;
      12. }

      The important things to note above, again, is the right border setting, which is the second ingredient for getting the beveled effect. Thus, the color of this border is darker than the border on the

    • element. The rest of the CSS shown above is trivial, making the buttons look similar to those on CNN’s website.

      So, although we can’t create a double border on a single element without images, we can get around that by taking advantage of the nested elements that are part of every navigation bar, the anchor and the list item.

      Next we give each button a hover state:

      1. #navigation li a:hover {
      2. background: #990000;
      3. color: #fff;
      4. }

      The result is missing just one element. Since each button has a double border on the right, that means the leftmost button will not have a border on the left. So, we add a class to the first

    • element to give that button a left border, which completes the look:

      1. #navigation li.first {
      2. border-left: solid 1px #ca0002;
      3. }

      It’s that simple. No need to slice up images in Photoshop; you can create a simple, yet beautiful beveled effect with some pretty straightforward CSS.

The Firefox Input Button Line-Height Bug

I recently ran into a bug in Firefox and Opera when I tried to set the line height of text inside a button (which effects input “submit” buttons as well as the HTML button tag). The bug? The line height can’t be changed!

For example, take the following code:

input#button {
border: 2px solid #06f;
color: #06f;
background-color: #6cf;
font: bold 12px Arial, Helvetica, sans-serif;
line-height: 50px;
}

In a perfect world, this code would be a quick and easy way to vertically center text inside a button and set the button’s height, since text is always centered inside of the space created by its line-height.

But the results are inconsistent. Chrome, Safari, and (I can’t believe this either) Internet Explorer 8 all center the text and resize the button just like I’d expect. But the results are less than perfect in Firefox and Opera (see the image above).
The Problem, Defined

A quick look at Firebug proves enlightening: even though I’ve specified a line-height of 50px, Firefox is using a line-height of 15px instead.

So what gives?

It seems our bug isn’t really a bug at all, but a “feature”: that is, it’s a deliberate decision by Firefox to limit line heights on buttons. This is evidenced by this line of CSS in Firefox’s default CSS:

button, input[type="reset"], input[type="button"], input[type="submit"] {
line-height:normal !important;
}

Basically, Firefox is setting the line-height to “normal” on buttons and is enforcing this decision with an !important declaration. This is a frustrating decision on their part, particularly considering (as Eric Meyer has pointed out at great and detailed length), line-height: normal is anything but.

And while trying to work around this rule, I discovered something that makes the situation a little more dire: browser-defined !important rules cannot be over-ruled by author-defined !important rules. This rule cannot be overruled by a CSS file, an inline style — anything.

So what’s to be done?

After a couple of hours of teeth-gnashing, I’ve settled on the following as an acceptable workaround. Instead of using line-height, use padding.

So to take the example from earlier, we’d convert it to look like this instead:

input#button {
border: 2px solid #06f;
color: #06f;
background-color: #6cf;
font: bold 12px Arial, Helvetica, sans-serif;
padding: 18px 6px;
}

This effectively centers the text inside our buttons, but it isn’t ideal. It means there’s no easy way to ensure our buttons are using the same line-height as the rest of our content, and it means the size of the button can’t be dependent upon the size of the button text. But I’ll take what I can get on this one.