手风琴

灵活的jQuery垂直手风琴插件

阿里云

这是一款十分灵活的 jQuery 垂直手风琴插件。之所以说该手风琴插件灵活,是因为可以通过参数设置,使它可以有多种显示形式:手风琴项同时显示多个或一次只显示一个。

HTML 代码:

html 结构使用一个 class 为 st-accordion 的 div 作为 wrapper,在里面放置一个无序列表。每一个无序列表项是一个手风琴项。span st-arrow 是手风琴右边的小箭头。

也想出现在这里?联系我们
创客主机
  1. <div id="st-accordion" class="st-accordion">
  2.     <ul>
  3.         <li>
  4.             <a href="#">
  5.                 Item Title
  6.                 <span class="st-arrow">Open or Close</span>
  7.             </a>
  8.             <div class="st-content">
  9.                 <p>Some content</p>
  10.             </div>
  11.         </li>
  12.         <li> ... </li>
  13.     </ul>
  14. </div>

JAVASCRIPT 代码:

下面是该手风琴插件的可用参数:

  1. $.Accordion.defaults        = {
  2.     // index of opened item. -1 means all are closed by default.
  3.     open            : -1,
  4.     // if set to true, only one item can be opened. 
  5.     // Once one item is opened, any other that is 
  6.     // opened will be closed first
  7.     oneOpenedItem   : false,
  8.     // speed of the open / close item animation
  9.     speed           : 600,
  10.     // easing of the open / close item animation
  11.     easing          : 'easeInOutExpo',
  12.     // speed of the scroll to action animation
  13.     scrollSpeed     : 900,
  14.     // easing of the scroll to action animation
  15.     scrollEasing    : 'easeInOutExpo'
  16. };

通过 init 函数来初始化该手风琴插件:

  1. _init : function( options ) {
  2.     this.options = $.extend( true, {}, $.Accordion.defaults, options );
  3.     // validate options
  4.     this._validate();
  5.     // current is the index of the opened item
  6.     this.current        = this.options.open;
  7.     // hide the contents so we can fade it in afterwards
  8.     this.$items.find('div.st-content').hide();
  9.     // save original height and top of each item    
  10.     this._saveDimValues();
  11.     // if we want a default opened item...
  12.     if( this.current != -1 )
  13.         this._toggleItem( this.$items.eq( this.current ) );
  14.     // initialize the events
  15.     this._initEvents();   
  16. },

_saveDimValues 函数保存了手风琴项原来的高度,通过它我们可以知道在打开一个手风琴项时需要滚动多少高度。如果我们设置一个手风琴项以默认方式打开,那么 _toggleItem 函数将被调用。接下来是初始化所有的事件:
_toggleItem 函数负责协调手风琴项被点击时的事件。如果有一个手风琴项已经被打开(该项拥有 class 为 st-open),我们将设置 current 为-1,将高度设置为原始高度。如果点击的手风琴项是关闭的,那么设置 current 为当前项,然后使它的高度移动到适合内容的高度。通过 _scroll 函数将页面滚动到当前手风琴项的位置。

  1. _toggleItem         : function( $item ) {
  2.  
  3.     var $content = $item.find('div.st-content');
  4.  
  5.     ( $item.hasClass( 'st-open' ) ) 
  6.  
  7.         ? ( this.current = -1, $content.stop(true, true).fadeOut( this.options.speed ), $item.removeClass( 'st-open' ).stop().animate({
  8.             height  : $item.data( 'originalHeight' )
  9.         }, this.options.speed, this.options.easing ) )
  10.  
  11.         : ( this.current = $item.index(), $content.stop(true, true).fadeIn( this.options.speed ), $item.addClass( 'st-open' ).stop().animate({
  12.             height  : $item.data( 'originalHeight' ) + $content.outerHeight( true )
  13.         }, this.options.speed, this.options.easing ), this._scroll( this ) )
  14.  
  15. },

在 _initEvents 函数中初始化了两个事件:点击一个手风琴项和窗口的 resize。当点击一个手风琴项时,_toggleItem 函数被调用,如果我们设置 oneOpenedItem 为 true,那么其它被打开的手风琴项先关闭在打开当前的手风琴项。

  1. _initEvents         : function() {
  2.     var instance    = this;
  3.     // open / close item
  4.     this.$items.find('a:first').bind('click.accordion', function( event ) {
  5.         var $item           = $(this).parent();
  6.         // close any opened item if oneOpenedItem is true
  7.         if( instance.options.oneOpenedItem && instance._isOpened() && instance.current!== $item.index() ) {
  8.  
  9.             instance._toggleItem( instance.$items.eq( instance.current ) );
  10.         }
  11.         // open / close item
  12.         instance._toggleItem( $item );
  13.         return false;
  14.     });
  15.     $(window).bind('smartresize.accordion', function( event ) {
  16.         // reset original item values
  17.         instance._saveDimValues();
  18.         // reset the content's height of any item that is currently opened
  19.         instance.$el.find('li.st-open').each( function() {       
  20.             var $this   = $(this);
  21.             $this.css( 'height', $this.data( 'originalHeight' ) + $this.find('div.st-content').outerHeight( true ) );       
  22.         });         
  23.         // scroll to current
  24.         if( instance._isOpened() )
  25.         instance._scroll();         
  26.     });   
  27. },

灵活的 jQuery 垂直手风琴插件

已有 347 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!