Archive for the ‘jQuery’ Category

May 13

How To Zebra-Stripe Tables Using CSS and jQuery

Zebra Stripes DemoAs I was browsing my ridiculously large and disorganized RSS feeds a few days ago, I came across a very interesting article that discusses the “benefits” of using zebra-striping on tables. The article was titled Zebra Striping: Does it Really Help? and it was published on A List Apart. In short, the author of the article discusses the use of zebra-striping, its history, and the benefits which are associated with its use. Furthermore, she describes and attempts to make sense of a small experiment carried out by herself in order to test the “benefits” linked to zebra-striping. The article concludes with the author’s assesment of zebra-striping as valuable in terms of visual appeal but with little to no influence on usability. As a concluding statement she writes, “At the end of the day, the decision about whether to use zebra striping probably comes down to a subjective assessment of likely gains versus the cost of implementation.” So, let’s make the cost of implementation minimal by using some simple jQuery and CSS. Before we begin, here is a sneak peak at the final result.

jQuery Zebra Stripes Script

There are many ways to implement a zebra-striping script using jQuery, and I will demonstrate one of them. The following approach uses a function, which takes two (optional) parameter and creates a specified CSS class for all the even rows of a given table. However, if the function is called without any parameters all even rows on tables will be given the class “even-row.” Here is what the script looks like:

  1. $(document).ready(function()
  2. {
  3.     stripeTable();
  4. });
  5.  
  6. function stripeTable(table_name, class_name)
  7. {
  8.     var target;
  9.  
  10.     if(table_name && class_name)
  11.     {
  12.         target = table_name + " tr:even";
  13.         $(target).addClass(class_name);
  14.     }
  15.     else
  16.     {
  17.         target = "table tr:even";
  18.         $(target).addClass("even-row");
  19.     }
  20. }

Note: the above script is calling the function with no parameter and thus will reset to the default rules.

Add Some Style

Now that our script is complete and working, let’s add some color to the newly created class, “even-row.” Remember that this class name is given because we did not specify parameters when we called the function on the script above. So, here is what the “even-row” CSS for the demo looks like:

  1. #content table tr.even-row td
  2. {
  3.     color: #c76400;
  4.     background: #e6e6e6;
  5. }

Note: the ID content is targeting a div of the same name on the example file. Thus, when implementing your own CSS rules remove it.

Use, Installation, and Download

In case you overlooked the sneak preview link at the beginning of this post, take a look at the script in “action” by clicking this demo link. This script is free for your use or modification. However, you need to download and install jQuery for the zebra-striping to work. In other words, the of your document will have to include the following files and look something like this:

  1. <script type="text/javascript" src="[YOUR PATH]/jquery.js"></script>
  2. <script type="text/javascript" src="zebra_stripes.js"></script>

In any case, enough talking (writing), download the script and enjoy!

zebra_stripes.js

May 01

W3C Valid Buttons, A jQuery Approach

jQuery Script SolutionThere are many people who consider the W3C valid buttons as good indicators of a web designers’s understanding and attention to Web Standards. Although, this may not be entirely true, it is a reasonable and understandable way of thinking. Thus, almost every site built these days will display a valid link to the W3C’s validation app somewhere in the site (more often than not in the “footer” section). However, most designer will refrain from using the W3C provided valid buttons and will instead cook a textual or visual solution of their own. But wouldn’t it be nice if you could use these somewhat-iconic buttons only when needed? Wouldn’t it be nice if they were less obtrusive and had a place in your design? If these inquiries gave you a sense of excitement, take a look at the demo and read on.

A jQuery Approach

For those of you who are long time readers, it comes with no surprise that I have chosen jQuery for this tutorial. For those of you who are new, visit the jQuery homepage and prepare yourself to be amazed by this versatile and lightweight java framework.

There are two main problems with the W3c valid buttons, which prevent most designers from using them. Firstly, the color choices … rather, the lack of color choices for the buttons makes it hard for it to blend with any design. Secondly, unless the site being built is a W3C site, chances are the design will have a more 2.0 look. In other words, unless the design of your site is outdated, these buttons will clash with your current design.

My solution to these problems is rather simple (thanks to jQuery), hide the buttons unless needed. In other words, use jQuery to append the W3C button(s) to your validation link(s), position it above the link(s), and animate them. In order to have a better sense of this technique, take a look at the demo.

Ready To Use Script

If you wish to download the script, scroll to the end of this post and click on the download button. However, before you hurry away with a big smile in your face here are a few things you should know (AKA how-to):

  1. jQuery is needed for the script to work.
  2. You will need to add one of the following classes to your validation links:
    • valid_html
    • valid_xhtml
    • valid_css

Note: the .js file is set to work with the valid_xhtml and valid_css classes. If you wish to use the valid_html class instead of the valid_xhtml one, uncomment that line of code and viceversa. Also, if you are using this script with WordPress make sure to read my Using jQuery with Wordpress post.

Download & Copyright Things

The script is free for you to use, modify, or destroy (hopefully not the latter). But recognition or a link back is always well received. Please make sure to leave any thoughts or problems you have with the script. Enjoy!

Download Script

April 19

Absolute Positioning Using jQuery

jQuery TutorialUsing coordinates to place an abolutely postioned element on a page may seem like a bit of an overkill when you can just assign the top, bottom, right, and left values in the CSS but believe me is not. I have used this effect in combination with the Text-Resize Detection script published on A List Apart to re-position an element next to another at all times (the project demanded this effect) regardless of window or text resizing. The things you can do with this nifty trick are only limited by your imagination. Look at a demo of the “trick” at work if you wish to see it before reading the tutorial. Enough rumbling, let’s begin.

Gathering Neccesary Information

There are a couple of things we are going to need to know before beginning to write the script and they are:

  • The name (id or class) of the element we are trying to get the coordinates of, which I will call “static_box (id name)” during this tutorial.
  • And, the name (id or class) of the element we are moving around, which I will call “moving_box (id name)” during this tutorial.

Now we can begin recreating what you saw on the demo. First, let’s start by creating the functions we will need in order to position the moving box to either the left or right of the static box. In this step we will write the commands for the main function only. In other words, we will absolutely position the moving box and give it top and left values.

function position_left() { /* code will go here */ } function position_right() { /* code will go here */ } function position(x,y) { var moving_box = $("#moving_box"); moving_box.css("position","absolute"); moving_box.css("top",y); moving_box.css("left",x); }

Now we need to record the coordinates of the static box and apply them to the moving box. However the actual X and Y coordinates will need to change a bit if we want things to line up correctly. We need to take padding and border into consideration and make them part of our equation. So with a bit of addition and subtraction involved in the equation, our empty functions should look something like this.

function position_left() { var static_box = $("#static_box"); var moving_box = $("#moving_box"); var mb_width = moving_box.width(); var coordinates = static_box.offset() var x = coordinates.left - mb_width; var y = coordinates.top; position(x,y); } function position_right() { var static_box = $("#static_box"); var sb_width = static_box.width() + ["static_box total padding + total border"]; var coordinates = static_box.offset() var x = coordinates.left + sb_width; var y = coordinates.top; position(x,y); } function position(x,y) { var moving_box = $("#moving_box"); moving_box.css("position","absolute"); moving_box.css("top",y); moving_box.css("left",x); }

That is all the code you need to recreate what you saw on the demo. However, you will need to call either function position_right or position_left for things to work. In the case of the demo, the functions are being called by an “onclick” function attached to the link elements.

Download

Please feel free to play around with the code and make any tweaks to it. If you have any questions or something is not working right for you, leave a comment and I will try to answer your question(s).

Download Demo