Tinder swipe cards made with hammer.js.
Created
March 25, 2022 17:34
-
-
Save sg773/56e11a578fd9a7294d8ae5333865b1df to your computer and use it in GitHub Desktop.
Tinder swipe cards
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
<div class="tinder"> | |
<div class="tinder--status"> | |
<i class="fa fa-remove"></i> | |
<i class="fa fa-heart"></i> | |
</div> | |
<div class="tinder--cards"> | |
<div class="tinder--card" id="hr"> | |
<img src="https://www.mansworldindia.com/wp-content/uploads/2021/08/Hritik-Roshan.jpg"> | |
<h3>Hrithik</h3> | |
<p>Mujhe left swipe karna was NOT FUNNY!</p> | |
</div> | |
<div class="tinder--card" id="srk"> | |
<img src="https://images.hindustantimes.com/rf/image_size_960x540/HT/p2/2020/10/19/Pictures/_377255de-1223-11eb-8d9b-3f5d4a8a0fbd.jpg"> | |
<h3>SRK</h3> | |
<p>Agar woh mujhe pyar karti hogi toh right swipe karegi!</p> | |
</div> | |
<div class="tinder--card" id="tiger"> | |
<img src="https://indianmemetemplates.com/wp-content/uploads/choti-bachi-ho-kya-blank.jpg"> | |
<h3>Tiger</h3> | |
<p>Bacchi ho kya?</p> | |
</div> | |
<div class="tinder--card" id="shivanshu"> | |
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1583922095690/d48CmwzVA.jpeg"> | |
<h3>Shivanshu</h3> | |
<p>Yours truly!</p> | |
</div> | |
</div> | |
<br><br> | |
</div> |
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
'use strict'; | |
var tinderContainer = document.querySelector('.tinder'); | |
var allCards = document.querySelectorAll('.tinder--card'); | |
var nope = document.getElementById('nope'); | |
var love = document.getElementById('love'); | |
const leftIds = ['hr', 'srk', 'tiger']; | |
function initCards(card, index) { | |
var newCards = document.querySelectorAll('.tinder--card:not(.removed)'); | |
newCards.forEach(function (card, index) { | |
card.style.zIndex = allCards.length - index; | |
card.style.transform = 'scale(' + (20 - index) / 20 + ') translateY(-' + 30 * index + 'px)'; | |
card.style.opacity = (10 - index) / 10; | |
}); | |
tinderContainer.classList.add('loaded'); | |
} | |
initCards(); | |
allCards.forEach(function (el) { | |
var hammertime = new Hammer(el); | |
hammertime.on('pan', function (event) { | |
el.classList.add('moving'); | |
}); | |
hammertime.on('pan', function (event) { | |
if (event.deltaX === 0) return; | |
if (event.center.x === 0 && event.center.y === 0) return; | |
tinderContainer.classList.toggle('tinder_love', event.deltaX > 0); | |
tinderContainer.classList.toggle('tinder_nope', event.deltaX < 0); | |
var xMulti = event.deltaX * 0.03; | |
var yMulti = event.deltaY / 80; | |
var rotate = xMulti * yMulti; | |
event.target.style.transform = 'translate(' + event.deltaX + 'px, ' + event.deltaY + 'px) rotate(' + rotate + 'deg)'; | |
}); | |
hammertime.on('panend', function (event) { | |
el.classList.remove('moving'); | |
tinderContainer.classList.remove('tinder_love'); | |
tinderContainer.classList.remove('tinder_nope'); | |
var moveOutWidth = document.body.clientWidth; | |
var keep = Math.abs(event.deltaX) < 80 || Math.abs(event.velocityX) < 0.5; | |
if (keep) { | |
event.target.style.transform = ''; | |
} else { | |
var endX = Math.max(Math.abs(event.velocityX) * moveOutWidth, moveOutWidth); | |
var toX = event.deltaX > 0 ? endX : -endX; | |
var endY = Math.abs(event.velocityY) * moveOutWidth; | |
var toY = event.deltaY > 0 ? endY : -endY; | |
var xMulti = event.deltaX * 0.03; | |
var yMulti = event.deltaY / 80; | |
var rotate = xMulti * yMulti; | |
if(leftIds.includes(event.target.id) && toX < 0){ | |
event.target.classList.toggle('removed', !keep); | |
} | |
event.target.style.transform = 'translate(' + toX + 'px, ' + (toY + event.deltaY) + 'px) rotate(' + rotate + 'deg)'; | |
initCards(); | |
} | |
}); | |
}); | |
function createButtonListener(love) { | |
return function (event) { | |
var cards = document.querySelectorAll('.tinder--card:not(.removed)'); | |
var moveOutWidth = document.body.clientWidth * 1.5; | |
if (!cards.length) return false; | |
var card = cards[0]; | |
card.classList.add('removed'); | |
if (love) { | |
card.style.transform = 'translate(' + moveOutWidth + 'px, -100px) rotate(-30deg)'; | |
} else { | |
card.style.transform = 'translate(-' + moveOutWidth + 'px, -100px) rotate(30deg)'; | |
} | |
initCards(); | |
event.preventDefault(); | |
}; | |
} | |
var nopeListener = createButtonListener(false); | |
var loveListener = createButtonListener(true); | |
nope.addEventListener('click', nopeListener); | |
love.addEventListener('click', loveListener); |
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
<script src="https://hammerjs.github.io/dist/hammer.min.js"></script> |
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
*, *:before, *:after { | |
box-sizing: border-box; | |
padding: 0; | |
margin: 0; | |
} | |
body { | |
background: #CCFBFE; | |
overflow: hidden; | |
font-family: sans-serif; | |
} | |
.tinder { | |
width: 100vw; | |
height: 100vh; | |
overflow: hidden; | |
display: flex; | |
flex-direction: column; | |
position: relative; | |
opacity: 0; | |
transition: opacity 0.1s ease-in-out; | |
} | |
.loaded.tinder { | |
opacity: 1; | |
} | |
.tinder--status { | |
position: absolute; | |
top: 50%; | |
margin-top: -30px; | |
z-index: 2; | |
width: 100%; | |
text-align: center; | |
pointer-events: none; | |
} | |
.tinder--status i { | |
font-size: 100px; | |
opacity: 0; | |
transform: scale(0.3); | |
transition: all 0.2s ease-in-out; | |
position: absolute; | |
width: 100px; | |
margin-left: -50px; | |
} | |
.tinder_love .fa-heart { | |
opacity: 0.7; | |
transform: scale(1); | |
} | |
.tinder_nope .fa-remove { | |
opacity: 0.7; | |
transform: scale(1); | |
} | |
.tinder--cards { | |
flex-grow: 1; | |
padding-top: 40px; | |
text-align: center; | |
display: flex; | |
justify-content: center; | |
align-items: flex-end; | |
z-index: 1; | |
} | |
.tinder--card { | |
display: inline-block; | |
width: 90vw; | |
max-width: 400px; | |
height: 70vh; | |
background: #FFFFFF; | |
padding-bottom: 40px; | |
border-radius: 8px; | |
overflow: hidden; | |
position: absolute; | |
will-change: transform; | |
transition: all 0.3s ease-in-out; | |
cursor: -webkit-grab; | |
cursor: -moz-grab; | |
cursor: grab; | |
} | |
.moving.tinder--card { | |
transition: none; | |
cursor: -webkit-grabbing; | |
cursor: -moz-grabbing; | |
cursor: grabbing; | |
} | |
.tinder--card img { | |
max-width: 100%; | |
pointer-events: none; | |
} | |
.tinder--card h3 { | |
margin-top: 32px; | |
font-size: 32px; | |
padding: 0 16px; | |
pointer-events: none; | |
} | |
.tinder--card p { | |
margin-top: 24px; | |
font-size: 20px; | |
padding: 0 16px; | |
pointer-events: none; | |
} | |
.tinder--buttons { | |
flex: 0 0 100px; | |
text-align: center; | |
padding-top: 20px; | |
} | |
.tinder--buttons button { | |
border-radius: 50%; | |
line-height: 60px; | |
width: 60px; | |
border: 0; | |
background: #FFFFFF; | |
display: inline-block; | |
margin: 0 8px; | |
} | |
.tinder--buttons button:focus { | |
outline: 0; | |
} | |
.tinder--buttons i { | |
font-size: 32px; | |
vertical-align: middle; | |
} | |
.fa-heart { | |
color: #FFACE4; | |
} | |
.fa-remove { | |
color: #CDD6DD; | |
} | |
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
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment