打开手风琴的第一项

时间:2013-01-29 19:42:06

标签: javascript jquery accordion jquery-ui-accordion

我正在为一个项目使用手风琴,并设法默认打开一个项目 但是,由于某种原因,该项目始终是手风琴的最后一项。

如何将它作为手风琴中的第一项?

这是我的代码:

$.Accordion.prototype = {
    _init: function (options) {
        this.options = $.extend(true, {}, $.Accordion.defaults, options);
        // validate options
        this._validate();
        // current is the index of the opened item
        this.current = this.options.open;
        // hide the contents so we can fade it in afterwards
        this.$items.find('div.accordion-content').hide();
        // save original height and top of each item  
        this._saveDimValues();
        // if we want a default opened item...
        if (this.current != 1) this._toggleItem(this.$items.eq(this.current));
        // initialize the events
        this._initEvents();
    },
    _saveDimValues: function () {

        this.$items.each(function () {

            var $item = $(this);

            $item.data({
                originalHeight: $item.find('a:first').height(),
                offsetTop: $item.offset().top
            });

        });

    },
    // validate options
    _validate: function () {

        // open must be between -1 and total number of items, otherwise we set it to -1
        if (this.options.open < -1 || this.options.open > this.itemsCount - 1) this.options.open = -1;

    },
    _initEvents: function () {

        var instance = this;

        // open / close item
        this.$items.find('a:first').bind('click.accordion', function (event) {

            var $item = $(this).parent();

            // close any opened item if oneOpenedItem is true
            if (instance.options.oneOpenedItem && instance._isOpened() && instance.current !== $item.index()) {

                instance._toggleItem(instance.$items.eq(instance.current));

            }

            // open / close item
            instance._toggleItem($item);

            return false;

        });

        $(window).bind('smartresize.accordion', function (event) {

            // reset orinal item values
            instance._saveDimValues();

            // reset the content's height of any item that is currently opened
            instance.$el.find('li.accordion-open').each(function () {

                var $this = $(this);
                $this.css('height', $this.data('originalHeight') + $this.find('div.accordion-content').outerHeight(true));

            });

            // scroll to current
            if (instance._isOpened()) instance._scroll();

        });

    },
    // checks if there is any opened item
    _isOpened: function () {
        return (this.$el.find('li.accordion-open').length > 0);
    },
    // open / close item
    _toggleItem: function ($item) {

        var $content = $item.find('div.accordion-content');

        ($item.hasClass('accordion-open'))

        ? (this.current = -1, $content.stop(true, true).fadeOut(this.options.speed), $item.removeClass('accordion-open').stop().animate({
            height: $item.data('100px')
        }, this.options.speed, this.options.easing))

        : (this.current = $item.index(), $content.stop(true, true).fadeIn(this.options.speed), $item.addClass('accordion-open').stop().animate({
            height: $item.data('100px') + $content.outerHeight(true)
        }, this.options.speed, this.options.easing), this._scroll(this))
    },
    // scrolls to current item or last opened item if current is -1
    _scroll: function (instance) {
        var instance = instance || this,
            current;

        (instance.current !== -1) ? current = instance.current : current = instance.$el.find('li.accordion-open:last').index();

    }
}

0 个答案:

没有答案