Skip to content

Instantly share code, notes, and snippets.

@dcorb
Created July 20, 2012 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcorb/3149951 to your computer and use it in GitHub Desktop.
Save dcorb/3149951 to your computer and use it in GitHub Desktop.
Auto-expandable code block when hovering. Drupal-ready.
// Effect that I liked from http://gsgd.co.uk/sandbox/jquery/easing/
$.easing.easeOutBack = function (x, t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
};
// Credit: http://stackoverflow.com/questions/1582534/calculating-text-width-with-jquery
$.fn.textWidth = function(){
var html_org = $(this).html();
var html_calc = '<span>' + html_org + '</span>'
$(this).html(html_calc);
var width = $(this).find('span:first').width();
$(this).html(html_org);
return width;
};
Drupal.behaviors.drupalmotion = {
attach: function (context, settings) {
$('body', context).once('drupalmotion', function () {
$('pre code', context).each(function(){
$pre = $(this).parent();
var contentwidth = $(this).textWidth() + 25; // 25px to compensate padding
var blockwidth = $(this).width() + 25;
if (contentwidth > blockwidth) {
$pre.hover(function() {
$(this).animate({ width: contentwidth}, 250, 'easeOutBack'); },
function() { $(this).animate({ width: blockwidth }, 250);
});
}
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment