Skip to content

Instantly share code, notes, and snippets.

@lincoln-chawora
Created February 19, 2020 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincoln-chawora/97afccdd7b37c63c0baa2da77a8f0fa4 to your computer and use it in GitHub Desktop.
Save lincoln-chawora/97afccdd7b37c63c0baa2da77a8f0fa4 to your computer and use it in GitHub Desktop.
How to make a card clickable using jquery
(function card($, Drupal) {
Drupal.behaviors.theme_card = {
attach(context) {
$('[data-card]', context)
.once('theme-card')
.each(function eachCard() {
// https://inclusive-components.design/cards/
const $link = $(this)
.find('a')
.first();
if ($link.length === 0 || $(this).data().cardClickable === false) {
return;
}
$(this).css('cursor', 'pointer');
let down;
let up;
$(this).mousedown(() => {
down = +new Date();
});
$(this).mouseup(e => {
// Bail if this was a middle or secondary mouse button event.
if (e.which !== 1) return;
up = +new Date();
if (up - down < 600) {
// Use the time difference between mouse down and mouse up to
// guess whether the user is clicking, or trying to select text.
// Only redirect if it looks like a click not text selection.
window.location.href = $link.attr('href');
}
});
});
},
};
})(jQuery, Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment