Created
March 3, 2016 02:11
-
-
Save stephenbelyea/54b7d0aa3e954b29248b to your computer and use it in GitHub Desktop.
Clickable CTAs with sensible link content.
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
// Manage click/focus states for CTA with inner link. | |
var setWrapCTAs = function (event) { | |
var wrap = $('[data-cta-wrap]'), | |
link = wrap.find('[data-cta-link]'); | |
// Clicking on CTA wrapper triggers internal link. | |
wrap.on('click', function(e) { | |
e.preventDefault(); | |
var path = $(this).find('[data-cta-link]').attr('href'); | |
if ( path !== "" ) { | |
window.location = path; | |
} | |
}); | |
// Clicking on internal link avoids bubbling, focus | |
// and blur toggle .focus helper class on wrapper. | |
link.on('click', function(e) { | |
e.stopPropagation(); | |
}) | |
.on('focus', function(e) { | |
$(this).closest('[data-cta-wrap]').addClass('focus'); | |
}) | |
.on('blur', function(e) { | |
$(this).closest('[data-cta-wrap]').removeClass('focus'); | |
}); | |
}; | |
// If link wrap CTAs, prep links. | |
if ( $('[data-cta-wrap] [data-cta-link]').length ) { | |
setWrapCTAs(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment