jQueryでできるフィルタリングのご紹介です。
以下がコードサンプルになります。
▼JavaScript
$("button").click(function () {
var image = $(this).attr("value");
$("#list li").each(function () {
$(this).animate({
"opacity": 0
}, 300, function () {
$(this).hide();
if ($(this).hasClass(image) || image == "all") {
$(this).show();
$(this).animate({
"opacity": 1
}, 300);
}
});
});
});
});
▼CSS
button{
font-size:90%;
margin-right:10px;
display:block;
color:#E9ECEB;
padding:10px 30px;
background:#000;
border-radius:20px;
border:none;
display:inline-block;
cursor:pointer;
}
#buttons button:last-child{
margin-right:0;
}
button:focus{
outline:none;
}
button:hover{
background:#717171;
}
#list{
margin:10px auto 0;
padding:10px 10px;
max-width: 80%;
background: #ccc;
height: 650px;
}
#list li{
margin:10px;
width:200px;
height:150px;
list-style-type:none;
display: inline-block;
}
▼HTML
デモページは、こちら











javascript初心者です。色々と参考にさせていただいています。別ページから遷移してフィルタリングは可能でしょうか。
>マツイさん
当ブログを閲覧していただきありがとうございます。
この記事のものは、javascriptで各ボタンをクリックしたとき、フィルタリングを行うという処理をしているので記事のものではできません。
javascriptをクリック時に処理を実行するという処理から読み込み時に処理を実行する処理に変更し、
「URLクエリパラメータ」というURLの後ろにパラメーターを追加することできるものなどで、
URLを切り替え、「if文の条件分岐」を行うことで実現することは可能です。
返信遅くなり申し訳ありません。承知いたしました。ありがとうございます。