bootstrap的 js插件的源碼寫的很是好,也算是編寫jquery插件的模範寫法,原本還想大篇詳細的分析一下呢,唉,沒時間啊,很早以前看過的源碼了,如今貼在了博客上,javascript
300來行的代碼,其中有不少jquery的高級用法,建議,從github上下載一下源碼,而後把本篇的代碼複製過去,而後,邊運行,邊閱讀,若是有不明白的地方,能夠給我留言,我給解答。css
下面是基本每行都加了註釋java
/* ======================================================================== * 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);