Created
February 17, 2012 09:53
-
-
Save peteboere/1852272 to your computer and use it in GitHub Desktop.
Small jQuery plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Equalise the height of boxes (uses min-height). | |
*/ | |
jQuery.fn.balanceHeights = function() { | |
// Get max height from list then apply to all. | |
var heights = []; | |
this.each( function () { | |
heights.push(jQuery(this).outerHeight()); | |
}); | |
return this.css('min-height', Math.max.apply({}, heights)); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* jQuery boxlink plugin | |
* | |
* Makes container boxes clickable if they contain at least one link. | |
* A class of 'boxlink-hover' is added to the container on mouseenter. | |
*/ | |
(function ($) { | |
$.fn.boxlink = function (options) { | |
var options = options || {}; | |
return this.each(function() { | |
var $box = $(this); | |
var $boxLinks = $box.find('a[href]'); | |
// Bail if no links | |
if (! $boxLinks.length) { | |
return; | |
} | |
// Unbind previously attached boxlink events and bind handlers. | |
$box | |
.off('.boxlink') | |
.on('mouseenter.boxlink', function() { | |
$box.addClass('boxlink-hover'); | |
}) | |
.on('mouseleave.boxlink', function() { | |
$box.removeClass('boxlink-hover'); | |
}) | |
.on('click.boxlink', function(e) { | |
var $target = $(e.target); | |
var $allLinks = $box.find('a[href]'); | |
// Bail if directly clicking a link (or child of a link), or, | |
// if $box has no links. | |
if ($target.closest('a[href]').length || ! $allLinks.length) { | |
return true; | |
} | |
// Resolve the link source. | |
var $linkSource = {}; | |
// Filtered to a selector. | |
if (options.filter) { | |
$linkSource = $allLinks.filter(options.filter); | |
} | |
// Filtered to [data-boxlink-target] attribute. | |
if (! $linkSource.length) { | |
$linkSource = $allLinks.filter('[data-boxlink-target], .boxlink-target'); | |
} | |
// Any link. | |
if (! $linkSource.length) { | |
$linkSource = $allLinks; | |
} | |
e.stopPropagation(); | |
window.location.href = $linkSource[0].href; | |
}); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment