<script src="./jquery.min.js"></script>
有些网站中有这样的效果,当点击一个按钮后,出现一个弹窗,然后其余的部分背景都变成黑色,如下图:

如何实现这种点击后弹窗并且全屏背景变黑的效果呢?方法如下:
首先我们要引入 jQuery 文件,这个效果是在 jQuery 的基础上实现的。

<script src="./jquery.min.js"></script>
其次,因为要实现弹窗效果,所以要加上以下 JS 代码;
<script>
$(function(){
$('#add_host').click(function(){
$('.shade, .add-model').removeClass('hide');
});
});
</script>
另外,我们要写二个 DIV 层,一个是弹出层,一个遮罩层;
<div class="shade hide"></div><div class="add-model hide">这里可以添加你想弹出的内容</div>
最后,我们要通过 CSS 来控制它们的样式;
<style>.hide{display: none;}
.shade{position: fixed;top : 0;right: 0;left: 0;bottom: 0;background: black;opacity: 0.6;z-index: 100;}
.add-model {position: fixed;height: 300px;width: 400px;top: 100px;left: 50%;z-index: 101;border: 1px solid red;background: white;margin-left: -200px;}
</style>jquery 实现点击弹框并且全屏背景变黑完整代码如下:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>jquery实现点击弹框并且全屏背景变黑</title>
<style>.hide{display: none;}.shade{position: fixed;top : 0;right: 0;left: 0;bottom: 0;background: black;opacity: 0.6;z-index: 100;}.add-model {position: fixed;height: 300px;width: 400px;top: 100px;left: 50%;z-index: 101;border: 1px solid red;background: white;margin-left: -200px;}</style></head><body><div><input id="add_host" type="button" value="添加"></div><div class="shade hide"></div><div class="add-model hide">这里可以添加你想弹出的内容</div>
<script src="./jquery.min.js"></script><script>$(function(){$('#add_host').click(function(){$('.shade, .add-model').removeClass('hide');});
});
</script></body></html>专业提供WordPress主题安装、深度汉化、加速优化等各类网站建设服务,详询在线客服!
