Ask questions and get good answers on mb.jQuery.components

0
1

In my current project, the users need to post articles which may include programming codes. I wanted the code block (pre block) to expand on hover to its full width so that you don't have to scroll the contents to see the entire code.

I was using mb hoverIntent() function but a problem came. I wanted the jquery to execute only if the code block's width is larger than width of the content:

Please see the code and suggest changes:

var contentwidth = $("pre").contents().width();
var blockwidth = $("pre").width();
if(contentwidth > blockwidth) {
 $("pre").hoverIntent(
 function(){
     $(this).animate({width:contentwidth},200); //contentwidth
     $(this).css({'z-index':'auto','background':'#ddd'});
  },
  function(){
     $(this).animate({width:blockwidth},100);
  });
}

asked May 14 '11 at 03:49

thepesh's gravatar image

thepesh
265

closed Jun 08 '11 at 15:58

Matteo%20Bicocchi's gravatar image

Matteo Bicocchi ♦♦
1776129

The question has been closed for the following reason "The question is answered, right answer was accepted" by Matteo Bicocchi Jun 08 '11 at 15:58


Hi, Hoverintent is just used by the mb components but the author is Brian Cherne (http://cherne.net/brian/resources/jquery.hoverIntent.html);

To solve your issue you can't retrive the .contents().width() via jquery; it fires an error. You should wrap the code inside the PRE with a CODE and then get the width of the CODE tag:

<pre>
  <code>
    ....
    ....
  </code>
</pre>

var contentwidth = $("pre code").width();

This should work.

answered May 14 '11 at 07:28

Matteo%20Bicocchi's gravatar image

Matteo Bicocchi ♦♦
1776129

edited May 14 '11 at 07:30

jQuery main site - Matteo Bicocchi