基本上,这是通过将 a 覆盖div
在所有当前屏幕元素之上,并将其背景设置为黑色并将其不透明度设置为 50%-75% 来完成的。div
然后最重要的是,将另一个包含您的“模态”内容的内容居中。
我建议使用 KendoUI、ExtJS、JQueryUI 等库,但它们都会执行类似于以下快速而肮脏的演示的操作:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function () {
$('#button').click(function () {
var blackOverlay = $('<div/>')
.attr('id', 'overlay')
.css({
'position': 'absolute',
'width': '100%',
'height': '100%',
'background-color': 'black',
'top': 0,
'left': 0,
'z-index': 1001,
'opacity': .75
});
var modalContent = $('<div/>')
.css({
'z-index': 1002,
'background-color': 'white',
'display': 'inline-block',
'width': 300,
'height': 150,
'margin-left': 400,
'margin-top': 400
})
.html('This is the modal content.<br/>');
var okayButton = $('<input/>')
.attr('type', 'button')
.attr('value', 'OK')
.click(function () {
blackOverlay.remove();
});
modalContent.append(okayButton);
blackOverlay.append(modalContent);
$('body').append(blackOverlay);
});
});
</script>
</head>
<body>
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
Here is some content.<br />
<input id="button" type="button" value="Click Me" />
</body>
</html>