您的位置:首页 > Web前端 > BootStrap

bootstrap源码学习之消息提示alert(一)

2016-01-18 10:57 639 查看
/* ========================================================================
* Bootstrap: alert.js v3.3.6
* http://getbootstrap.com/javascript/#alerts * ========================================================================
* Copyright 2011-2016 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */

+function ($) { //这种写法称为IIFE(Imdiately Invoked Function Expression 立即执行的函数表达式)
'use strict';//进入"严格模式"的编译指示(pragma),将"use strict"放在函数体的第一行,则整个函数以"严格模式"运行

// ALERT CLASS DEFINITION
// ======================

var dismiss = '[data-dismiss="alert"]'//可取消(dismiss)功能:向关闭按钮添加 data-dismiss="alert",就会自动为警告框添加关闭功能。
var Alert   = function (el) {
$(el).on('click', dismiss, this.close)
}

Alert.VERSION = '3.3.6'

Alert.TRANSITION_DURATION = 150//让过渡效果持续150 秒

Alert.prototype.close = function (e) {
var $this    = $(this)//这一步是将所点击的按钮元素赋值给变量
var selector = $this.attr('data-target')//取得对应的面板的CSS表达式

if (!selector) {
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
}

var $parent = $(selector)

if (e) e.preventDefault()

if (!$parent.length) {
$parent = $this.closest('.alert')
}

$parent.trigger(e = $.Event('close.bs.alert'))

if (e.isDefaultPrevented()) return

$parent.removeClass('in')

function removeElement() {
// detach from parent, fire event then clean up data
$parent.detach().trigger('closed.bs.alert').remove()
}

$.support.transition && $parent.hasClass('fade') ?
$parent
.one('bsTransitionEnd', removeElement)
.emulateTransitionEnd(Alert.TRANSITION_DURATION) :
removeElement()
}

// ALERT PLUGIN DEFINITION
// =======================

function Plugin(option) {
return this.each(function () {
var $this = $(this)
var data  = $this.data('bs.alert')

if (!data) $this.data('bs.alert', (data = new Alert(this)))
if (typeof option == 'string') data[option].call($this)
})
}

var old = $.fn.alert//备份

$.fn.alert             = Plugin
$.fn.alert.Constructor = Alert

// ALERT NO CONFLICT
// =================

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

// ALERT DATA-API
// ==============

$(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)

}(jQuery);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: