アコーディオンメニューを実装するjQueryプラグイン-ddaccordion.js

wordpressのダッシュボードのメニューのようにメニューをアコーディオンで展開するjQueryのプラグインです。
オプションで最後にクリックした要素をcookieに保存しページを移動しても最後にクリックした要素を開いた状態にしてくれるというのがあるようです。

■配布先 → Glossy Accordion Menu

ヘッダー内でjquery本体とddaccordion.jsを読み込みし、その際にクレジットを記述しなくてはいけないようです。

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="ddaccordion.js">
/***********************************************
* Accordion Content script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
***********************************************/
</script>

配布先サイトのスクリプトの呼び出しの記述例です↓

<script type="text/javascript">
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["suffix", "<img src='plus.gif' class='statusicon' />", "<img src='minus.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})
</script>

↓オプションの指定

headerclass: メニューでアコーディオンを展開する親要素に付けるclass
contentclass: 子メニューとして展開する要素のclass
collapseprev: true=一つのボックスだけ展開 false=複数のボックスを展開できる
revealtype: 動作トリガ:"click", "clickgo", "mouseover"
mouseoverdelay: 動作トリガ="mouseover" の場合、アクション起こす最初の時間
defaultexpanded: [0] 読み込み後に展開しておくボックスの指定。クッキーで保存して再訪問した際にクッキーが残ってれば有効になる
animatedefault:  ロード後に開くカテゴリまたはクッキーがある場合に、trueはアニメーションして開く。falseはアニメーションせずに開く。
persiststate: クッキーに開かれたカテゴリを登録する true:登録する。 false:登録しない。
toggleclass: 二つのCSS classを指定できる。最初は閉じている状態、次が開いている状態のclassを記述
togglehtml: 閉じている・開いている時に画像を指定する["suffix", "閉じた状態のアイコン" , "開いた状態のアイコン"]となる。["prefix", "", ""]とすると何も表示しない。
animatespeed: 開くアニメーションのスピード。"fast", "normal", or "slow"

↓↓↓デモを作ってみました。
demoページ

デモページのコードです
↓head部分

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Jquery ddaccordion.js|Jquery ddaccordion.jsデモ</title>
<script type="text/javascript" src="js/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="js/ddaccordion.js">
</script>
<script type="text/javascript">
ddaccordion.init({
	headerclass: "submenuheader", 
	contentclass: "submenu",
	revealtype: "click", 
	mouseoverdelay: 200,
	collapseprev: false,
	defaultexpanded: [], 
	onemustopen: false, 
	animatedefault: false,
	persiststate: true,
	toggleclass: ["", ""],
	togglehtml: ["suffix", "<img src='down.png' class='statusicon' />", "<img src='up.png' class='statusicon' />"], 
	animatespeed: "slow",
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})
</script>
<link rel="stylesheet" href="style.css" type="text/css">
</head>

↓body部分

<div class="content">
  <ul class="menu">
    <li><a class="menuitem" href="#">メニュー01</a></li>
    <li><a class="menuitem submenuheader">メニュー02 サブメニューヘッダー</a></li>
    <div class="submenu">
      <ul>
        <li><a href="#">サブメニュー1-01</a></li>
        <li><a href="#">サブメニュー1-02</a></li>
        <li><a href="#">サブメニュー1-03</a></li>
        <li><a href="#">サブメニュー1-04</a></li>
      </ul>
    </div>
    <li><a class="menuitem" href="#">メニュー03</a></li>
    <li><a class="menuitem" href="#">メニュー04</a></li>
    <li><a class="menuitem submenuheader">メニュー05 サブメニューヘッダー</a></li>
    <div class="submenu">
      <ul>
        <li><a href="#">サブメニュー2-01</a></li>
        <li><a href="#">サブメニュー2-02</a></li>
      </ul>
    </div>
    <li><a class="menuitem" href="#">メニュー05</a></li>
  </ul>
</div>

Related Article

3 Comments & Tracbacks

  • Becky 2012-09-03 9:04 PM

    質問をお願いします。
    ddaccordion.jsの解説ありがとうございます。
    設置は完了し、クッキーは正常に動いています。
    質問なのですが、
    例えばトップページ(index.html)に移動したときにも、
    過去のクッキーで子リストが開いてしまうのですが、
    トップページだけ親しか開かない設定にする方法がありましたら教えてほしいのですが。

    Reply

Leave a Comment

Emailは公開されません。*は必須項目です。


*


Categorys

Tags

CSS3 ダッシュボード ヘッダー トラブル コードサンプル コンテンツ スライドショー jQueryプラグイン php 引っ越し 素材 お知らせ JavaScript Facebook CSS カテゴリー 投稿タイプ IE HTML5 Shareボタン seo レスポンシブ Photoshop タクソノミー ナビゲーション カスタムメニュー Git Macアプリ サイドバー WPセキュリティ SVG iTunes PHPリファレンス API WP使い方 query_posts データベース get_posts() 条件分岐 コメント function RSS スマートフォン Sass/Compass さくらVPS