JQuery UI and JQTouch

On a current project I wanted to use the JQuery UI Slider in a JQTouch project. Turns out that Mobile Safari rewires all of the standard JS events. Think about it. A standard mouseDown event on mobile device has more implications. Did a user want to drag an element? Did they want to scroll the page? Did they want want to pinch zoom? It’s hard for mobile Safari to tell. From a quick look at the Apple Developer site it appears that Mobile Safari “flattens” all the mouse events into a single action. When you tap and lift your finger mobile Safari fires mouseOver, mouseMove, mouseDown, mouseUp and click. At this point you’ve lifted your finger and your drag action isn’t really going to work.

After a search I ran across a few fixes for this. Here’s a link to one

http://code.google.com/p/jquery-ui-for-ipad-and-iphone/

This seemed to work well, though it had the problem of also nixing touch scrolling in my project. Since all of the mouse events were now rewired Mobile Safari was not getting the events it needed to scroll with a swipe.

Looking under issues on the Google Project site I found a few people had asked about this. The author had posted a fix which seemed to work well.

Using the :nth-child selector

Ever want to select only the first element in a list? What about the last element? Or how about odd or even element? What about every 3rd element? Or, what about selecting the first 3 elements or the last 3 elements?

Of course you do, and if you haven’t wanted to do this in the past you will want to do in the future at some point, probably sooner than you think.

You can approach the problem with classes, by adding a special class to say the first three elements or every other element. But this becomes very tedious and lacks flexibility. A better solution would be a CSS selector. Well here it is :nth-child(). The :nth-child() selector allows you to accomplish the following.

Imagine for these examples that you have a page with 9 divs, each with the class name box. To select all of these you’d use: .box I’ll modify this selection with :nth-child() in the examples below to illustrate how it works.

  1. Pick any single element – .box:nth-child(3) /* Selects the 3rd element */
  2. Pick out only the odd elements – .box:nth-child(odd) /* Selects all odd numbered elements */
  3. Pick out only the even numbered elements – .box:nth-child(even) /* Select all even numbered elements */

These first three examples use :nth-child() in it’s most basic an obvious form. Include the integer position of the element or a key word, odd or even.

Here’s a few more examples.

  1. Pick every 3rd element for example 3, 6, 9 etc. – .box:nth-child(3n)
  2. Pick every 5th element for example 5, 10, 15 etc. – .box:nth-child(5n)

Here you can think of n as an integer 1, 2, 3 etc. multiplied by the number on the left. So 3n would be 3, 6, 9 etc. There’s more, how these examples.

  1. Pick every third element starting with the first. For example boxes 1, 4, 7, 10 etc. – .box:nth-child(3n+1)
  2. Pick every third element starting on 5, for example: 5, 8, 11, etc. – .box:nth-child(3n+5)
  3. Pick every element after the 4th: 5, 6, 7, 8 etc. – .box:nth-child(n+5)

You can think of the last example: .box:nth-child(n+5) as .box:nth-child(1n+5). Which might make more sense.

Here we’re using an+b. Where elements are selected as a*n+b. Which works out as the following for 3n+5

  • (3*1)+1 = 4
  • (3*2)+1 = 7
  • (3*3)+1 = 10
  • etc.

The last example above .box:nth-child(n+5) would select all of the elements after the first 4. To select only the first 4 elements you can use :nth-child() in this way:

  1. Pick the first 4 elements, for example: 1, 2, 3 and 4. – .box:nth-child(-n+4)

Think of this as:

  • (-1*0) + 4 = 4
  • (-1*1) + 4 = 3
  • (-1*2) + 4 = 2
  • (-1*3) + 4 = 1

An alternative to :nth-child() is :nth-last-child(). Which works in the same way, but starts counting backwards from the last element.

What could you use this for? Here are a few ideas:

  1. Selecting every other item in a list.
  2. Selecting the first item in a list.
  3. Selecting the first three items in a list.
  4. Setting the margins on the last item in every row.
  5. Selecting the first row of elements.
  6. Selecting the last row of elements.

 

LESS is it more?

Someone just brought LESS to my attention. LESS is a Javascript library that extends the functionality of CSS. LESS effectively adds variables and functions to your CSS. Usng LESS you can define variables and apply mathematical functions to your values in your stylesheets.

For more information read more on their web page: LESS.org

JQuery: .css()

