Put your message here! Contact me for more information
 
 







 

Archive for October, 2006


 

I just recently completed a demo for a client of mine. The demo was a news portal with lots of links to external news contents. I wanted to spice things up a little, so I implemented a inline link-tip (or inline popup, however, this name may confuse you with the light-box type popup) using the [[http://developer.yahoo.com/yui/|Yahoo! User Interface Library]] (YUI). Thanks to YUI, the effect is now quite easy to be implemented.

Here’s a screencast of what the inline link-top looks like

{{ http://alexle.net/wp-content/uploads/2006/10/inline.gif }}

(If you want to see the actualy demo page or get the sample code, please scroll down to the end of the post)

===== The setup =====
Here is the XHTML-compliant markup code for the content and the hover.

www.AlexLe.net has a few home-writen tutorials.

Link 1 to test if the hover is hidden properly.

www.VentureBeat.com
the blog of Matt Marshall
tracking Sillicon Valley,VC, and Startup


Basically we have a few DIVs, positioned either inline or absolutely to show how the inline link-tip works in both cases. Inside each container div, we have a link with a className of **articleTitle** and an extra attribute called **hoverId**. This extra **hoverId** attribute specifies the ID of the popup that we’d like to associate with this link.

Next, we setup the hover container div. The popup div has the className of ‘inlinePopup’ so that we can style it with CSS later on. Let’s take the div with Id ‘hover1′ as our example.

...




For this technique to work, we have to explicitly add the **style=”display:none;”** for the popup div. Setting display:none in the inlinePopup CSS class definition will not work.

For the CSS styling, we can just add these rules to the page to make things a little nicer.

===== The Javascript =====
Here is the javascript to handle the inline popup/ tooltip. The idea is after the page has been rendered properly (window onLoad is fired), we scan through the DOM, look for any link with the className of **articleTitle**, then attach custom events for onMouseOver and onMouseOut to handle the display of the popup. And thanks to YUI, adding custom events and web2.0 animations is easier than ever.

First of all, we include the YUI’s library to our page









Then we insert some javascript codes. Let’s start with the pageInit() function first.
pageInit = function() {
/* Popup summary */
/* We find all items with the articleTitle class */
var els = YAHOO.util.Dom.getElementsByClassName('articleTitle', 'a');
for( var i=0; i < els.length; i++ )
{
/* Get the hover div */
sDivId = els[i].getAttribute('hoverId');
sDiv = document.getElementById( sDivId );
YAHOO.util.Dom.setStyle( sDiv , 'opacity', '0');

/* Unhide the Summary layer when mouse over/ out*/
YAHOO.util.Event.addListener( els[i], "mouseover", hoverOver );
YAHOO.util.Event.addListener( els[i], "mouseout", hoverOut );
}
}
YAHOO.util.Event.addListener( window, "load", pageInit );

We basically find all **a** (anchor) elements in the page with the className of **articleTitle**. Then we loop through the result **els** array and look for which popup div is associated with this link. As we stored the popup div’s ID in the hoverId attribute of the link, we can just grab the value by using the getAttribute(’hoverId’). And next, we set the opacity of the popup div to 0. With YUI, we don’t have to worry about the different syntax of different browsers. Only one single line of code to set the opacity! Next, we add our custom functions to the **a** element to handle the **onMouserOver** and **onMouserOut** events with the **YAHOO.util.Event.addListener( element, “eventType”, customHandler )**

==== Custom Event Handlers ====
We can now implement our **hoverOver()** and **hoverOut()** handlers. Let’s start with **hoverOut()**

/* This function is to unhide the div, then do a smooth very web2.0 fade-in effect */
function hoverOver()
{
var sDivId = this.getAttribute('hoverId');
var sDiv = document.getElementById( sDivId );

sDiv.style.left = YAHOO.util.Dom.getX( this ) +'px';
sDiv.style.top = (YAHOO.util.Dom.getY( this ) + this.offsetHeight + 2 ) +'px';
sDiv.style.display = '';
var animIn = new YAHOO.util.Anim( sDiv, { opacity: { to: 1 } }, 0.25, YAHOO.util.Easing.easeOut);
animIn.animate();
}

When the mouse hovers over the link, this hoverOver() function is triggered. However, at this very moment, we find out that we don’t have any reference to targetted popup div, so we go grab the popup div’s id from the hoverId attribute of the a element. Luckily, we can just reference to the a element as **this**.

=== Positioning the Popup & Adding Dramatic fading effect ===
As our links can be rendered anywhere in the screen, we have to know where to put our popup div. The solution is to set the position of the popup div to the coordinates of the link. With YUI, it’s quite easy: **YAHOO.util.Dom.getX( this )** and **YAHOO.util.Dom.getY( this )** for the x and y values of the a element. But this will give us the top left corner of the boundary box of the link element, which means our popup would be placed **on top** of the link. So we add the offsetHeight of the a element to the top (Y) value of the popup, plus a few extra pixels as the ‘margin-top’. Now our div is positioned correctly. With this approach, we can place our link anywhere: whether it’s in a absolutely positioned container, or inline within a paragraph, the popup div is still positioned nicely below it.

That isn’t t too bad, is it? Now let’s add the fade-in effect. As we already set the opacity of the popup div to 0 when the page is loaded, we can just use the animation feature of YUI for the dramatically fade-in.

...
var animIn = new YAHOO.util.Anim( sDiv, { opacity: { to: 1 } }, 0.25, YAHOO.util.Easing.easeOut);
animIn.animate();
...

I use 0.25 second for the popup div to appear as I think anything longer will be quite annoying to the end-users (”whoever designed this dang page is wasting my time here”). Let’s keep our visitors happy. The YAHOO.util.Easing.easeOut is added so that the animation is eased out for a smoother appearance of our popup div.

Next, we added the **hoverOut()** handler to hide the popup div with an equally nice fade-out effect. But for **hoverOut()** to work correctly, we have to do some extra work towards the end. Here’s the code:


/* This function is to hide the div with a dramatic web2.0 fade effect */
function hoverOut()
{
var sDivId = this.getAttribute('hoverId');
var sDiv = document.getElementById( sDivId );
var animOut = new YAHOO.util.Anim( sDiv, { opacity: { to: 0 } }, 0.25, YAHOO.util.Easing.easeOut);
animOut.animate();
animOut.onComplete.subscribe( hoverHide );
}

Again, we get the reference to the popup div, then we fade it out by changing its opacity to 0. At this point, eventhough the user won’t see the popup div anymore, it’s not actually hidden. This means that the popup div is still covering up the space underneath it, thus link or clickable elements underneath won’t work (not clickable). We have to explicitly set the display of the popup div to none when the fade-out animation is finished to remove it from the page. We do this by using the **animOut.onComplete.subscribe()** to hand a callback method to the animation object. Our callback method will be called when the animation is completed and hide the popup div.

Here’s the one-line code for the hoverHide() callback function

/* Call back function used in hoverOut() */
function hoverHide()
{
this.getEl().style.display = 'none';
}

The **this** keyword now is interpreted as the animation object, not the link item. We use this.getEl() to get the reference to the targetted hover div, and set its style.display to ‘none’. Now everything’s working the way it’s supposed to. Yay!

===== Final Remarks =====
Thanks to YUI, the code is truely multi-platform compatible. All the event handlings are done through the YAHOO.util.Event so we don’t have to deal with messy browser sniffing and special cases. The YAHOO animation is simple and flexible enough for us to customize the way we want. The inline popup is also robust enough as we can style it with CSS like adding some neat drop-shadow and round-corner effects. As you can see from the screencast above, the inline popup is working correctly in the extreme case of rapidly hovering in and out. Nobody would do anything that crazy in real life but it’s nice to see how the effect behaves in extreme cases. And I think one important feature of this technique is that we can implement it to any existing page with minimal code editing.

I hope you enjoy this small tutorial!

Cheers!

===== Code & Sample Page =====
You can download the sample code of this tutorial here.
* [[http://alexle.net/wp-content/uploads/2006/10/inlinePopup.zip|Inline Popup Source File]]

The same code can be accessed here
* [[http://alexle.net/wp-content/uploads/2006/10/inlinePopup/|Demo Page]]

view comments
 

KT is THE best site for any heavy metal lovers. Period. The members are very active and there are always a boatload of new releases. I don’t want to talk about how torrents are killing the music industry and etc. (that’s just noise to me - yeah, big fat CEOs of huge labels are getting the big cut from the CD sales, so the money don’t go directly to the artists anyway. The RIAA is just a gang of highly greedy labels who are trying to go after small unorganized regular web users. Have you ever heard any CEO of any recording labels going broke? Exactly!)

Thanks to torrents, I am able to expose myself to amazing bands all over the world. And true fans are willing to shell out $15, $20 bucks for a CD, or even spend $30, $40 to buy the t-shirt of the bands. That money will actually go and support the band.

So many good band and musicians are putting out albums this time of the year. I was able to get the newest Angra’s album, Aurora Consurgens, the new Tarot’s album, Crows Fly Black, Dragonland’s Astronomy, and tons of other amazing bands. Angra’s newest album is a solid work power metal. The songs are vey well written and produced. Of the 10 songs on the CD, 5 of them get my 5 stars rating in iTunes, and the rest are also as good. The guitar work of Kiko Loureiro is fast, efficient, smooth, and crystal-clear. Actually I didn’t know that Kiko is from Angra. I already knew him from his excellent solo album No Gravity (#8, Moment of Truth, is an amzing song, btw), but I didn’t read his bio. When I listened to the song #9, Scream Your Heart Out, on Aurora Consurgens, I noticed the familiar playing style and the clear distortion settings of Kiko’s guitar, I looked up the Angra’s band and voila, Kiko is the lead guitarist of the band. Here is the song list and my ratings

  1. The Voice Commanding You (4/5)
  2. Ego Panted Grey (5/5): I love the deep expressive vocal part, and especially the amazing guitar bridging riff. The song start out slow, with only vocal,bass, and acoustic guitar. It then explodes with the full band coming in, then the amazing bridging riff that I just mentioned. The vocal remains as expressive and emtional as before. In the middle of the song, Kiko shows his amazing talents with the guitar solo while the symphony plays richly in the backgroud to support the him. And I have no comments on the percussion work except praises. This song alone is worth the money already.
  3. Breaking Ties: A little too 80’s (the chorus part), but still very enjoyable. The vocal reminds me of Dream Theater.
  4. Salvation Suicide: (5/5) Fast tempo, highly technical guitar work.
  5. Window To Nowhere: (4/5) Another good song, especially the beginning.
  6. So Near So Far (5/5): a 7 minutes song! This is rare nowadays as most songs are about 3:30mins to 4:00mins maximum. Middle Eastern influence beginning - love it! The song comes in slow with delicate instruments in the background, and the guitar part comes in about 1:20 into the song, still with the middle eastern influence. The vocal is high and strong. Around 4:30, it’s Kiko’s showtime. His solo wins my 5 stars rating. The middle-eastern acoustic solo around 5:15 is equally beautiful. Kiko’s guitar work is very intricate. It’s highly technical by being fast, accurate, but at the same time, very expressive, emotional, and melodic. The song ends with the guitars looping and nicely fade out. What can I say? Amazing
  7. Passing By (4/5)
  8. Scream Your Heart Out (5/5): The song already starts out with highly melodic Kiko’s guitar solo. The guitar riff just before the vocal comes it is awesome. I have to admit that the percussions of all the songs in this album are very well written and performed. Together with Kiko, the percussions give the songs the necessary punch and power. The song doesn’t even build up,it erupts.
  9. Abandoned Fate (4/5) A slow song to conclude the excellent album

I highly recommend this new Angra’s album to power metal fans. I haven’t listened much to previous Angra’s albums so I can’t compare too much the the new album against the older ones. Anyway, this album is so worth your listening.

view comments
 

For those of us who register alot of domains
* OYH1: 10% off any order
* OYH2: 5$ off any order of 30$ or more
* OYH3: 6.95$ domain name registration
(found from inoveryourhead.net)

I just used the OYH3 to save myself 2 bucks. Cheers!

view comments