Skip to content

Instantly share code, notes, and snippets.

@Yidada
Created July 21, 2016 09:37
Show Gist options
  • Save Yidada/52c4be6a67e9da9edd2b9d4516b47a78 to your computer and use it in GitHub Desktop.
Save Yidada/52c4be6a67e9da9edd2b9d4516b47a78 to your computer and use it in GitHub Desktop.
简单的仿照 twitter heart做的一个
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>twitter_heart</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<style type="text/css">
@keyframes heart {
from {
background-position: -0px;
}
to {
background-position: -2800px;
}
}
.heart.like {
animation: heart .28s steps(28);
animation-fill-mode: forwards;
}
.heart {
background: url('http://o8sxkcont.bkt.clouddn.com/web_heart_animation.png');
background-repeat: no-repeat;
height: 100px;
width: 100px;
}
</style>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<div class="heart"></div>
<!-- <script src="../common/zepto.js"></script> -->
<script>
// var $heart = $(".heart");
// $heart.click(function(e) {
// $(this).toggleClass('like')
// })
(function() {
var heart = document.getElementsByClassName("heart")[0];
heart.addEventListener('click', function(e) {
var c = heart.getAttribute('class');
if (c.indexOf('like')>0) {
heart.setAttribute('class', 'heart')
} else {
heart.setAttribute('class', 'heart like')
}
})
}())
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment