bootstrapソース学習と例:bootstrap-collapse

8748 ワード

今回はアコーディオンのコンポーネントについて話していますが、bootstrapの実現はちょっとおかしいです.CSSのクラス名はaccordionを接頭辞に、JSの中の処理コンポーネントと関連方法はcollapseです.
HTML構造は以下の通りである:クラス名accordionのDIVは、accordion-groupというNクラス名のDIVを含み、各グループはaccordion-headingとaccordion-bodyの2つの部分に分かれている.accordion-headingにはトリガ用のaccordion-toggleが含まれており、accordion-bodyのaccordion-innerこそコンテンツを再生するためのものです.
       
, , , , , ? , , , 。
, 5A 100 , 10 80 230 。 , , 150 , 。 、 , 1.5 , , 。
, 24 。 , , “ ”。 9 , 31 279 。 , , 。

他のコンポーネントと同様に、参照するだけで使用できます.ただし、コールバックをバインドする場合は、バインドします.accordion-bodyの上にあります.bootstrapの公式チュートリアルはあまりよくできていないような気がしますが、イベントをバインドするクラス名が統一されておらず、そこに縛られていることが一目でわかります.しかし、ユーザーにHTMLを書くのはあまりよくありません.
!function ($) {

    "use strict"; // jshint ;_;


    /* COLLAPSE PUBLIC CLASS DEFINITION
  * ================================ */

    var Collapse = function (element, options) {
        this.$element = $(element)//   accordion-body
        this.options = $.extend({}, $.fn.collapse.defaults, options)

        if (this.options.parent) {
            this.$parent = $(this.options.parent)
        }

        this.options.toggle && this.toggle()
    }

    Collapse.prototype = {

        constructor: Collapse

        ,
        dimension: function () {
            var hasWidth = this.$element.hasClass('width')
            return hasWidth ? 'width' : 'height'
        }

        ,
        show: function () {
            var dimension
            , scroll
            , actives
            , hasData

            if (this.transitioning) return

            dimension = this.dimension()
            //      width  
            scroll = $.camelCase(['scroll', dimension].join('-'))//  scrollWidth  scrollHeight
            //                 
            actives = this.$parent && this.$parent.find('> .accordion-group > .in')
            //      
            if (actives && actives.length) {
                hasData = actives.data('collapse')
                if (hasData && hasData.transitioning) return
                actives.collapse('hide')//    ,        
                hasData || actives.data('collapse', null)
            }
            //             
            this.$element[dimension](0)
            //         
            this.transition('addClass', $.Event('show'), 'shown')

            $.support.transition && this.$element[dimension](this.$element[0][scroll])
        }

        ,
        hide: function () {
            var dimension
            if (this.transitioning) return
            dimension = this.dimension()
            this.reset(this.$element[dimension]())
            this.transition('removeClass', $.Event('hide'), 'hidden')
            this.$element[dimension](0)
        }

        ,
        reset: function (size) {
            var dimension = this.dimension()

            this.$element
            .removeClass('collapse')
            [dimension](size || 'auto')//        
            [0].offsetWidth

            this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')

            return this
        }

        ,
        transition: function (method, startEvent, completeEvent) {
            var that = this
            , complete = function () {
                if (startEvent.type == 'show') that.reset()
                that.transitioning = 0
                that.$element.trigger(completeEvent)
            }

            this.$element.trigger(startEvent)

            if (startEvent.isDefaultPrevented()) return

            this.transitioning = 1
            //       in
            this.$element[method]('in')

            $.support.transition && this.$element.hasClass('collapse') ?
            this.$element.one($.support.transition.end, complete) :
            complete()
        }

        ,
        toggle: function () {//      
            this[this.$element.hasClass('in') ? 'hide' : 'show']()
        }

    }


    /* COLLAPSE PLUGIN DEFINITION
  * ========================== */

    var old = $.fn.collapse

    $.fn.collapse = function (option) {
        return this.each(function () {
            var $this = $(this)
            , data = $this.data('collapse')
            , options = typeof option == 'object' && option
            if (!data) $this.data('collapse', (data = new Collapse(this, options)))
            if (typeof option == 'string') data[option]()
        })
    }

    $.fn.collapse.defaults = {
        toggle: true
    }

    $.fn.collapse.Constructor = Collapse


    /* COLLAPSE NO CONFLICT
  * ==================== */

    $.fn.collapse.noConflict = function () {
        $.fn.collapse = old
        return this
    }


    /* COLLAPSE DATA-API
  * ================= */

    $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
        var $this = $(this), href
        , target = $this.attr('data-target') //            ,1  'data-target'
        || e.preventDefault() //2  href
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
        , option = $(target).data('collapse') ? 'toggle' : $this.data()
        $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')//? CSS        
        $(target).collapse(option)//       bootstrap    ,             
    })

}(window.jQuery);
bootstrap学習by司徒正美 $(function () { var log = function(s){ window.console && console.log(s) } $(".accordion-body").on("show", function(){ log("show") }).on("shown", function(){ log("shown") }).on("hide", function(){ log("hide") }).on("hidden", function(){ log("hidden") }ある国のネットユーザーが国土問題で中国のネットユーザーとけんかをしている間に、私は北京に電話します.中国のネットユーザーはとても淡々と答えています.あなたの経済レベルでは、通行料を払うことができますか.この2,3日の新しいネタによると、李白が今日を生きていたら、大半以上の詩が書けないだろう.名山大川の入場券が買えないからだ. 国土问题 現在、中国の5 A級観光地の半数は100元に達し、黄山の入場券は10年来80元から230元に上昇した.山東曲阜によると、同類の観光地に比べて料金が低く、150元しかかからず、運賃を上げずに身を投げたという.曲阜の孔廟、孔府と孔林は、年収1億5000万元前後で、すべて地方財政を納付したが、観光地の維持コストは公開されていない. 门票问题 学者の楊支柱は二人の子供を産んだため公職を取り消され、24万元余りの罰金を科された.彼は、計画出産の罰金は以前は直接超生罰金と呼ばれ、WTO加盟後は「社会扶養費」に変更されたと述べた.9省・市の超過罰金の平均数によると、全国31省・市が毎年徴収する超過罰金は279億元に達する.このうち大都市ではこの収入を財政に上納し、地方では分配が混乱し、一部の罰金が行方不明になっている.
実行コード
導入されたlessファイルはaccordionです.less
bootstrapのオルガンは垂直形式でしか現れない.