The JQuery .css() method provides a function to set CSS properties and values for any element. The .css() method will also retrieve the value of any CSS poperty for any element.

To set a CSS property use of the of the following methods.

.css(property,value);

Using this system set the property as the first parameter and the value as the second. Both parameters should strings. Use the standard CSS property names, the same as used in your CSS style sheet. For example:

$('p').css('color', '#F00');

The line above would find all p tags on a page and set the color to red.

Sometimes you may want to set multiple properties at once. In these cases use this syntax.

.css({prop:value,prop:vlaue,...});

In this case pass one parameter, an object containing all of the properties and values to be set. Since property names set in the object can not contain special characters you’ll need to alternate property names. For example normally you would use background-color in your CSS style sheet. When using an object property list the – (hyphen) is not allowed. Instead use: backgroundColor. Notice I removed the unacceptable character and began the next word with an uppercase letter. For example:

$('p').css({lineHeight:'18px',letterSpacing:'0.1em'});

The example above sets the line height and letter spacing of all p elements.

Alternately, the CSS property name can be used as long as it is wrapped in quotes. For example the line above could alternatively be written as:

$('p').css({'line-height':'18px','letter-spacing':'0.1em'});

 

The value of any property may also be retrieved. For example, imagine you wanted the font-size of one element to match the font-size of another.

$('li').css('font-size', $('#headline').css('font-size') );

The line above sets the font size of all li elements to the same font-size as that of the element with the id name #headline.

JQuery: Getting Started

JQuery is a great tool for web development. It’s a Javascript framework that makes working with Javascript much easier. JQuery makes it easier to write code that works across platforms. JQuery simplifies all of the common Javascripts that may need to perform.

Download the JQuery library from Jquery.com. They archive different versions and provide a compressed (or minified) version and an un-compressed version. You can download the latest compressed version from the JQuery home page and it will work with all of the examples. Move this file into the directory where you will be working.

You can obviously place the jquery js file anywhere, as long as you provide the correct path to the file in your HTML document.

JQuery is very easy to use. Follow these steps:

  1. Import the JQuery js file with the script tag
  2. Set up another script tag for your own javascript.

For example:

<script type="text/javascript" src='jquery-1.4.4.min.js'></script>
<script type="text/javascript">
   // your own JS here
</script>

At this point you can use JQuery in your scripts. Notice, your scripts come second.

Many times you will want to wait for your document to load before applying your own JS. This important, because executing JS before the elements it acts on are loaded will cause your scripts to fail. JQuery provides the ready() event to easily work around this.

To set up the ready event use the following:

<script type="text/javascript" src='jquery-1.4.4.min.js'></script>
<script type="text/javascript">
$(document).ready(function(){
   // Scripts here execute after the page has loaded.
});
</script>

CSS Position

Here are a few examples showing the use of the CSS absolute and relative position properties.

A key feature when using position:absolute is when the parent is position:relative the absolute child element can be positioned within the coordinate space of the parent. Effectively this means you can position the child by setting it’s left and top properties. The child element will position from it’s upper left corner relative to the upper left corner of the parent.

For example setting the child to left:0 and top:0 would place the child object so that it’s upper left corner was aligned with the upper corner of the parent.

Position

Full Page Background cover

The background-size property determines how a background image fills an area. The size and method used to fill the space has several options. Here’s an example that fills the background with an image, resizing the image to fill the space.

Background-size example

It may take a moment to load the background image. After the background loads resize the browser window. Notice the image always fills the space.

opacity vs rgba

This transparency thing is very useful, esoecially to desginers. There are two methods for setting the transparency of an element with your stylesheet.

First use the opacity property. For example imagine you have a paragraph. The following would make the paragraph transparent. This would include the entire element, text and background.

p {
opacity:0.5;
}

The other method is to assign an element an rgba color value. Any element that can be assigned a color value using a hex color like #FF0000, can also be assigned a color value with rgba. For example:

p{
color:rgba(255,0,0,0.5);
}

In this example the color of the text would red 50% transparent. The first three values, with ranges of 0 to 255, represent the amount of red, green and blue. The last value, with a range of 0 to 1, represents the amount of transparency.

Using rgba you can transparency any feature of an element.

The difference between the two is, using opacity will take the element with it’s current rendering and display it transparent at the opacity value. rgba on the other hand selts the color value of any feature of an element to include a transparency level.