Skip to content

Instantly share code, notes, and snippets.

@craigmdennis
Created February 12, 2012 22:13
Show Gist options
  • Save craigmdennis/1811116 to your computer and use it in GitHub Desktop.
Save craigmdennis/1811116 to your computer and use it in GitHub Desktop.
Capture a link and perform an animation before navigating to it
$('a:not(.external').on('click', function(e) {
// Detect if the ctrl key is pressed (indicating opening in a new tab)
if (!e.ctrlKey) {
// Cache variables for speed
var $this = $(this);
var href = $this.attr('href');
e.preventDefault();
// If the current url is not the same as the link href
if (window.location.href != $href) {
// If the current link is a mailto
if (href.indexOf('mailto') > -1) {
// Send people to the link (open email client in)
window.location = href;
} else {
// Perform 'outbound' animations
$('#content').fadeOut( function(){
// Send people on their way after animation callback
window.location = href;
});
});
}
});​
@craigmdennis
Copy link
Author

You can use the Wordpress Plugin WP External Links to set the class of .external on the links as well as a whole bunch of other things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment