6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryでタブ切り替え

Last updated at Posted at 2016-02-16

#仕様
ごく普通のタブ。
#HTML
targetとtriggerにデータ属性を付与。

HTML
<ul>
  <li data-tab-trigger='tab'>kuma</li>
  <li data-tab-trigger='tab'>tama</li>
  <li data-tab-trigger='tab'>kiso</li>
</ul>
<ul>
  <li data-tab-target='tab'>球磨だクマー</li>
  <li data-tab-target='tab'>多摩だニャー</li>
  <li data-tab-target='tab'>木曽だキソー</li>
</ul>

#JS
引数でロード時にアクティブになるタブを何番目にするか指定。
(0からカウントが始まるので注意)

JQuery
var tabSwitch = function(active_num){
	var $trigger = $('[data-trigger-tab]'),
		$target  = $('[data-target-tab]'),
		$active;
	if(!$target.length) return;
	$target.addClass('is_Disable');
	$target.eq(active_num).removeClass('is_Disable');
	$trigger.eq(active_num).addClass('is_active');
	$trigger.on('click',function(){
		var num = $(this).index(),
			$active = $('.is_active'),
			hide_num = $active.index();
		$active.removeClass('is_active_tab');
		$target.eq(hide_num).addClass('is_Disable');
		$target.eq(num).removeClass('is_Disable');
		$trigger.eq(num).addClass('is_active');
	})
};
$(function(){
    tabswitch(0);
});

#CSS

css
.is_Disable {
  display:none;
}
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?