- ãã³ããŒã¬ãŒã¡ãã¥ãŒã®äœãæ¹ãããããªã
- 䜿ã£ãŠãããã³ããŒã¬ãŒã¡ãã¥ãŒãåã£ãŠããã®ãäžå®
- å®åã§äœ¿ãããã³ããŒã¬ãŒã¡ãã¥ãŒãç¥ããã
ãã®ãããªæ©ã¿ã«çããŸãã
çŸä»£ã§ã¯ã»ãšãã©ã®ãµã€ãã«äœ¿çšãããŠãããã³ããŒã¬ãŒã¡ãã¥ãŒã§ãããHTML
ãCSS
ãJavaScript
ãããããæžãå¿
èŠãããã®ã§å€§å€ã§ãããïŒ
ããã§ãã®èšäºã§ã¯ããã³ããŒã¬ãŒã¡ãã¥ãŒã®äœãæ¹ã培åºè§£èª¬ããŸãã
ãã äœãã ãã§ã¯ãªããããŒããŒãæäœãèŠèŠæ¯æŽããŒã«ãå©çšããæ¹ãžã®é æ ®ãäžå¿ã«ãå ·äœçãªæè¡ãšãã¯ããã¯ã玹ä»ããŸãã
ãããèªãã°ãå®åã§ã䜿ãã䜿ãããããã³ããŒã¬ãŒã¡ãã¥ãŒã®äœãæ¹ã1ããç解ããããšãã§ããŸãã
1ããèªåã§äœãã®ã§ãã«ã¹ã¿ãã€ãºãç°¡åã«ã§ããæ§ã
ãªæ¡ä»¶ã«å¯Ÿå¿ã§ããããã«ãªããŸãã
ãã®èšäºãæžãã人
ããã
ããªãŒã©ã³ã¹ã§webãã¶ã€ããŒãããŠããŸãã
ãã¶ã€ã³ããã³ãŒãã£ã³ã°ãéçšãŸã§å¹
åºã察å¿ããŠããŸãã
3幎éã§50件以äžã®ãµã€ããã³ãŒãã£ã³ã°ããŠããŸããã
ãã³ããŒã¬ãŒã¡ãã¥ãŒãšã¯
ãã³ããŒã¬ãŒã¡ãã¥ãŒãšã¯ãäžæ¬ç·ïŒâ¡ïŒã®ã¢ã€ã³ã³ã§è¡šãããã¡ãã¥ãŒã®ããšã§ãã
ååã®ç±æ¥ã¯ãã¢ã€ã³ã³ã®åœ¢ããã³ããŒã¬ãŒã®ããã«èŠããããšããæ¥ãŠããŸãã
äž»ã«ã¹ãã衚瀺ã®éã«äœ¿ãããŠããŸããããè¿å¹Žã§ã¯PC衚瀺ã§ã䜿ãããããšãå¢ããŠããŸãã
ã¢ã€ã³ã³ããã³ããŒã¬ãŒã«äŒŒãŠããããšãããã®ååãä»ããããŠããŸãããä»ã«ã以äžã®ãããªåŒã³åããããŸãã
- ãã³ããŒã¬ãŒã¢ã€ã³ã³
- ããã¯ãŒã¡ãã¥ãŒ
- ãããã¢ããã¡ãã¥ãŒ
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®åœ¹å²
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®äž»ãªåœ¹å²ã¯ããµã€ãããã£ããèŠããããšã§ãã
ç¹ã«ã¹ããŒããã©ã³ã®ãããªå°ããªç»é¢ã§å€§ããªå¹æãçºæ®ããŸãã
äŸãã°ãã¹ããŒããã©ã³ã§ãŠã§ããµã€ããèŠããšããããããã®ã¡ãã¥ãŒé
ç®ãç»é¢ãå ãããšæ··ä¹±ããŸãããã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã䜿ãã°ããããã®é
ç®ããããã«é ããå¿
èŠãªãšãã ã衚瀺ãããããšãã§ããŸãã
çµæãšããŠããŠãŒã¶ãŒã¯æ å ±ãç°¡åã«èŠã€ããããšãã§ããå¿«é©ã«ãµã€ããå©çšã§ããŸãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã¯ãã¹ããŒã¹ã®æå¹å©çšãšãŠãŒã¶ããªãã£åäžã®ããã«æ¬ ãããªãèŠçŽ ã§ãã
ã¡ãªãããšãã¡ãªãã
ãŠã§ããµã€ããäœããšãããã³ããŒã¬ãŒã¡ãã¥ãŒã¯ãšãŠã䟿å©ãªããŒã«ã§ãã
ãããããã¹ãŠã®ãã¶ã€ã³ã«ã¯è¯ãç¹ãšããã§ãªãç¹ããããŸãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒãäŸå€ã§ã¯ãããŸããã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®ã¡ãªãããšãã¡ãªããããŸãšããŸããã
- ã¹ããŒã¹ãæå¹æŽ»çšã§ãã
ç»é¢ãå°ãããŠããããããã®ã¡ãã¥ãŒé ç®ã綺éºã«åããããšãã§ããŸãã - ã¹ãããªãããã¶ã€ã³
ã·ã³ãã«ã§èŠããããã¶ã€ã³ãäœãã®ã«åœ¹ç«ã¡ãŸãã - ã©ããªããã€ã¹ã§ãOK
ã¹ããã§ãããœã³ã³ã§ããã©ã®ããã€ã¹ã§èŠãŠãåãããã«äœ¿ããŸãã
- ã¡ãã¥ãŒãèŠã€ãã«ãã
ã¡ãã¥ãŒãé ããŠããã®ã§ã䜿ãæ¹ããããã«ããããšããããŸãã - åå¿è
ã«ã¯é£ããããšã
åããŠã®äººã¯ãã¡ãã¥ãŒã®ã¢ã€ã³ã³ã«æ°ã¥ããªããããããŸããã - ã¢ã¯ã»ã·ããªãã£ã®åé¡
ã¹ã¯ãªãŒã³ãªãŒããŒã䜿ã人ããããŒããŒãã ãã§æäœãã人ã«ã¯äžäŸ¿ãããããŸããã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®ã¡ãªãããšãã¡ãªãããç解ããããšã§ãã¿ããªã«äœ¿ãããããŠã§ããµã€ããäœãããšãã§ããŸãã
次ããã¯ãå ·äœçãªãã³ããŒã¬ãŒã¡ãã¥ãŒã®äœãæ¹ã解説ããŠãããŸãã
å®è£ ãã¢
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®å®è£
ãã¢ã玹ä»ããŸããHTML
ãšCSS
ãjQuery
ã§å®è£
ããŠããŸãã
See the Pen ããã¯ãŒãããã³ãã¬ãŒã by ããã (@k-suzuki3533) on CodePen.
jQueryã䜿çšããã¡ãªãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®åäœã¯ããã©JavaScript
ã§ãå®è£
ã§ããŸãããç§ã¯jQuery
ã䜿çšããŠããŸãã
çç±ã¯ãjQuery
ã䜿çšããŠããå¶äœäŒç€ŸããŸã ãŸã å€ãããã§ãã
è¿å¹Žã¯JavaScript
ã®æžãæ¹ãç°¡åã«ãªããjQuery
ãšå€§å·®ãªãæžãããšãã§ããŸãã
ããããjQuery
ã䜿çšããŠããå¶äœäŒç€Ÿã¯ãŸã ãŸã å€ããèªã¿èŸŒã¿ãæå®ãããããšããã°ãã°ãããŸãã
jQuery
ãèªã¿èŸŒãŸããŠããã®ã§ããã°ã䜿çšããæ¹ãããç°¡åã«æžãããšãã§ããŸãã®ã§ã䜿ããªãæã¯ãããŸããããã
jQuery
ã䜿çšããŠããªãæ¡ä»¶ã§ããã°JavaScript
ã§èšè¿°ããŠãè¯ããšæããŸããããŸã ãŸã jQuery
ã䜿çšããŠããå¶äœäŒç€Ÿãå€ãçºãããã«åãããŠjQuery
ã§èšè¿°ããŠããŸãã
次ããã¯ãããããã®ããŒãã®å ·äœçãªäœãæ¹ã解説ããŠãããŸãã
ãã³ããŒã¬ãŒãã¿ã³ã®äœãæ¹
ãã³ããŒã¬ãŒãã¿ã³ã®å
·äœçãªäœãæ¹ã¯ã以äžã®èšäºã§è§£èª¬ããŠããŸãã
ã¬ã¹ãã³ã·ãã«ãæå°éã®ã³ãŒãã§å¯Ÿå¿ããæ§ã
ãªãã¶ã€ã³ã»æ¡ä»¶ã«å¯Ÿå¿ã§ããããã«ããŠããŸãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®äœãæ¹
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®å
·äœçãªäœãæ¹ã¯ã以äžã®èšäºã§è§£èª¬ããŠããŸããjQuery
ã¯ã¯ã©ã¹ã®çè±ã®ã¿ãšããã¢ãã¡ãŒã·ã§ã³ã¯CSS
ã§å¶åŸ¡ããŠããã®ã§ãæ§ã
ãªã¢ãã¡ãŒã·ã§ã³ã«ç°¡åã«å¯Ÿå¿ã§ããŸãã
ãŠãŒã¶ããªãã£ãšã¢ã¯ã»ã·ããªãã£ã«ã€ããŠèãã
ãŠã§ããµã€ãã®ã¢ã¯ã»ã·ããªãã£ãšãŠãŒã¶ããªãã£ã¯ããã¹ãŠã®äººãå¿«é©ã«å©çšã§ããããã«ããããã«äžå¯æ¬ ã§ãã
ã¢ã¯ã»ã·ããªãã£ãšã¯ãéãããæã€äººãå«ããã¹ãŠã®äººããŠã§ããµã€ãã容æã«å©çšã§ããããšã§ãã
ãŠãŒã¶ããªãã£ã¯ããŠãŒã¶ãŒã«ãšã£ãŠãŠã§ããµã€ããçŽæçã§äœ¿ããããããšã§ãã
ããã§ã¯ãç¹ã«ããŒããŒããé³å£°èªã¿äžãããŒã«ãå©çšãã人ã«ãšã£ãŠäœ¿ããããããèæ
®ããŸãã
å
·äœçã«ã¯ã以äžã®ç¹ã«èæ
®ãããã¹ãŠã®ãŠãŒã¶ãŒãå¿«é©ã«ãŠã§ããµã€ããå©çšã§ããããã«ããŸãã
aria-controls
å±æ§ã䜿çšããaria-expanded
å±æ§ã䜿çšãã- ã¡ãã¥ãŒå€ãã¯ãªãã¯ãããšã¡ãã¥ãŒãéããããããã«ãã
- tabããŒã§æäœã§ããããã«ãã
- escapeããŒã§ã¡ãã¥ãŒãéããããããã«ãã
aria-controls
å±æ§ã䜿çšãã
aria-controls
å±æ§ãšã¯ãå¥ã®èŠçŽ ãå¶åŸ¡ããŠããããšã瀺ããã®ã§ãã
ä»åã®ã³ãŒãã§ã¯ã以äžã®ããã«èšè¿°ããŠããŸãã
<button aria-controls="drawer-nav">
aria-controls="drawer-nav"
ãšèšè¿°ããããšã§ããã¿ã³ã drawer-nav
ãšããidãå¶åŸ¡ããŠããããšãäŒãããŸãã
ããã«ãããã¹ã¯ãªãŒã³ãªãŒããŒãéããŠãã¿ã³ãäœãããã®ããç解ã§ããŸãã
aria-expanded
å±æ§ã䜿çšãã
aria-expanded
å±æ§ãšã¯ãå¶åŸ¡ããŠããå¥ã®èŠçŽ ã®ç¶æ
ã瀺ããã®ã§ãã
ä»åã®ã³ãŒãã§ã¯ã以äžã®ããã«èšè¿°ããŠããŸãã
<button aria-expanded="false">
ãã®å±æ§ã®ç¶æ
ïŒ true
ãŸã㯠false
ïŒã«ãã£ãŠãã¡ãã¥ãŒã®ç¶æ
ãäŒããããšãã§ããŸããJavaScript
ã§å±æ§ãå¶åŸ¡ããããšã§ãã¡ãã¥ãŒã®ãŠãŒã¶ããªãã£ãšã¢ã¯ã»ã·ããªãã£ãåäžããŸãã
JavaScriptã§å±æ§ãåãæ¿ãã
jQuery
ã䜿çšããŠããã³ããŒã¬ãŒãã¿ã³ãã¯ãªãã¯ããéã«aria-expanded
ã®å€ãå€æŽããŸãã
ããã«ãããã¡ãã¥ãŒã®ééç¶æ
ãåæ ãããŸãã
å ·äœçã«ã¯ã以äžã®ã³ãŒãã§å¶åŸ¡ããŠããŸãã
function toggleMenu(closeOnly) {
var isExpanded = $drawerBtn.attr("aria-expanded") === "true";
if (closeOnly && !isExpanded) {
return; // ã¡ãã¥ãŒãæ¢ã«éããŠããå Žåã¯äœãããªã
}
$drawerBtn.attr("aria-expanded", !isExpanded);
ããã«ããããã¿ã³ãã¯ãªãã¯ããããš aria-expanded
å±æ§ããã§ãã¯ããå€ãå転ãããŸãã
ãã®å¶åŸ¡ã«ãããã¹ã¯ãªãŒã³ãªãŒããŒãéããŠã¡ãã¥ãŒã®çŸåšã®ç¶æ ãç¥ãããšãã§ããŸãã
ãã®ããã«ãJavaScript
ãå©çšããŠaria-expanded
å±æ§ã管çããããšã¯ããŠãŒã¶ããªãã£ãšã¢ã¯ã»ã·ããªãã£ãåäžãããããã«éèŠã§ãã
ã¡ãã¥ãŒå€ãã¯ãªãã¯ãããšã¡ãã¥ãŒãéããããããã«ãã
åºæ¬ã¯ãã³ããŒã¬ãŒãã¿ã³ã§ã¡ãã¥ãŒã®ééãå¶åŸ¡ããŸãããããã ãã§ã¯äžäŸ¿ã«æããæ¹ãããŸãã
ã¡ãã¥ãŒå€ã¯ãªãã¯ã§ã¡ãã¥ãŒãéããããããã«ãããšã䜿ãããããããã«åäžããŸãã
HTML
ãšjQuery
ã§ç°¡åã«å®è£
ã§ããŸãã®ã§ãå
·äœçãªæ¹æ³ã玹ä»ããŸãã
HTML
ã¯ã¡ãã¥ãŒã®èæ¯ãšãªã.l-drawer-nav__bg
ãã¡ãã¥ãŒå
ã«é
眮ããŸãã
ãã®èæ¯éšåãã¯ãªãã¯ãããšãã¡ãã¥ãŒãéããããã«ããŠãããŸãã
<div class="l-drawer-nav__bg"></div>
jQuery
ã§ã¯ã.l-drawer-nav__bg
ã¯ãªãã¯æã«ã¡ãã¥ãŒãéããåŠçãèšè¿°ããŸãã
var $drawerNavBg = $(".l-drawer-nav__bg");
$drawerNavBg.click(function () {
toggleMenu(true);
});
ãã®ããã«ãã¡ãã¥ãŒå€ãã¯ãªãã¯ããããšã§ã¡ãã¥ãŒãéããæ©èœã¯ããŠã§ããµã€ãã®äœ¿ãããããåäžãããããã®éèŠãªèŠçŽ ã§ãã
TabããŒã§æäœã§ããããã«ãã
ãµã€ããTabããŒã§æäœã§ããããã«ããããšã¯ãããŒããŒãæäœãã人ã«ãšã£ãŠäœ¿ããããããäžã§éèŠã§ãã
å ·äœçã«ã¯ã以äžã®2ç¹ã確èªããŸãã
- ãã©ãŒã«ã¹ãèŠããããã«ãã
- ã¡ãã¥ãŒãéããŠãããšãã¯ãã¡ãã¥ãŒå€ã«ãã©ãŒã«ã¹ãåœãŠãªãããã«ãã
ãã©ãŒã«ã¹ãèŠããããã«ãã
TabããŒã§ã®æäœã«éèŠãªã®ã¯ãã©ã®èŠçŽ ããã©ãŒã«ã¹ãããŠãããç®èŠã§ããããšã§ããCSS
ã® :focus
ã¯ã©ã¹ã䜿çšããããšã§ãã©ã®èŠçŽ ããã©ãŒã«ã¹ãããŠãããç°¡åã«ç®èŠã§ããããã«ãã¶ã€ã³ã§ããŸãã
ããã©ã«ãã§ããã®CSSã¯åœãã£ãŠããŸããã䜿çšããŠãããªã»ããCSSã«ãã£ãŠã¯æ¶ãããŠããå¯èœæ§ããããŸãã
ã¡ãã¥ãŒãéããŠãããšãã¯ãã¡ãã¥ãŒå€ã«ãã©ãŒã«ã¹ãåœãŠãªãããã«ãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒãéããŠããæã¯ãJavaScriptã䜿çšããŠã¡ãã¥ãŒå
ã®èŠçŽ ã®ã¿ã«ãã©ãŒã«ã¹ãåœããããã«ããŸãã
ä»åã®äŸã§ã¯ã以äžã®ã³ãŒãã§å®è£
ããŠããŸãã
<div class="l-drawer-nav__bg" tabindex="0"></div>
// ããã²ãŒã·ã§ã³ã¡ãã¥ãŒã®èæ¯ã«ãã©ãŒã«ã¹ã移ã£ãæã®ã€ãã³ããã³ãã©
$drawerNavBg.on("focus", function () {
// ãã©ãŒã«ã¹ããã³ããŒã¬ãŒã¡ãã¥ãŒãã¿ã³ã«æ»ã
$drawerBtn.focus();
});
HTML
ã«ã¯tabindex="0"
ãæå®ãããã©ãŒã«ã¹å¯èœã«ããŠããŸããjQuery
ã§ã¯l-drawer-nav__bg
ã«ãã©ãŒã«ã¹ãåœãã£ãæã«ããã³ããŒã¬ãŒãã¿ã³ã«ãã©ãŒã«ã¹ãæ»ãããã«ããŠããŸãã
ããã§ããã³ããŒã¬ãŒã¡ãã¥ãŒãéããŠããæã¯ã¡ãã¥ãŒå€ã«ãã©ãŒã«ã¹ãåœãããªãããã«ãªããŸãã
ããã«ããããŠã§ããµã€ãã®ããã²ãŒã·ã§ã³ããã䜿ãããããªããŸãã
EscapeããŒã§ã¡ãã¥ãŒãéããããããã«ãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒãEscapeããŒã§ç°¡åã«éããããšãã§ããŸãã
ãã®æ©èœããããšãããŒããŒãã§æäœãã人ã¯ããªã䟿å©ã§ãã
ãã¡ããJavaScriptã§ç°¡åã«å®è£ ã§ããŸãã
// ããŒããŒãã€ãã³ããã³ãã©ãè¿œå
$(document).keydown(function (e) {
// EscããŒãæŒãããå Žåãã¡ãã¥ãŒãéãã
if (e.key === "Escape") {
toggleMenu(true);
}
});
ãã®ã³ãŒãã¯ããŠãŒã¶ãŒãEscapeããŒãæŒããšãtoggleMenu(true)
é¢æ°ãåŒã³åºãããã¡ãã¥ãŒãéããããã«ãªã£ãŠããŸãã
å®è£
ãã¢ãããäžåºŠèŒããŠãããŸãã
ãŠãŒã¶ããªãã£ãšã¢ã¯ã»ã·ããªãã£ã«é¢ããã³ãŒããå
¥ããŠããã®ã§ãjQuery
ãè€éã«ãªã£ãŠããŸããããã®å䜿ããããã¡ãã¥ãŒã«ãªã£ãŠããŸãã
See the Pen ããã¯ãŒãããã³ãã¬ãŒã by ããã (@k-suzuki3533) on CodePen.
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®ã«ã¹ã¿ãã€ãºæ¹æ³
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®ã«ã¹ã¿ãã€ãºæ¹æ³ã«ã€ããŠã¯ã以äžã®èšäºã§çŽ¹ä»ããŠããŸãã
å
·äœçãªã³ãŒããèŒããŠããã®ã§ãæ°ã«å
¥ã£ããã®ãã³ããããŠäœ¿çšããŠãã ããã
ãããã質å
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®èæ¯ãã¹ã¯ããŒã«ãããŠããŸããŸãã
ãã³ããŒã¬ãŒã¡ãã¥ãŒã®å±éæã«ã<body>
ã¿ã°ã«ãCSS
ãåœãŠãå¿
èŠããããŸãã
詳ããã¯ä»¥äžã®èšäºã§è§£èª¬ããŠããŸãã
ãŸãšã
ãã®èšäºã§ã¯ããã³ããŒã¬ãŒã¡ãã¥ãŒã®äœ¿ããããå®è£
æ¹æ³ã解説ããŸããã
éèŠãªãã€ã³ãã¯ä»¥äžã®ãšããã§ãã
aria-controls
ãšaria-expanded
å±æ§ã䜿çšããŠãã¡ãã¥ãŒã®ã¢ã¯ã»ã·ããªãã£ãé«ããã- ã¡ãã¥ãŒå€ãã¯ãªãã¯ãããšã¡ãã¥ãŒãéããæ©èœãå®è£ ããã
- TabããŒãšEscapeããŒã«ããæäœããµããŒãããããšã§ããã¹ãŠã®ãŠãŒã¶ãŒã«äœ¿ããããäœéšãæäŸããã
ããããèæ
®ããããšã§ããã䜿ãããããŠã§ããµã€ããäœããããã«ãªããŸãã
ãã²èªåã®ãŠã§ããµã€ãã§ãããã®ãã¯ããã¯ãè©ŠããŠã¿ãŠãã ããã