http://tutorialzine.com
Created by Martin Angelov on Apr 21st, 2010
By now, you’ve probably heard about Adobe’s new CS5 software pack. Also, you’ve probably seen their
product pages, where they present the new features of the suite. Apart from the great design, they’ve also implemented an interesting solution for showcasing the new features their products are capable of – using contextual slideout tips.
Knowing the importance of HTML standards, we are making a set of contextual slideout tips with jQuery & CSS3, which are ideal for product pages and online tours. As a bonus, they are SEO friendly, so all the content is visible to search engines.
The idea
The main idea is to create an easily configurable set of contextual slideouts. Each can be opened in one of four directions –
bottom-right (default),
bottom-left,
top-left and
top-right, and each can be in one of three colors –
green (default),
blue, and
red.
To create a slideout element, you need to include a regular paragraph p tag to the page. This means that all the content is visible to search engines in a semantic way. The paragraphs are replaced with the markup for the slideouts by jQuery on page load, with the title, class and style attributes of the paragraph passed to the newly created element.
Contextual Slideouts
Step 1 – XHTML
Now lets take a look at the structure of the paragraph tags you should add to the page, and how they are configured.
demo.html
03 | < p title = "A New Tutorial Every Week" style = "top:200px;left:120px;" > |
04 | This slideout is going to open to the bottom-right (the default). |
07 | < p title = "2200+ Twitter Followers" class = "openTop openLeft blue" style = "top:400px;left:650px;" > |
08 | This slideout is going to open to the top-left. |
11 | < p title = "Over 5000 RSS Subscribers" class = "openTop red" style = "top:500px;left:90px;" > |
12 | This slideout is going to open to the top-right. |
As you can see, each of the tags contains a
style, a
class (optional) and a
title attribute. As discussed above, these are copied to the slideouts when jQuery replaces the markup.
The
style attribute contains the coordinates relative to the parent div element, which means that the slideouts are positioned in exactly the same place as the paragraphs.
The
class attribute is optional and specifies a number of options for the slideouts. You can choose the direction to which the slideouts open, and their color.
slideout markup
01 | < div class = "slideOutTip openLeft blue" style = "left:100px;top:200px" > |
03 | < div class = "tipVisible" > |
04 | < div class = "tipIcon" >< div class = "plusIcon" >div >div > |
05 | < p class = "tipTitle" >The title of the slideoutp > |
08 | < div class = "slideOutContent" > |
Structure of the Slideout
Step 2 – CSS
Now lets take a closer look at the styling. Only the styles directly used by the slideouts are presented here. You can see the rest of the styles in
styles.css in the download archive.
styles.css – Part 1
07 | background-color : #111 ; |
16 | -moz-box-shadow: 0 0 1px #999 ; |
17 | -webkit-box-shadow: 0 0 1px #999 ; |
18 | box-shadow: 0 0 1px #999 ; |
22 | .tipVisible{ cursor : pointer ; height : 22px ; } |
26 | font-family : 'Myriad Pro' , Arial , Helvetica , sans-serif ; |
38 | background-color : #61b035 ; |
39 | border : 1px solid #70c244 ; |
44 | -moz-border-radius: 1px ; |
45 | -webkit-border-radius: 1px ; |
The
tipVisible contains the
tipTitle and
tipIcon divs, floated to the left inside it. These are the only divs that are visible to the user when the page loads. In the jQuery step of the tutorial, you will see that we are also binding an event listener for the click event on
tipVisible, which slide-opens the content.
styles.css – Part 2
02 | . green .tipIcon{ background-color : #61b035 ; border : 1px solid #70c244 ; } |
03 | . blue .tipIcon{ background-color : #1078C7 ; border : 1px solid #1e82cd ; } |
04 | . red .tipIcon{ background-color : #CD3A12 ; border : 1px solid #da421a ; } |
10 | background : url ( 'img/plus.gif' ) no-repeat center center ; |
14 | -webkit-transition: -webkit-transform 0.2 s linear; |
15 | -moz-transition: -moz-transform 0.2 s linear; |
16 | transition: transform 0.2 s linear; |
19 | .slideOutTip.isOpened{ z-index : 10000 ; } |
21 | .slideOutTip.isOpened .plusIcon{ |
23 | -moz-transform:rotate( 45 deg); |
24 | -webkit-transform:rotate( 45 deg); |
25 | transform:rotate( 45 deg); |
35 | .openLeft .tipTitle{ float : right ; padding : 0 0 0 5px ; } |
36 | .openLeft .slideOutContent{ margin-top : 22px ; } |
37 | .openLeft.openTop .slideOutContent{ margin-top : 0 ; } |
48 | .main > p{ display : none ; } |
The default version of the slideout opens to the bottom-right. You can change this by assigning the
openLeft or
openTop class to the original p you add to the page (remember that the classes of the p tags are copied to the structure of the slideout). You can also change the color of the icon to
blue or
red by also assigning the respective class names.
A number of CSS3 rues are used here. Along the usual
border-radius (for rounded corners) and
box-shadow (for a outer-glow effect), I added the
transform:rotate(45deg) property, which rotates the plus sign when the slideout is opened.
If you view the example in
Safari/
Chrome (or version
3.7 of
Firefox, which is yet to be released), you can even see that the rotation is animated. This is done with the CSS3
transition property, inside which we specify the property that is going to be animated, the duration of the effect and the type of the animation.
Lastly, we use the
.main > p to hide the p tags that are directly inside the main div, so if JavaScript is disabled, you will not see the paragraphs. You could alternatively style and incorporate them in your design for a proper fallback solution.
Closed Slideout
Step 3 – jQuery
When the page loads, jQuery loops through all the paragraph elements in the main div, and replaces them with the markup of the slideouts. It later binds event listeners for the click event and slides open the content in the direction that was specified with the class names when the event occurs. Lets see how this works.
script.js – Part 1
01 | $(document).ready( function (){ |
05 | $( '.main p' ).replaceWith( function (){ |
13 |
"slideOutTip '+$(this).attr('class')+'" style= "'+$(this).attr('style')+'" >\
|
17 |
"tipTitle" > '+$(this).attr(' title ')+'
\ |
26 | $( '.slideOutTip' ).each( function (){ |
33 | $( this ).width(40+$( this ).find( '.tipTitle' ).width()); |
38 | $( '.tipVisible' ).bind( 'click' , function (){ |
39 | var tip = $( this ).parent(); |
42 | if (tip.is( ':animated' )) |
45 | if (tip.find( '.slideOutContent' ).css( 'display' ) == 'none' ) |
47 | tip.trigger( 'slideOut' ); |
49 | else tip.trigger( 'slideIn' ); |
As of version 1.4 of the jQuery library, the
replaceWith() method can take a function as a parameter. This is really handy, as it allows us to dynamically generate the markup. The
this points to the element, so we can easily get the values of the different attributes and the contents of the paragraph.
script.js – Part 2
01 | $( '.slideOutTip' ).bind( 'slideOut' , function (){ |
04 | var slideOut = tip.find( '.slideOutContent' ); |
07 | $( '.slideOutTip.isOpened' ).trigger( 'slideIn' ); |
10 | if (!tip.data( 'dataIsSet' )) |
12 | tip .data( 'origWidth' ,tip.width()) |
13 | .data( 'origHeight' ,tip.height()) |
14 | .data( 'dataIsSet' , true ); |
16 | if (tip.hasClass( 'openTop' )) |
24 | bottom : tip.parent().height()-(tip.position().top+tip.outerHeight()), |
32 | tip.find( '.tipVisible' ).css({position: 'absolute' ,bottom:3}); |
38 | tip.find( '.slideOutContent' ).remove().prependTo(tip); |
41 | if (tip.hasClass( 'openLeft' )) |
48 | right : Math.abs(tip.parent().outerWidth()-(tip.position().left+tip.outerWidth())), |
52 | tip.find( '.tipVisible' ).css({position: 'absolute' ,right:3}); |
58 | tip.addClass( 'isOpened' ).animate({ |
59 | width : Math.max(slideOut.outerWidth(),tip.data( 'origWidth' )), |
60 | height : slideOut.outerHeight()+tip.data( 'origHeight' ) |
We are binding two custom events to the slideout – “
slideIn” and “
sldieOut“. This way it is easier to initiate the opening and closing by just triggering the respective event.
Depending on whether one of the ‘
openLeft‘ or ‘
openRight‘ classes are assigned to the slideout, we apply some additional rules to the elements, so that they can slide-open properly.
After this we assign the
isOpened class to the slideout. Not only it marks the slideout as opened, it also applies a z-index of 10000 so it shows on top of all the other elements on the page.
script.js – Part 3
01 | }).bind( 'slideIn' , function (){ |
06 | tip.find( '.slideOutContent' ).fadeOut( 'fast' , function (){ |
08 | width : tip.data( 'origWidth' ), |
09 | height : tip.data( 'origHeight' ) |
11 | tip.removeClass( 'isOpened' ); |
The closing of the slideout consists of running an animation which returns the element to its original size (saved with the
data() method) and removing the
isOpened class.
Postado por Fernando Schimit - Fox Creative