Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created January 12, 2016 19:10
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 thinkphp/313b83fa3938ec2b52e8 to your computer and use it in GitHub Desktop.
Save thinkphp/313b83fa3938ec2b52e8 to your computer and use it in GitHub Desktop.
(function(){
var animating = false;
function animatecard(ev) {
if (animating === false) {
var t = ev.target;
if (t.className === 'but-nope') {
t.parentNode.classList.add('nope');
animating = true;
fireCustomEvent('nopecard',
{
origin: t,
container: t.parentNode,
card: t.parentNode.querySelector('.card')
}
);
}
if (t.className === 'but-yay') {
t.parentNode.classList.add('yes');
animating = true;
fireCustomEvent('yepcard',
{
origin: t,
container: t.parentNode,
card: t.parentNode.querySelector('.card')
}
);
}
if (t.classList.contains('current')) {
fireCustomEvent('cardchosen',
{
container: getContainer(t),
card: t
}
);
}
}
}
function fireCustomEvent(name, payload) {
var newevent = new CustomEvent(name, {
detail: payload
});
document.body.dispatchEvent(newevent);
}
function getContainer(elm) {
var origin = elm.parentNode;
if (!origin.classList.contains('cardcontainer')){
origin = origin.parentNode;
}
return origin;
}
function animationdone(ev) {
animating = false;
var origin = getContainer(ev.target);
if (ev.animationName === 'yay') {
origin.classList.remove('yes');
}
if (ev.animationName === 'nope') {
origin.classList.remove('nope');
}
if (origin.classList.contains('list')) {
if (ev.animationName === 'nope' ||
ev.animationName === 'yay') {
origin.querySelector('.current').remove();
if (!origin.querySelector('.card')) {
fireCustomEvent('deckempty', {
origin: origin.querySelector('button'),
container: origin,
card: null
});
} else {
origin.querySelector('.card').classList.add('current');
}
}
}
}
document.body.addEventListener('animationend', animationdone);
document.body.addEventListener('webkitAnimationEnd', animationdone);
document.body.addEventListener('click', animatecard);
window.addEventListener('DOMContentLoaded', function(){
document.body.classList.add('tinderesque');
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment