gpt4 book ai didi

javascript - 如何使 "div"元素弹出并使屏幕变暗?

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:50 24 4
gpt4 key购买 nike

好吧,我一直在寻找一些网站和 this有点接近我想要的。

我想要一个按钮来显示一个 div,(我可以通过我的剧透代码做到这一点),然后我想要同一个按钮使屏幕变暗,就像上面的链接一样,现在我 - 不能 - 做。是使它成为一个切换按钮。我可能缺少一些基本的东西,但我不知道。

下面我有一个带有按钮的 html,它会显示一个带有“hello”的 div,我想要它,所以当“hello”弹出时它会使屏幕变暗,但是按下再次点击按钮(或)点击 div,屏幕将再次恢复正常。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button onclick="if(document.getElementById('spoiler-') .style.display=='none') {document.getElementById('spoiler-') .style.display=''}else{document.getElementById('spoiler-') .style.display='none'}">Spoiler</button>
<div id="spoiler-" style="display:none">
<p style="border: 2px solid gray;">Hello!</p>
</div>
</body>
</html>

感谢您花时间阅读本文。希望尽快解决这个问题>_<

最佳答案

在开始之前,我想提醒您不要使用内联 JS 或 CSS,这是不好的做法,不太容易阅读,您应该始终将 HTML、CSS 和 JS 分开。

现在回答您的问题,与其重新发明轮子,不如看看这个名为 BootStrap 的非常简洁的框架。 .

在头文件中添加这些链接和脚本:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" />
<!-- We need jQuery to make bootstrap.min.js work -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

这是一个 BootStrap 模式的工作示例:

$("#myModal").on("show.bs.modal", function(event) {
console.log("Modal has been shown, do something amazing now!");
});
.modal-backdrop {
background-color: #333;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" />
<!-- We need jQuery to make bootstrap.min.js work -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<button class="btn btn-primary btn-lg" data-target="#myModal" data-toggle="modal" id="modalOne">Button</button>
<!-- Modal -->
<div aria-hidden="true" aria-labelledby="myModalLabel" class="modal fade" id="myModal" role="dialog" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal" type="button">
<span aria-hidden="true">&times;</span> <span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
lorem lorem lorem lorem lorem lorem lorem lorem lorem lorem lorem.
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" type="button">Close</button>
<button class="btn btn-primary" type="button">OK</button>
</div>
</div>
</div>
</div>

您还可以通过更改 CSS 中 .modal-backdrop 类下的 background-color 属性来更改背景颜色。

如果您需要有关 BootStrap 的额外帮助,你可以头here开始使用 BootStrap。

祝你好运,万事如意。

关于javascript - 如何使 "div"元素弹出并使屏幕变暗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com