jquery制作弹出层带遮罩效果,点击阴影部分层消失。
整体还是比较简单的。
HTML代码很简单
<a href="#" class="big-link" data-reveal-id="myModal" data-animation="fade">jquery点我弹出</a>
<div id="myModal" class="reveal-modal">
<h1>jquery弹出层</h1>
<p>很不错的效果</p>
<a class="close-reveal-modal">×</a>
</div>
定义弹出层的样式
.reveal-modal-bg { position: fixed; height: 100%; width: 100%; z-index:; display: none; top:; left:; background:rgba(00, 00, 00, 0.8) } .reveal-modal { visibility: hidden; top: 150px; left: 50%; margin-left: -300px; width: 520px; position: absolute; z-index:; padding: 30px 40px 34px; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; -moz-box-shadow: 0 0 10px rgba(0,0,0,.4); -webkit-box-shadow: 0 0 10px rgba(0,0,0,.4); -box-shadow: 0 0 10px rgba(0,0,0,.4); background-color: #FFF; } .reveal-modal.small { width: 200px; margin-left: -140px;}
.reveal-modal.medium { width: 400px; margin-left: -240px;}
.reveal-modal.large { width: 600px; margin-left: -340px;}
.reveal-modal.xlarge { width: 800px; margin-left: -440px;} .reveal-modal .close-reveal-modal { font-size: 22px; line-height: 0.5; position: absolute; top: 8px; right: 11px; color: #333; text-shadow: 0 -1px 1px rbga(0,0,0,.6); font-weight: bold; cursor: pointer; }
重点是js
引用jquery库是必不可少的。
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
最后是js
(function($) { /*---------------------------
点击属性
----------------------------*/ $('a[data-reveal-id]').live('click', function(e) {
e.preventDefault();
var modalLocation = $(this).attr('data-reveal-id');
$('#'+modalLocation).reveal($(this).data());
}); /*---------------------------
扩展和执行方法
----------------------------*/ $.fn.reveal = function(options) { var defaults = {
animation: 'fadeAndPop', //动画效果
animationspeed: 300, //效果时间
closeonbackgroundclick: true, //如果你点击背景模态会关闭
dismissmodalclass: 'close-reveal-modal' //一个按钮或元素的类将关闭一个开放的模态
}; //Extend dem' options
var options = $.extend({}, defaults, options); return this.each(function() { /*---------------------------
全局变量 ----------------------------*/
var modal = $(this),
topMeasure = parseInt(modal.css('top')),
topOffset = modal.height() + topMeasure,
locked = false,
modalBG = $('.reveal-modal-bg'); /*---------------------------
创建背景
----------------------------*/
if(modalBG.length == 0) {
modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal);
} /*---------------------------
打开、关闭动画效果
----------------------------*/
//Entrance Animations
modal.bind('reveal:open', function () {
modalBG.unbind('click.modalEvent');
$('.' + options.dismissmodalclass).unbind('click.modalEvent');
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"top": $(document).scrollTop()+topMeasure + 'px',
"opacity" : 1
}, options.animationspeed,unlockModal());
}
if(options.animation == "fade") {
modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"opacity" : 1
}, options.animationspeed,unlockModal());
}
if(options.animation == "none") {
modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure});
modalBG.css({"display":"block"});
unlockModal()
}
}
modal.unbind('reveal:open');
}); //关闭
modal.bind('reveal:close', function () {
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"top": $(document).scrollTop()-topOffset + 'px',
"opacity" : 0
}, options.animationspeed/2, function() {
modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'});
unlockModal();
});
}
if(options.animation == "fade") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"opacity" : 0
}, options.animationspeed, function() {
modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure});
unlockModal();
});
}
if(options.animation == "none") {
modal.css({'visibility' : 'hidden', 'top' : topMeasure});
modalBG.css({'display' : 'none'});
}
}
modal.unbind('reveal:close');
}); /*---------------------------
添加开放和关闭监听器 ----------------------------*/
//立即开模态10:42:50
modal.trigger('reveal:open') //关闭
var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function () {
modal.trigger('reveal:close')
}); if(options.closeonbackgroundclick) {
modalBG.css({"cursor":"pointer"})
modalBG.bind('click.modalEvent', function () {
modal.trigger('reveal:close')
});
}
$('body').keyup(function(e) {
if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key
}); /*---------------------------
锁定动画
----------------------------*/
function unlockModal() {
locked = false;
}
function lockModal() {
locked = true;
} });//each call
}//orbit plugin call
})(jQuery);
OK,完成了,看下效果图。