且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javascript和CSS创建模态弹出窗口

更新时间:2022-11-23 11:44:16

这里是HTML,应该插入JS,样式应该在外部样式表中。

Here is the HTML, which should probably be inserted with JS, and the styles should be in an external stylesheet.

<div style="background: gray; width: 200px; height: 200px; position: absolute; left: 50%; top: 50%; margin-left: -100px; margin-top: -100px" id="modal">I'm a modal</div>

然后,您可以利用jQuery来显示它。

Then, you could leverage jQuery to display it.

$('a.modal').bind('click', function(event) { 
    event.preventDefault();
    $('#modal').fadeIn(800);
});

这只是一个开始,你需要从中学习,例如,脚本应该检查是(':hidden')并显示,如果没有,则 fadeOut(800)或类似。

This is only a start, you'll want to learn from this and build upon it. For example, the script should check is(':hidden') and show, and if not then fadeOut(800) or similiar.