【问题标题】:How do I make a "div" element pop up and darken the screen?如何使“div”元素弹出并使屏幕变暗?
【发布时间】:2017-06-13 19:38:12
【问题描述】:

好吧,我浏览了几个网站,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>

感谢您抽出宝贵时间阅读本文。希望尽快解决这个问题>_

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    在我开始之前,我想提醒你不要使用内联 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。

    祝你好运,万事如意。

    【讨论】:

    • 请举例功能
    • @x-rw 你这是什么意思?
    • 我将不得不使用 CSS 等,但这对我的想法来说几乎是完美的,感谢您的快速响应。
    • @Smokezarn 随时!另外,如果有帮助,请不要忘记投票,谢谢。
    • 哦,所以我将所有内容添加到测试文档中以使 CSS 正确,结果当我将其添加到我想要的页面时,添加 BootStrap 链接会掩盖我的其余 CSS在。但我认为这是因为我手动让一堆 div 并排放置。所以我认为重置它们应该很容易
    猜你喜欢
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多