您可以创建一个模态 DIV,其位置固定为在弹出窗口出现时显示并在弹出窗口消失时隐藏它
CSS:
<style type="text/css">
.modal {
position: fixed;
top: 0;
left: 0;
background: #000;
opacity: 0.6;
display: none;
z-index: 100; /* play with this param to show the modal behind the popup and above the page content */
}
</style>
脚本:
$(function() {
function Modal() {
this.$el = $('<div class="modal" />');
$('body').append(this.$el);
this.$el.on('click', this.preventEvent.bind(this));
$(window).on('resize', this.resizeModal.bind(this));
}
Modal.prototype.show = function() {
this.$el.css('width', $(window).width() + 'px');
this.$el.css('height', $(window).height() + 'px');
this.$el.show();
}
Modal.prototype.hide = function() {
this.$el.hide();
}
Modal.prototype.resizeModal = function() {
this.$el.css('width', $(window).width() + 'px');
this.$el.css('height', $(window).height() + 'px');
}
// prevent background event
Modal.prototype.preventEvent = function(e) {
e.preventDefault();
e.stopPropagation();
}
var modal = new Modal();
// when you are showing your popup - modal.show();
// when you are hiding your popup - modal.hide();
});