Placed in: buy propecia 5mgbuy accutane with no prescriptionbuy zithromax onlinebuy cialis overnight
jQuery Code Expander

There is a problem on the web. Displaying actual programming code takes away a lot of space of the webpage (especially the longer ones). You can split up the code in several parts, but that's really hard to read for programmers. You can leave it "as it is" and users would have to scroll a lot if they're not interested in the code itself. And scrollbars in code-examples - that's just horrible (and yes - that's what I have on this website).

I created a solution for this problem and call it the jQuery Code Expander. It does exactly what you think it does - Expand any code you want to place online using jQuery.

jQuery Code Expander

But, before you can expand anything, it has to be cropped. That's where the CSS kicks in. It's like a combination of using scrollbars and fully showing the code!

Demo jQuery Code Expander   Download jQuery Code Expander

This script only changes those elements that needs to be expanded and doesn't touch those who don't. An additional overlay image is added, just to show the user that it can expand the code example. Check out the demo and read below what the secret of this technique is.

HTML

Although the HTML is very simply, it's vital to make this technique work. It looks like this:

 
<pre class="jcexpander"><code>
   <!-- Your code goes here -->
</code></pre>

The pre-element has a class (jcexpander) and one child element (code). That's all we need!

CSS

Once again, the CSS is really small, but this is another key to let this technique be a success. I've only placed the important stuff here - you can view the full CSS in the download.

 
pre.jcexpander {
   width:80%;
   height:500px;
   overflow:hidden;
}

As you can see, the pre element has been trimmed down. The overflow is set to hidden so you won't see the scrollbars. You should have a cropped version of the code block right now. Now, on to the jQuery part!

jQuery

There is something really important going on over here: Although the pre-element has been cropped down, the code-element hasn't. This means, that when you request the size of the code-element, you would get the size of how big it really is (when it wasn't cropped). That information is what we're going to use next.

I came up with the following script (which also adds the icon on top). Comments are added to make things clear.

 
/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
   // We'll need to loop through each <pre> element
   // with the class "jcexpander"
   $("pre.jcexpander").each(function(){
      
      // Only do something when the inner element (the <code> element)
      // is bigger than the parent (<pre>) element
      if( $("code", this).height() > $(this).height() ||
         $("code", this).width() > $(this).width()) {
         
         // We'll need to store the original values of the sizes
         // since we'll use it to "grow back"
         $(this).data('originalHeight' , $(this).height());
         $(this).data('originalWidth', $(this).width());
         
         // Create a IMG element for the overlay
         var icon = document.createElement("img");
         $(icon).css({ 'position' : 'absolute'});
         $(icon).attr("src", "images/fullscreen.png");
         
         // Append the image to the <pre> element
         $(icon).prependTo($(this));
         
         // When the <pre> element is hovered, this happens
         // First function is "over", second is "out"
         $(this).hover(function(){
            // Fade out the image
            $(icon).fadeOut();
            
            // Read the size of the <code> element
            var codeWidth = $("code", this).width();
            var codeHeight = $("code", this).height();
            
            // Size the <pre> element to be just as big
            // as the <code> element
            $(this)
               .stop()
               .animate({
                  width : codeWidth + "px",
                  height : codeHeight + "px"
               }, 1500);
               
         }, function(){
            // Fade in the image
            $(icon).fadeIn();
            
            // Size the <pre> element back to the
            // original size.
            $(this)
               .stop()
               .animate({
                  width : $(this).data('originalWidth') + "px",
                  height : $(this).data('originalHeight') + "px"
               }, 1500);
         });
      }
   });
});

And that's it! You can now try the script or just view the demo.

Conclusion and Download

Pretty cool, don't you think? If you want to implement this on your website, you might want to mess with the CSS (z-index and position) to make sure it doesn't break anything of your layout. I'm also aware of the problem that, if you expand the bottom box and scroll down, the window flashes. It's because there is a background image and I sadly don't thing there's a solution for this.

Demo jQuery Code Expander   Download jQuery Code Expander

What do you think of this script? Do you love it, see room for improvement, or think it's a bad idea overall? Feel free to share!


Tags:  code expander jquery webdevelopment

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!Technorati!StumbleUpon!Newsvine!Furl!Ma.gnolia!
Comments
Add NewSearchRSS
web2000 - Neat effect   2009-09-04 01:21:23
Gravatar image Hey that's a nice effect and good use of jQuery
Pascal   2009-09-04 18:14:07
Gravatar image It's a nice effect but it breaks if you don't have js activated... ;)
Also it looks like i broke it when i resized the top code and then went down and resized the lower code box while the other one was resizing (i use firefox 3.5)
Marco   2009-09-05 18:54:29
Gravatar image It's pretty obvious that it doesn't work when the user has JavaScript disabled. But who does that these days? You could also set "overflow" to "auto", to make it work on those systems too.

It's not broke when you resize two windows - the animation just isn't smooth anymore. It is when the page is very long, but when a code box is hitting the bottom of the page, it gives some hick-up's.
Gaya   2009-09-04 19:48:11
Gravatar image Looks nice Marco!

This will look great in cooperation with SynthaxHighlighter!
Cory - Loving the idea Marco   2009-09-06 06:20:32
Gravatar image I think you're on to something here. However, the hiccups you mentioned probably need to be worked out.

The big thing I'm noticing is alot of blinking as the box grows (FF on a Mac).
nurettin - Thanks   2009-09-07 18:12:47
Gravatar image thanks it is a very full
Fullblack - changing the position of the icon   2009-09-08 15:45:05
Gravatar image How is it possible put the icn on the right, or to determine the position of th icn

best regards

Alfredo
Marco   2009-09-09 17:47:55
Gravatar image Ofcourse! As you can see, the icon is absolutely positioned. With "margin-left" you can let it float to the right (just do some CSS stuff).

Why do you need to determine the position of the icon?
Janko - Nice   2009-09-09 09:16:17
Gravatar image Really nice implementation, much better than popular expanding to the right only.
Marco   2009-09-09 17:48:04
Gravatar image Thanks a lot mate - that means a lot coming from you :D!
victories - Nice   2009-09-12 22:57:24
Gravatar image How can I limit the right to expand max 600px? In the long code, page carries
Marco - Simple   2009-09-14 22:17:35
Gravatar image You can simply change the jQuery code right under "var codeWidth". Add the following:

Quote:
if (codeWidth > 600) {
codeWidth = 600;
}


Good luck!
khan   2009-09-17 19:46:30
Gravatar image All the best of luck to you
leo - query on this jquery   2009-09-21 22:56:05
Gravatar image what if i wanted to do this effect on regular text and html? not code. how can i accomplish that?
Marco - Good question!   2009-09-22 14:06:43
Gravatar image Hi Leo,

Good question! The actual "secret" here, is placing an element INSIDE an element. The outside element has a hidden overflow; The inside element hasn't.

Although I haven't tried it, you can try using a with a hidden overflow, which contains a that hasn't.

Good luck!
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

About 4662 others will follow everything on Marcofolio.net.