javascript - 弹出模式不起作用

标签 javascript jquery html css

我有一个弹出窗口不起作用。当我点击按钮时,屏幕变暗,就像弹出窗口即将出现但没有弹出窗口出现。请帮忙!!!

这是html文件

      <!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="utf-8">
          <link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
          <link rel="stylesheet" type="text/css" href="css/main.css">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        </head>
        <body>
   <!-- the button to call pop up -->
          <a href="#" class='btn open-modal' data-modal="#modal1">Open Modal</a>

     <!-- pop up beginning -->
        <div class='modal' id='modal1'>
          <div class='content'>
            <h1 class='title'>This is a modal</h1>
            <p>
                Here is some text and stuff. cool cool  
            </p>
            <a class='btn close-modal' data-modal="#modal1" href="#">K Bye</a>
          </div>    
        </div>  
   <-- pop up ending -->

   <!-- the jquery script -->
        <script
          src="https://code.jquery.com/jquery-3.1.1.min.js"
          integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
          crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

        <script type="text/javascript" src="script.js"></script>
        </body>
        </html>

这是css文件

          * {
            box-sizing: border-box;
          }

          body {
            font-family: "Nunito", sans-serif;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
          }

          .btn {
            padding: 20px 50px;
            display: inline-block;
            background: #EF233C;
            color: white;
            text-decoration: none;
            transition: 0.35s ease-in-out;
            font-weight: 700;
          }
          .btn:hover {
            background: #dc1029;
          }

          .overlay {
            width: 100%;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            padding: 40px;
            position: fixed;
            top: 0;
            left: 0;
            background: rgba(0, 0, 0, 0.75);
            opacity: 0;
            pointer-events: none;
            transition: 0.35s ease-in-out;
            max-height: 100vh;
            overflow-y: auto;
          }
          .overlay.open {
            opacity: 1;
            pointer-events: inherit;
          }
          .overlay .modal {
            background: white;
            text-align: center;
            padding: 40px 80px;
            box-shadow: 0px 1px 10px rgba(255, 255, 255, 0.35);
            opacity: 0;
            pointer-events: none;
            transition: 0.35s ease-in-out;
            max-height: 100vh;
            overflow-y: auto;
          }
          .overlay .modal.open {
            opacity: 1;
            pointer-events: inherit;
          }
          .overlay .modal.open .content {
            transform: translate(0, 0px);
            opacity: 1;
          }
          .overlay .modal .content {
            transform: translate(0, -10px);
            opacity: 0;
            transition: 0.35s ease-in-out;
          }
          .overlay .modal .title {
            margin-top: 0;
          }

javascript文件

        $(".modal").each( function(){
            $(this).wrap('<div class="overlay"></div>')
        });

        $(".open-modal").on('click', function(e){
            e.preventDefault();
            e.stopImmediatePropagation;

            var $this = $(this),
                    modal = $($this).data("modal");

            $(modal).parents(".overlay").addClass("open");
            setTimeout( function(){
                $(modal).addClass("open");
            }, 350);

            $(document).on('click', function(e){
                var target = $(e.target);

                if ($(target).hasClass("overlay")){
                    $(target).find(".modal").each( function(){
                        $(this).removeClass("open");
                    });
                    setTimeout( function(){
                        $(target).removeClass("open");
                    }, 350);
                }

            });

        });

        $(".close-modal").on('click', function(e){
            e.preventDefault();
            e.stopImmediatePropagation;

            var $this = $(this),
                    modal = $($this).data("modal");

            $(modal).removeClass("open");
            setTimeout( function(){ 
                $(modal).parents(".overlay").removeClass("open");
            }, 350);

        }); 

最佳答案

您必须将 display: block 应用于 .overlay .modal.open

这是因为即使您的 .open 类附加到您的 .modaldisplay: none 来自 .modal 需要被 display: block 覆盖才能使特定的 .modal 可见。

.overlay .modal.open {
  opacity: 1;
  pointer-events: inherit;
  display: block;
  width: 60%;
  height: 60%;
  margin: auto;
}

引用代码:

$(".modal").each(function() {
  $(this).wrap('<div class="overlay"></div>')
});

$(".open-modal").on('click', function(e) {
  e.preventDefault();
  e.stopImmediatePropagation;

  var $this = $(this),
    modal = $($this).data("modal");

  $(modal).parents(".overlay").addClass("open");
  setTimeout(function() {
    $(modal).addClass("open");
  }, 350);

  $(document).on('click', function(e) {
    var target = $(e.target);

    if ($(target).hasClass("overlay")) {
      $(target).find(".modal").each(function() {
        $(this).removeClass("open");
      });
      setTimeout(function() {
        $(target).removeClass("open");
      }, 350);
    }

  });

});

$(".close-modal").on('click', function(e) {
  e.preventDefault();
  e.stopImmediatePropagation;

  var $this = $(this),
    modal = $($this).data("modal");

  $(modal).removeClass("open");
  setTimeout(function() {
    $(modal).parents(".overlay").removeClass("open");
  }, 350);

});
* {
  box-sizing: border-box;
}

body {
  font-family: "Nunito", sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btn {
  padding: 20px 50px;
  display: inline-block;
  background: #EF233C;
  color: white;
  text-decoration: none;
  transition: 0.35s ease-in-out;
  font-weight: 700;
}

.btn:hover {
  background: #dc1029;
}

.overlay {
  width: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  padding: 40px;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.75);
  opacity: 0;
  pointer-events: none;
  transition: 0.35s ease-in-out;
  max-height: 100vh;
  overflow-y: auto;
}

.overlay.open {
  opacity: 1;
  pointer-events: inherit;
}

.overlay .modal {
  background: white;
  text-align: center;
  padding: 40px 80px;
  box-shadow: 0px 1px 10px rgba(255, 255, 255, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: 0.35s ease-in-out;
  max-height: 100vh;
  overflow-y: auto;
}

.overlay .modal.open {
  opacity: 1;
  pointer-events: inherit;
  display: block;
  width: 60%;
  height: 60%;
  margin: auto;
}

.overlay .modal.open .content {
  transform: translate(0, 0px);
  opacity: 1;
}

.overlay .modal .content {
  transform: translate(0, -10px);
  opacity: 0;
  transition: 0.35s ease-in-out;
}

.overlay .modal .title {
  margin-top: 0;
}
<link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<body>
  <!-- the button to call pop up -->
  <a href="#" class='btn open-modal' data-modal="#modal1">Open Modal</a>

  <!-- pop up beginning -->
  <div class='modal' id='modal1'>
    <div class='content'>
      <h1 class='title'>This is a modal</h1>
      <p>
        Here is some text and stuff. cool cool
      </p>
      <a class='btn close-modal' data-modal="#modal1" href="#">K Bye</a>
    </div>
  </div>
  <-- pop up ending -->

    <!-- the jquery script -->
    <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <script type="text/javascript" src="script.js"></script>
</body>

关于javascript - 弹出模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046763/

相关文章:

javascript - React-native:将场景绑定(bind)到 NavigationBar

javascript - 外部化各种 React 组件中的通用函数

jquery - Highcharts:尝试更新系列

javascript - jQuery 交换元素内容给出类型 F 错误

javascript - 查找具有相同类且具有空值的下一个元素

javascript - 添加函数就是禁用js文件中的其他函数

javascript - 从 http.get() 响应中获取 url

javascript - 在 AngularJS 的另一个 Controller 中更新变量

javascript - ExtJS--在展开/折叠时调整面板高度

javascript - 当用户单击后退按钮返回上一个网页时,如何再次调用ajax?