「javascript/test」の編集履歴(バックアップ)一覧に戻る

javascript/test - (2017/11/30 (木) 06:18:04) のソース

#js(){{{{{
<style>
/* 全体 */
#tabs {
  margin-top: 50px;
  padding-bottom: 40px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0,0,0,0.2);
  width: 100%;
  margin 0 auto;
}
/* タブボタン(label←forで関連付け→input radio) */
.tab_item {
  height: 50px;
  border-bottom: 3px solid #148;
  background-color: #def;
  line-height: 50px;
  font-size: 16px;
  text-align: center;
  color: #565656;
  display: block;
  float: left;
  text-align: center;
  font-weight: bold;
  transition: all 0.2s ease;
}
.tab_item:hover {
  opacity: 0.75;
}
/* ラジオボタンは常に隠す */
input[name="tab_item"] {
  display:none;
}
/* コンテンツは全て隠す */
#tabs> [id^="tab_content"] {
  padding: 40px 40px 0;
  clear: both;
  overflow: hidden;
  display: none;
}
/* 選択されたラジオボタンとコンテンツを関連付けて対応するコンテンツのみ表示 */
#btn1:checked ~ #tab_content1,
#btn2:checked ~ #tab_content2,
#btn3:checked ~ #tab_content3,
#btn4:checked ~ #tab_content4,
#btn5:checked ~ #tab_content5 {
  display: block;
}
/* 選択中のタブボタン */
#tabs input:checked + .tab_item {
  background-color: #148;
  color: #fff;
}
</style>
<script type="text/javascript">
function lastTabIndex() {
  // ブラウザを閉じるまで現在のコメントタブを記憶する
 sessionStorage.lastTabIndex = $(this).attr('for');
}
$(function(){
  var target = $('#tab_menu');
  var captions = $.trim(target.text()).split(',');
  var def = sessionStorage.getItem('lastTabIndex') || 'btn1';
  if ($(def.replace('btn', '#tab_content')).length == 0) {
    def = 'btn1';
  }
  for (var i = captions.length; i > 0; i--) {
    target.after($('<label class="tab_item">').attr('for', 'btn'+i).css('width', 'calc(100%/'+captions.length+')').text(captions[i-1]).click(lastTabIndex));
    target.after($('<input type="radio" name="tab_item">').attr('id', 'btn'+i).prop('checked', 'btn'+i === def));
  }
  target.remove();
});
</script>
}}}}}