Bootstrap 模態窗口源碼分析


前言:

bootstrap的 js插件的源碼寫的非常好,也算是編寫jquery插件的模范寫法,本來還想大篇詳細的分析一下呢,唉,沒時間啊,很早之前看過的源碼了,現在貼在了博客上,

300來行的代碼,其中有很多jquery的高級用法,建議,從github上下載一下源碼,然后把本篇的代碼復制過去,然后,邊運行,邊閱讀,如果有不明白的地方,可以給我留言,我給解答。

下面是基本每行都加了注釋

/* ========================================================================
 * Bootstrap: modal.js v3.3.7
 * http://getbootstrap.com/javascript/#modals
 * ========================================================================
 * Copyright 2011-2016 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * ======================================================================== */


+function ($) {
  'use strict';

  // MODAL CLASS DEFINITION
  // ======================

  var Modal = function (element, options) {//modal類:首先是Modal的構造函數,里面聲明了需要用到的變量,隨后又設置了一些常量。
    this.options             = options
    this.$body               = $(document.body)
    this.$element            = $(element)
    this.$dialog             = this.$element.find('.modal-dialog')
    this.$backdrop           = null
    this.isShown             = null
    this.originalBodyPad     = null
    this.scrollbarWidth      = 0
    this.ignoreBackdropClick = false//忽略遮罩成點擊嗎,不忽略,即:點擊遮罩層退出模態

    if (this.options.remote) {//這是遠端調用數據的情況,用遠端模板來填充模態框
      this.$element
        .find('.modal-content')
        .load(this.options.remote, $.proxy(function () {
          this.$element.trigger('loaded.bs.modal')//觸發加載完數據時的監聽函數
        }, this))
    }
  }

  Modal.VERSION  = '3.3.7'

  Modal.TRANSITION_DURATION = 300   //transition duration   過度時間
  Modal.BACKDROP_TRANSITION_DURATION = 150      //背景過度時間

  Modal.DEFAULTS = {//defaults  默認值
    backdrop: true,//有無遮罩層
    keyboard: true,//鍵盤上的 esc 鍵被按下時關閉模態框。
    show: true//模態框初始化之后就立即顯示出來。
  }
//變量設置完畢,接着就該上函數了。Modal的擴展函數有這么幾個:
//toggel,show,hide,enforceFocus,escape,resize,hideModal,removeBackdrop,
//backdrop,handleUpdate,adjustDialog,resetAdjustments,checkScrollbar,setScrollbar,resetScrollbar,
//measureScrollbar終於列完了,恩一共是16個。toggel函數比較簡單,是一個顯示和隱藏的切換函數。代碼如下
  Modal.prototype.toggle = function (_relatedTarget) {
    return this.isShown ? this.hide() : this.show(_relatedTarget)
  }

  Modal.prototype.show = function (_relatedTarget) {
    var that = this
    var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })//觸發   尾行注冊的show.bs.modal事件,並給relatedTarget賦值   $.Event  創建事件對象的目的就是可以給他任意賦值

    this.$element.trigger(e)//

    if (this.isShown || e.isDefaultPrevented()) return//如果已經顯示就返回

    this.isShown = true//標記

    this.checkScrollbar()//核對是否有滾動條,並且測量滾動條寬度(非x軸)
    this.setScrollbar()//如果有滾動條,就給body一個padding-right   是一個滾動條的寬度,這一步的目的是為了呼應,下面的去掉y軸滾動條
    this.$body.addClass('modal-open')//接着為body元素添加modal-open類、即去掉y軸滾動條,頁面就會往右一個滾動條的寬度,

    this.escape()//按esc退出模態
    this.resize()//窗口大小調整,窗口大小改變,模態框也跟着變

    this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))//注冊右上角關閉事件

    this.$dialog.on('mousedown.dismiss.bs.modal', function () {//在dialog中按下鼠標,在父元素中抬起         忽略: 在模態中按下鼠標,在遮罩層中抬起鼠標
      that.$element.one('mouseup.dismiss.bs.modal', function (e) {//在父元素中抬起
        if ($(e.target).is(that.$element)) that.ignoreBackdropClick = true
      })
    })

    this.backdrop(function () {//遮罩層:真的是一個壓軸函數,,,,         這個回調函數是遮罩完畢后運行的函數
      var transition = $.support.transition && that.$element.hasClass('fade')

      if (!that.$element.parent().length) {
        that.$element.appendTo(that.$body) // don't move modals dom position
      }

      that.$element
        .show()
        .scrollTop(0)//show  並且   弄到頂部

      that.adjustDialog()//調整對話框

      if (transition) {
        that.$element[0].offsetWidth // force reflow
      }

      that.$element.addClass('in')

      that.enforceFocus()

      var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })

      transition ?
        that.$dialog // wait for modal to slide in
          .one('bsTransitionEnd', function () {
            that.$element.trigger('focus').trigger(e)//模態過度完成后,觸發foucus 和shown.bs.modal
          })
          .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
        that.$element.trigger('focus').trigger(e)//否則直接進行
    })
  }

  Modal.prototype.hide = function (e) {//他的存在就是一個事件監聽函數,所以可以加e    下面是模態窗口關閉處理函數
    if (e) e.preventDefault()//取消默認行為

    e = $.Event('hide.bs.modal')//無論什么事件進入這里都換成    'hide.bs.modal'  媽的這樣就通用了,,,無論是點擊“x”,還是取消,確定,都這么處理,

    this.$element.trigger(e)//處發hide

    if (!this.isShown || e.isDefaultPrevented()) return

    this.isShown = false//恢復初始的false

    this.escape()//移除esc事件
    this.resize()//移除為window綁定的resize事件

    $(document).off('focusin.bs.modal')//

    this.$element
      .removeClass('in')
      .off('click.dismiss.bs.modal')
      .off('mouseup.dismiss.bs.modal')

    this.$dialog.off('mousedown.dismiss.bs.modal')//該移除的都移除

    $.support.transition && this.$element.hasClass('fade') ?
      this.$element
        .one('bsTransitionEnd', $.proxy(this.hideModal, this))//到了這里,雖然模態已經沒有了,但僅僅是把透明度改為0了,this.hideModal才是真正移除
        .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
      this.hideModal()
  }

  Modal.prototype.enforceFocus = function () {//模態框獲得焦點
    $(document)
      .off('focusin.bs.modal') // guard against infinite focus loop
      .on('focusin.bs.modal', $.proxy(function (e) {
        if (document !== e.target &&
            this.$element[0] !== e.target &&
            !this.$element.has(e.target).length) {
          this.$element.trigger('focus')
        }
      }, this))
  }

  Modal.prototype.escape = function () {//鍵盤上的 esc 鍵被按下時關閉模態框。
    if (this.isShown && this.options.keyboard) {//僅在模態窗顯示的時候才注冊這個事件
      this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {//不僅可以把事件穿過來,。。。牛
        e.which == 27 && this.hide()//27  時調用hide方法(ps:比if省事多了,高手就是高手)
      }, this))
    } else if (!this.isShown) {//否則移除他,感覺怪怪的,不管了
      this.$element.off('keydown.dismiss.bs.modal')
    }
  }

  Modal.prototype.resize = function () {//為你window resize注冊事件
    if (this.isShown) {
      $(window).on('resize.bs.modal', $.proxy(this.handleUpdate, this))
    } else {
      $(window).off('resize.bs.modal')
    }
  }

  Modal.prototype.hideModal = function () {
    var that = this
    this.$element.hide()
    this.backdrop(function () {
      that.$body.removeClass('modal-open')
      that.resetAdjustments()
      that.resetScrollbar()
      that.$element.trigger('hidden.bs.modal')
    })
  }

  Modal.prototype.removeBackdrop = function () {
    this.$backdrop && this.$backdrop.remove()
    this.$backdrop = null
  }

  Modal.prototype.backdrop = function (callback) {
    var that = this
    var animate = this.$element.hasClass('fade') ? 'fade' : ''//是否有fade動畫類

    if (this.isShown && this.options.backdrop) {//條件:正在show,並且有遮罩層
      var doAnimate = $.support.transition && animate//  do的條件是支持css3過度和有fade class

      this.$backdrop = $(document.createElement('div'))//創建遮罩層div
        .addClass('modal-backdrop ' + animate)//添加 modal-backdrop  和fade   class
        .appendTo(this.$body)//加到body底部下面(待定其他版本可能加在模態里面)

      this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {//點擊模態窗口處理事件:
        if (this.ignoreBackdropClick) {
          this.ignoreBackdropClick = false
          return
        }
        if (e.target !== e.currentTarget) return//如果沒有點擊模態,則不做處理
        this.options.backdrop == 'static'
          ? this.$element[0].focus()//指定靜態的背景下,不關閉模式點擊
          : this.hide()//否則   關閉模態
      }, this))

      if (doAnimate) this.$backdrop[0].offsetWidth // force reflow       英文翻譯  強迫回流,,,先不管

      this.$backdrop.addClass('in')//添加in   0.5的透明度

      if (!callback) return

      doAnimate ?
        this.$backdrop
          .one('bsTransitionEnd', callback)
          .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) ://如果有fade動畫的話 就給遮罩層綁定一個遮罩過度時間,為什么要這么寫以后聊
        callback()

    } else if (!this.isShown && this.$backdrop) {
      this.$backdrop.removeClass('in')

      var callbackRemove = function () {
        that.removeBackdrop()
        callback && callback()
      }
      $.support.transition && this.$element.hasClass('fade') ?
        this.$backdrop
          .one('bsTransitionEnd', callbackRemove)
          .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) :
        callbackRemove()

    } else if (callback) {
      callback()
    }
  }

  // these following methods are used to handle overflowing modals

  Modal.prototype.handleUpdate = function () {
    this.adjustDialog()
  }

  Modal.prototype.adjustDialog = function () {//處理因為滾動條而使模態位置的不和諧問題
    var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight//模態是否溢出屏幕,即高度大於客戶端高度

    this.$element.css({
      paddingLeft:  !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '',
      paddingRight: this.bodyIsOverflowing && !modalIsOverflowing ? this.scrollbarWidth : ''
    })
  }

  Modal.prototype.resetAdjustments = function () {
    this.$element.css({
      paddingLeft: '',
      paddingRight: ''
    })
  }

  Modal.prototype.checkScrollbar = function () {
    var fullWindowWidth = window.innerWidth
    if (!fullWindowWidth) { // workaround for missing window.innerWidth in IE8
      var documentElementRect = document.documentElement.getBoundingClientRect()
      fullWindowWidth = documentElementRect.right - Math.abs(documentElementRect.left)
    }
    this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth//即是否有滾動條
    this.scrollbarWidth = this.measureScrollbar()
  }

  Modal.prototype.setScrollbar = function () {//用來為body元素設置padding-right的值,防止body的元素被scrollbar阻擋
    var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
    this.originalBodyPad = document.body.style.paddingRight || ''
    if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
  }

  Modal.prototype.resetScrollbar = function () {
    this.$body.css('padding-right', this.originalBodyPad)
  }

  Modal.prototype.measureScrollbar = function () { // thx walsh  測量   Scrollbar
    var scrollDiv = document.createElement('div')
    scrollDiv.className = 'modal-scrollbar-measure'
    this.$body.append(scrollDiv)
    var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
    this.$body[0].removeChild(scrollDiv)
    return scrollbarWidth
  }


  // MODAL PLUGIN DEFINITION
  // =======================

  function Plugin(option, _relatedTarget) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.modal')//如果是第二次打開模態窗口,這個數據才會有
      var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)//合並一下默認參數
      //
      if (!data) $this.data('bs.modal', (data = new Modal(this, options)))//把modal對象存起來,避免第二次打開時在new對象,這點值得學習
      if (typeof option == 'string') data[option](_relatedTarget)/*這里是區分option是對象和字符串的情況*/
      else if (options.show) data.show(_relatedTarget)
    })
  }

  var old = $.fn.modal

  $.fn.modal             = Plugin
  $.fn.modal.Constructor = Modal


  // MODAL NO CONFLICT
  // =================

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


  // MODAL DATA-API    這里是不用一行js代碼就實現modal的關鍵
  // ==============

  $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {//點擊按鈕的時候觸發模態框的東西,
    var $this   = $(this)
    var href    = $this.attr('href')
    var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
    var option  = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())//這地方是區別一下第一次觸發和第二次觸發
      //到這里為止是為了得到被控制的modal的dom元素
    if ($this.is('a')) e.preventDefault()

    $target.one('show.bs.modal', function (showEvent) {//調用show方法后立即執行的事件
      if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
      $target.one('hidden.bs.modal', function () {//調用show后創建的事件,模態框隱藏后觸發,
        $this.is(':visible') && $this.trigger('focus')//如果原來的按鈕還存在的(或顯示的)話,那就讓他得到焦點
      })
    })
    Plugin.call($target, option, this)
  })

}(jQuery);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM