溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Bootstrap中模態(tài)窗口源碼的示例分析

發(fā)布時間:2021-07-07 11:25:48 來源:億速云 閱讀:144 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Bootstrap中模態(tài)窗口源碼的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內(nèi)容如下

/* ========================================================================
 * 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的構(gòu)造函數(shù),里面聲明了需要用到的變量,隨后又設(shè)置了一些常量。
 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//忽略遮罩成點(diǎn)擊嗎,不忽略,即:點(diǎn)擊遮罩層退出模態(tài)

 if (this.options.remote) {//這是遠(yuǎn)端調(diào)用數(shù)據(jù)的情況,用遠(yuǎn)端模板來填充模態(tài)框
  this.$element
  .find('.modal-content')
  .load(this.options.remote, $.proxy(function () {
   this.$element.trigger('loaded.bs.modal')//觸發(fā)加載完數(shù)據(jù)時的監(jiān)聽函數(shù)
  }, this))
 }
 }

 Modal.VERSION = '3.3.7'

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

 Modal.DEFAULTS = {//defaults 默認(rèn)值
 backdrop: true,//有無遮罩層
 keyboard: true,//鍵盤上的 esc 鍵被按下時關(guān)閉模態(tài)框。
 show: true//模態(tài)框初始化之后就立即顯示出來。
 }
//變量設(shè)置完畢,接著就該上函數(shù)了。Modal的擴(kuò)展函數(shù)有這么幾個:
//toggel,show,hide,enforceFocus,escape,resize,hideModal,removeBackdrop,
//backdrop,handleUpdate,adjustDialog,resetAdjustments,checkScrollbar,setScrollbar,resetScrollbar,
//measureScrollbar終于列完了,恩一共是16個。toggel函數(shù)比較簡單,是一個顯示和隱藏的切換函數(shù)。代碼如下
 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 })//觸發(fā) 尾行注冊的show.bs.modal事件,并給relatedTarget賦值 $.Event 創(chuàng)建事件對象的目的就是可以給他任意賦值

 this.$element.trigger(e)//

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

 this.isShown = true//標(biāo)記

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

 this.escape()//按esc退出模態(tài)
 this.resize()//窗口大小調(diào)整,窗口大小改變,模態(tài)框也跟著變

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

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

 this.backdrop(function () {//遮罩層:真的是一個壓軸函數(shù),,,,   這個回調(diào)函數(shù)是遮罩完畢后運(yùn)行的函數(shù)
  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()//調(diào)整對話框

  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)//模態(tài)過度完成后,觸發(fā)foucus 和shown.bs.modal
   })
   .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  that.$element.trigger('focus').trigger(e)//否則直接進(jìn)行
 })
 }

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

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

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

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

 this.isShown = false//恢復(fù)初始的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))//到了這里,雖然模態(tài)已經(jīng)沒有了,但僅僅是把透明度改為0了,this.hideModal才是真正移除
  .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  this.hideModal()
 }

 Modal.prototype.enforceFocus = function () {//模態(tài)框獲得焦點(diǎn)
 $(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 鍵被按下時關(guān)閉模態(tài)框。
 if (this.isShown && this.options.keyboard) {//僅在模態(tài)窗顯示的時候才注冊這個事件
  this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {//不僅可以把事件穿過來,。。。牛
  e.which == 27 && this.hide()//27 時調(diào)用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'))//創(chuàng)建遮罩層div
  .addClass('modal-backdrop ' + animate)//添加 modal-backdrop 和fade class
  .appendTo(this.$body)//加到body底部下面(待定其他版本可能加在模態(tài)里面)

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

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

  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 () {//處理因?yàn)闈L動條而使模態(tài)位置的不和諧問題
 var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight//模態(tài)是否溢出屏幕,即高度大于客戶端高度

 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元素設(shè)置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')//如果是第二次打開模態(tài)窗口,這個數(shù)據(jù)才會有
  var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)//合并一下默認(rèn)參數(shù)
  //
  if (!data) $this.data('bs.modal', (data = new Modal(this, options)))//把modal對象存起來,避免第二次打開時在new對象,這點(diǎn)值得學(xué)習(xí)
  if (typeof option == 'string') data[option](_relatedTarget)/*這里是區(qū)分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代碼就實(shí)現(xiàn)modal的關(guān)鍵
 // ==============

 $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {//點(diǎn)擊按鈕的時候觸發(fā)模態(tài)框的東西,
 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())//這地方是區(qū)別一下第一次觸發(fā)和第二次觸發(fā)
  //到這里為止是為了得到被控制的modal的dom元素
 if ($this.is('a')) e.preventDefault()

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

}(jQuery);

關(guān)于“Bootstrap中模態(tài)窗口源碼的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI