// *************************
// ターゲットの高さを保存
// *************************
var itemHeights = []; //実際の要素の高さ(配列)
var returnHeight; //縮めているときに見える高さ
$(function () {
$(".more-item").each(function () {
var thisHeight = $(this).height(); //実際の.more-itemの高さを取得
itemHeights.push(thisHeight); //配列に入れる
$(this).addClass("is-hide"); //.is-hideの高さ(CSSで指定)に縮める
returnHeight = $(this).height(); //.is-hideの高さを取得
});
});
// *************************
// .openクリック時の処理
// *************************
$(".more-btn > .open").click(function () {
var index = $(this).parent(".more-btn").index(".more-btn"); //何番目の.more-btnか確認
var addHeight = itemHeights[index]; //配列から高さを取得
$(this).parent(".more-btn").addClass("is-show").next(".more-item").animate({
height: addHeight
}, 200).removeClass("is-hide"); //実際の.more-itemの高さに戻す
$(this).css("display", "none"); //開くボタンを消す
$(this).next(".close").css("display", "block"); //閉じるボタンを出す
});
// *************************
// .closeクリック時の処理
// *************************
$(".more-btn > .close").click(function () {
$(this).parent(".more-btn").removeClass("is-show").next(".more-item").animate({
height: returnHeight
}, 200).addClass("is-hide"); //.is-hideの高さ(CSSで指定)に縮める
$(this).css("display", "none"); //閉じるボタンを消す
$(this).prev(".open").css("display", "block"); //開くボタンを出す
});
// ******************************
// ウィンドウサイズ変更時の処理
// ******************************
$(window).resize(function () {
$(".more-btn").each(function () {
var index = $(this).index(".more-btn");
var thisHeight = $(this).next(".more-item").children().height(); //ターゲットの子要素の高さを取得
itemHeights[index] = thisHeight; //高さを更新
var addHeight = itemHeights[index];
if ($(this).hasClass("is-show")) {
$(this).next(".more-item").css("height", addHeight); //開いている.more-itemの高さを変更する
}
});
});