Created
May 26, 2013 03:27
-
-
Save ikait/5651614 to your computer and use it in GitHub Desktop.
タブ切り替えビュー (jQueryプラグイン版
・タブ切り替え用のカスタムイベントを作って、triggerで動かす。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<link rel="stylesheet" href="style.css"> | |
<script src="jquery-1.10.0.js"></script> | |
<script src="jquery.tabs.js"></script> | |
<script> | |
$(function () { | |
$("nav").tabs({ | |
sectionContainer: "#content", | |
sectionAnchorProperty: 'id' | |
}); | |
}); | |
</script> | |
<title>Document</title> | |
</head> | |
<body> | |
<main id="main"> | |
<nav id="nav"> | |
<ul> | |
<li><a href="#section1">section1</a></li> | |
<li><a href="#section2">section2</a></li> | |
<li><a href="#section3">section3</a></li> | |
<li><a href="#section4">section4</a></li> | |
</ul> | |
</nav> | |
<article id="content"> | |
<section class="item" id="section1"> | |
<h1>section1</h1> | |
<p>content..</p> | |
</section> | |
<section class="item" id="section2"> | |
<h1>section2</h1> | |
<p>content..</p> | |
</section> | |
<section class="item" id="section3"> | |
<h1>section3</h1> | |
<p>content..</p> | |
</section> | |
<section class="item" id="section4"> | |
<h1>section4</h1> | |
<p>content..</p> | |
</section> | |
</article> | |
</main> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
$.fn.tabs = function(options) { | |
// defaults | |
var settings = $.extend({ | |
// tabs setting | |
tabElements: 'a', | |
/* tabContainer: this, */ | |
tabLinkProperty: 'href', | |
// sections setting | |
sectionElements: 'section', | |
sectionContainer: document, | |
sectionAnchorProperty: 'id', | |
// misc | |
showingClass: 'active' | |
}, options); // a arguments object override the adove defaults | |
var $tabContainer = $(this); | |
var $sectionContainer = $(settings.sectionContainer); | |
/** | |
* custom event: show or hide specified tab and section. | |
* | |
* usage: | |
* $tabContainer.trigger('changeTabs', <targetSection>); | |
*/ | |
$tabContainer.on('changeTabs', function(e, targetSection) { | |
// change location hash | |
window.location.hash = targetSection; | |
// delete a '#' for id as a anchor | |
if (targetSection[0] === '#' && | |
settings.sectionAnchorProperty !== 'id') { | |
targetSection = targetSection.slice(1); | |
} | |
// change specified tab's class | |
$tabContainer | |
.find(settings.tabElements) | |
.removeClass(settings.showingClass).end() | |
.find([settings.tabElements, | |
"[", settings.tabLinkProperty, "=", targetSection, "]" | |
].join('')) | |
.addClass(settings.showingClass); | |
// delete a '#' for id as a anchor | |
if (targetSection[0] === '#') { | |
targetSection = targetSection.slice(1); | |
} | |
// change specified section's class | |
$sectionContainer | |
.find(settings.sectionElements) | |
.removeClass(settings.showingClass).end() | |
.find([settings.sectionElements, | |
"[", settings.sectionAnchorProperty, "=", targetSection, "]" | |
].join('')) | |
.addClass(settings.showingClass); | |
}); | |
// if clicked | |
$tabContainer.on('click', settings.tabElements, function(e) { | |
e.preventDefault(); | |
// show the section that clicked tab points | |
var targetSection = $(this).attr(settings.tabLinkProperty); | |
$tabContainer.trigger("changeTabs", targetSection); | |
}); | |
// after loading page, show the first tab/section | |
if (window.location.hash) { | |
$tabContainer.trigger("changeTabs", window.location.hash); | |
} else { | |
$tabContainer.trigger("changeTabs", | |
$(settings.tabElements).first().attr(settings.tabLinkProperty) | |
); | |
} | |
// event for back/next button | |
$(window).on('hashchange', function(e) { | |
$tabContainer.trigger("changeTabs", window.location.hash); | |
}); | |
// return for chaining | |
return $tabContainer; | |
}; | |
}(jQuery)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@charset "utf-8"; | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
#main { | |
display: block; | |
width: 600px; | |
margin: 0 auto; | |
} | |
#nav { | |
padding-top: 30px; | |
} | |
#nav li { | |
float: left; | |
list-style: none; | |
border-width: 1px 1px 1px 0; | |
border-style: solid; | |
} | |
#nav li:first-child { | |
border-width: 1px; | |
} | |
#nav li a { | |
display: block; | |
padding: 10px 20px; | |
} | |
#nav li a:hover, | |
#nav li a.active { | |
background-color: #efefef; | |
} | |
#content { | |
clear: both; | |
padding-top: 15px; | |
} | |
#content section { | |
display: none; | |
border: 1px solid; | |
padding: 15px; | |
} | |
#content section.active { | |
display: block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment