基本上,這是通過在所有當前屏幕元素上重疊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>
來源
2012-12-27 01:48:07
Dan
非常感謝您! – Till