Skip to content

Instantly share code, notes, and snippets.

@matsuhisa
Last active October 27, 2015 03:56
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 matsuhisa/fc3f75dee2d6fca67dd1 to your computer and use it in GitHub Desktop.
Save matsuhisa/fc3f75dee2d6fca67dd1 to your computer and use it in GitHub Desktop.
favorites
<html>
<head>
<meta charset="UTF-8">
<title>クリックイベント</title>
<link rel="stylesheet" href="./style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="./script.js"></script>
</head>
<body>
<div class="button like" data-button-id="123456">
<p class="">あいうえお</p>
</div>
<div class="button like" data-button-id="78910">
<p class="">かきくけこ</p>
</div>
</body>
</html>
var click_like = function(){
// 1回目なのか?をチェックする
if($(this).hasClass("like")){
$(this).removeClass("like");
$(this).addClass("liked");
// 登録作業
}else if ($(this).hasClass("liked")) {
$(this).removeClass("liked");
$(this).addClass("like");
// 登録解除作業
}
}
var favorites = {
places: [78910, 12, 45, 111]
}
//
$(function(){
var likes = $(".like");
likes.each(function(index){
var button_id = $(this).data('button-id');
console.log(button_id);
if( (button_id > 0) && $.inArray(button_id, favorites['places'])) {
$(this).removeClass("like");
$(this).addClass("liked");
}
$(this).bind('click', click_like);
})
})
.button {
display: inline-block;
border: 1px solid #333;
padding: 0.2em;
cursor: pointer;
}
.like {
color: #ff658b;
background-color: #eee;
}
.liked {
color: #fff;
font-weight: bold;
background-color: #ff658b;
-webkit-animation: bg-color 0.2s;
}
@-webkit-keyframes bg-color {
50% { color: #ff658b; }
90% { color: #fff; }
}
@matsuhisa
Copy link
Author

jQuery について、bindからonにすること
http://qiita.com/nmta/items/310aa1cf385fa55129c1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment