Activate your free membership today | Log-in

Monday, September 1st, 2008

CSS Sprites2: Return of the JS

Category: CSS, jQuery

In March 2004, Dave Shea wrote about CSS Sprites, and now he is back with CSS Sprites 2. He walks us through using JavaScript to make this all work nicely, and picks jQuery to get 'er done:

After putting this together piece by piece, we end up with:

JAVASCRIPT:
  1.  
  2. $(document).ready(function(){
  3.  
  4.         // remove link background images since we're re-doing the hover interaction below
  5.         // (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
  6.         // we also want to only remove the image on non-selected nav items, so this is a bit more complicated
  7.         $(".nav").children("li").each(function() {
  8.                 var current = "nav current-" + ($(this).attr("class"));
  9.                 var parentClass = $(".nav").attr("class");
  10.                 if (parentClass != current) {
  11.                         $(this).children("a").css({backgroundImage:"none"});
  12.                 }
  13.         });     
  14.  
  15.  
  16.         // create events for each nav item
  17.         attachNavEvents(".nav", "home");
  18.         attachNavEvents(".nav", "about");
  19.         attachNavEvents(".nav", "services");
  20.         attachNavEvents(".nav", "contact");
  21.  
  22.  
  23.         function attachNavEvents(parent, myClass) {
  24.                 $(parent + " ." + myClass).mouseover(function() {
  25.                         $(this).append('<div class="nav-' + myClass + '"></div>');
  26.                         $("div.nav-" + myClass).css({display:"none"}).fadeIn(200);
  27.                 }).mouseout(function() {
  28.                         $("div.nav-" + myClass).fadeOut(200, function() {
  29.                                 $(this).remove();
  30.                         });
  31.                 }).mousedown(function() {
  32.                         $("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
  33.                 }).mouseup(function() {
  34.                         $("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
  35.                 });
  36.         }
  37. });
  38.  

Posted by Dion Almaer at 6:41 am
3 Comments

+++--
3.8 rating from 20 votes

Friday, August 29th, 2008

jQuery.com redesigned with a Rock Star

Category: jQuery

When I was doing an interview with John Resig for the Open Web Podcast, he mentioned that the redesign of jQuery.com had a lot of people talking, and it seems like people have strong feelings about the Rock Star for whatever reason.

Ignoring the style, the redesign is more than just that:

The entirety of the site has a new layout. With drastically improved multi-layer navigation and a standardized sidebar it should become much easier to navigate the individual portions of the site.
You should probably wear a hard hat while exploring the interior pages - font sizes, spacing, and colors are all in need of tweaking, which will be handled over the upcoming week (it’s fun working against Trac, Wordpress, Drupal, and Mediawiki simultaneously).

Posted by Dion Almaer at 2:30 pm
26 Comments

++---
2.6 rating from 67 votes

Degrading script tags for fun and profit

Category: JavaScript, jQuery

John Resig posted on degrading script tags and adding functionality to <script> so you can add a src attribute and a body of code that will be executed one the external script loaded error free:

JAVASCRIPT:
  1.  
  2. <script src="some-lib.js">
  3.   var foo = use_some_lib();
  4.   foo.do.stuff();
  5. </script>
  6.  

To make this all work, John shows us a jquery aware version that detects loading and such:

JAVASCRIPT:
  1.  
  2.  
  3. (function(){
  4. var scripts = document.getElementsByTagName("script");
  5. var curScript = scripts[ scripts.length - 1 ];
  6.  
  7. if ( curScript.executed )
  8.   return;
  9.  
  10. // ... jQuery ...
  11.  
  12. curScript.executed = true;
  13. var script = curScript.innerHTML;
  14. if ( script ) {
  15.   jQuery(document).ready(function(){
  16.     jQuery.globalEval( script );
  17.   });
  18. }
  19. })();
  20.  

Where this kicks in, is more than just saving a script code block. Steve Souders has been researching different ways to tie blocks of code to scripts and having the optimal ordering for performance. We can tie this learning to John's work, and the developer can choose what they want to do (e.g. when loading don't have the icon moving etc etc).

Posted by Dion Almaer at 8:26 am
3 Comments

+++--
3.1 rating from 20 votes

The Ajax Experience Framework Summit

Category: Dojo, Prototype, The Ajax Experience, jQuery

We talked a few months ago about something new we're doing at the Ajax Experience this year: the "Framework Summit." Basically, we're providing space for Prototype, jQuery, and Dojo to hold their own half-day events on-site, and these events are free and open to the general public.

Since we announced the summit, the frameworks have created their agendas for their events for their events:

Dojo Developer Day:

- Welcome, Introductions (Alex Russell, Dylan Schiemann)
- Tutorial - Progressive Dojo (Peter Higgins)
- Presentation - DojoX GFX and FX (Matthew Russell)
- Presentation - Secrets of DojoX (Tom Trenka)
- Tutorial - Getting going ... Zend + Dojo (Matthew O'Phinney)
- Tutorial - Dijit Layouts In and Out (Nikolai Onken)
- Tutorial - Reusable code, Widgeting (Peter Higgins)
- Community - Getting Involved (Peter Higgins, Nikolai Onken)
- Lightning Demos - What do you have? Show us.

Prototype Developer Day

- Welcome, Introductions (Prototype Core members)
- Contributing docs (Christophe Porteneuve)
- Contributing code (Andrew Dupont)
- Prototype & Performance
- Extended Q&A (Prototype Core members)

jQuery Developer Day

- State of jQuery (Rey Bango)
- Progressively Enhancing the User Experience Using jQuery (Karl Swedberg)
- An In-Depth Look at jQuery UI (Paul Bakaus)
- jQuery Team Code Review (jQuery Team)

Other Frameworks?

Some have asked why we didn't also include Framework X, Y, and Z at the summit. We have a simple answer: we only had room for three frameworks so we choose the three most popular frameworks (according to every survey we've seen). If the concept is successful this time around, we hope to do it on a bigger scale next year.

Obviously we hope you can make it to the entire Ajax Experience event, but if you can't do that, consider coming to one of the Framework Summit events. See you there!

Posted by Ben Galbraith at 7:00 am
9 Comments

++---
2.6 rating from 22 votes

Tuesday, August 26th, 2008

jParallax Turns Elements into a Viewport

Category: jQuery

In the "oh wow, I didn't know JavaScript could do that" category, I just came across a cool new jQuery plugin called jParallax which implements a parallax effect on selected elements. Now, I'm not ashamed to admit not knowing what "parallax" meant so I looked it up on Wikipedia which totally added closure to the concept being implemented. Till then, I just thought that was a really cool effect!

Parallax turns a selected element into a 'window', or viewport, and all its children into absolutely positioned layers that can be seen through the viewport. These layers move in response to the mouse, and, depending on their dimensions (and options for layer initialisation), they move by different amounts, in a parallaxy kind of way.


jParallax includes several options for controlling the effect including setting the animation timing and layer positioning control. The demos can be viewed here.

We also covered Brett Taylor's implementation of a parallax effect last year where he showed how to build parallax backgrounds.

Update: I've added straight links to the demos

Demo Links

Posted by Rey Bango at 9:36 am
14 Comments

++++-
4.3 rating from 20 votes

Thursday, August 21st, 2008

Chain.js: jQuery Data Binding Service

Category: jQuery

Rizqi Ahmad has created a data binding service for jQuery called Chain.js.

A simple example shows you where to start. When given HTML like:

HTML:
  1.  
  2. <div id="quickdemo">
  3.     <div class="item"><span class="library">Library Name</span></div>
  4. </div>
  5.  

The following JavaScript will add data as items to the list:

JAVASCRIPT:
  1.  
  2. $('#quickdemo')
  3.     .items([
  4.         {library:'Prototype'},
  5.         {library:'jQuery'},
  6.         {library:'Dojo'},
  7.         {library:'MooTools'}
  8.     ])
  9.     .chain();
  10.  

Check out the demos for more detailed examples.

Posted by Dion Almaer at 1:43 am
9 Comments

++++-
4.5 rating from 32 votes

Monday, August 18th, 2008

Transformie: Implement WebKit CSS transforms in IE

Category: CSS, IE, jQuery

Paul Bakaus, or jQuery UI fame, has created a nice little hack to implement WebKit CSS transforms in IE

When you include the library, it can scan for your -webkit-transform-* transforms (soon to support the standard transform-*) and will go to work for you using a couple of nifty technologies all put together:

  • IE Filters such as DXImageTransform.Microsoft.Matrix that do the rotate, skew, scale, and general matrix work for you
  • onpropertychange "almost behaves like DOMAttrChanged, but is much finer grained. It is capable of telling you whenever a DOM property changes on an element, and when you track the style attribute, it actually passes the actual style that changed along with the event." Here it is at work in the library:
    JAVASCRIPT:
    1.  
    2. jQuery(Transformie.trackChangesFor).bind('propertychange', function(e) {
    3.         if(e.originalEvent.propertyName == 'style.webkitTransform') {
    4.                 Transformie.refreshMatrix(this);       
    5.         }                     
    6. });
    7.  

From there you can see the transforms which look like:

JAVASCRIPT:
  1.  
  2. // rotate
  3. matrices.push($M([
  4.         [Math.cos(a),   -Math.sin(a),      0],
  5.         [Math.sin(a),   Math.cos(a),       0],
  6.         [0,     0,   1]
  7. ]));
  8.  

Very nice indeed.

Posted by Dion Almaer at 7:43 am
8 Comments

+++--
3.7 rating from 29 votes

A simple solution to the “other” problem with select boxes

Category: Examples, JavaScript, Tip, jQuery

Jeffrey Olchovy has posted a simple tutorial on using jQuery to solve a "select-to-input toggle" that shows and hides a text field when you select "Other". It overloads the same form name, so the server side gets just one value, and doesn't know or care if it was in the drop down or typed in. You can try it live here.

This is a simple little solution to the issue that there isn't a native control to really do the job. What you really probably want here is the ability to drop down and select items, or just type into the select box field itself. This is one reason why people implement auto-complete text fields instead of using select boxes for this case, but wouldn't it be nice to be able to tag your <select> and be done?

Posted by Dion Almaer at 5:51 am
10 Comments

+++--
3.2 rating from 31 votes

Wednesday, July 30th, 2008

Inline Script Wrapper and Dependencies

Category: Performance, jQuery

Stuart Colville has found an issue where he needed to output some JavaScript in the middle of a page, before a library that depended on it was available:

The 6th Rule in Yahoo’s Performance Rules recommends placing script before the closing body tag to prevent blocking holding up the rendering of the page’s content. This works well but there are times where script needs to be output higher up in the page than it’s dependencies.

In this example I’m using jQuery but feel free to substitute jQuery for the your favorite framework.

The requirement is that there’s a need to run some code that would ideally use jQuery somewhere in the middle of the page. I could avoid the dependency and re-write everything without jQuery and for simple scripts this can be a good way to go. But, if I want to use some of the more complex jQuery features, then I really don’t want to have to re-invent the wheel or resort to including jQuery in the head of the document.

This lead him to the following example

HTML:
  1.  
  2.     <script type="text/javascript">
  3.         var muffin = muffin || {};
  4.         muffin.inline = muffin.inline || [];
  5.         muffin.inline.add = function(f){
  6.            muffin.inline[muffin.inline.length] = f;
  7.         };
  8.     </script>
  9.  
  10.     <script type="text/javascript">   
  11.         muffin.inline.add(function(){
  12.             $('#green')[0].style.backgroundColor = 'green';
  13.         });
  14.         muffin.inline.add(function(){
  15.              $('#red')[0].style.backgroundColor = 'red';
  16.         });
  17.     </script>
  18.  
  19.     <div id="red"><p>This should be Red</p></div>
  20.     <div id="green"><p>This should be Green</p></div>
  21.  
  22.     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
  23.     <script type="text/javascript">
  24.         $(function(){
  25.             if (muffin && muffin.inline){
  26.                 for (var i=0, j=muffin.inline.length; i<j ; i++){
  27.                     muffin.inline[i]();
  28.                 }
  29.             }
  30.         });
  31.     </script>
  32.  

This seems a little niche. You my run into this as you have server side components outputting things, but ideally you can fix that in your architecture and ship the JavaScript in the correct location.

Posted by Dion Almaer at 5:42 am
5 Comments

+----
1.8 rating from 6 votes

Monday, July 21st, 2008

jQuery: Sparklines Plug-in

Category: Plugins, jQuery

Edward Tufte has long had a following of fans in the field of information visualizations. Among his interesting taxonomy of visualization types is the "Sparkline", which he describes as "data-intense, design-simple, word-sized graphics".

While Tufte originally suggested that computer displays are too low-resolution to effectively make use of Sparklines (vs. printed page), James Dempster pointed us to some work the folks at Splunk have done to join a long line of folks who have given it a go anyway.

The resulting jQuery plug-in is really nice. Now if only they had the ability to overlay two line graphs over the same area using a transparent fill... ;-)

(Oh, and there's also a simple Sparkline generator for Google Charts over at style.org.)

Posted by Ben Galbraith at 8:21 am
9 Comments