如何关闭空间:如何限制在用户空白空间中关闭模型

当我试图点击提交按钮时,当用户点击空空间时,一个模态将在该模型中打开它将关闭。我需要限制模态关闭,唯一的用户可以使用 X 标记关闭。我需要添加一个时间间隔,以便在五分钟后自动关闭模态。

我使用核心 JavaScript 请帮助我任何人都知道

        // Get the modal
        var modal = document.getElementById('myModal');
    
        // Get the on that opens the modal
        var btn = document.getElementById("btn_sum");
    
        // Get the <span> element that closes the modal
        var span = document.getElementsByClName("close")[0];
    
          
        // When the user clicks the on, open the modal 
        btn.onclick = function() {
            modal.style.display = "block";
        }
    
    
        // When the user clicks on <span> (x), close the modal
        span.onclick = function() {
            modal.style.display = "none";
        }
    
        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
        /* Modal Style */
        
        /* Modal (background) */
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 20px; /* Location of the box */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: scroll; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }
    
        /* Modal Content */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 60%;
        
top:10px;
        
height:auto;
        }
    
        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
    
        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
    
    
        }
    
<div cl="submit_btn" style="padding:10px;"><on cl="on" id="btn_sum"><span>Submit </span></on></div>
  <div cl="modal" id="myModal"><!-- Modal content -->
   <div cl="modal-content"><span cl="close">&times;</span>
   <h5>Thanks For Submitting Your Details, You Get a cupon Code Shortly</h5>
</div>
        </div>
1

这个演示工作在 5 秒,如果用户不交互该模块将关闭,使其在 5 分钟后工作,你应该替换数字 5000 到 300,000,其中(1000 * 60 * 5 = 300,000):

var modal = document.getElementById("myModal");
// Get the on that opens the modal
var btn = document.getElementById("btn_sum");
// Get the <span> element that closes the modal
var span = document.getElementsByClName("close")[0];
// When the user clicks the on, open the modal
btn.addEventListener("click", function(event) {
  modal.style.display = "block";
  setTimeout(event => {
    modal.style.display = "none";
//      close after 5 secounds for demo
//      where every 1000 = 1 secound
  }, 5000);
});
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
};
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 20px; /* Location of the box */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: scroll; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }
    
        /* Modal Content */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 60%;
        
top:10px;
        
height:auto;
        }
    
        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
    
        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
    
    
        
<div cl="submit_btn" style="padding:10px;"><on cl="on" id="btn_sum"><span>Submit </span></on></div>
<div cl="modal" id="myModal">
  <!-- Modal content -->
  <div cl="modal-content"><span cl="close">&times;</span>
    <h5>Thanks For Submitting Your Details, You Get a cupon Code Shortly</h5>
  </div>
</div>
1

您有window.onclick事件,当您在模态外部单击时关闭模态,您需要删除它。要在特定时间后关闭模态,您可以使用 javascript 的setTimeout。我还建议在加载文档后运行 JS

$(document).ready(function() {
  // Get the modal
  var modal = document.getElementById("myModal");
  // Get the on that opens the modal
  var btn = document.getElementById("btn_sum");
  // Get the <span> element that closes the modal
  var span = document.getElementsByClName("close")[0];
  // When the user clicks the on, open the modal
  btn.onclick = function() {
    modal.style.display = "block";
      // you wanna set 3000 to 300000 to close after 5 min
  setTimeout(() => {
    modal.style.display = "none";
  }, 3000);
  };
  // When the user clicks on <span> (x), close the modal
  span.onclick = function() {
    modal.style.display = "none";
  };
});
/* Modal Style */
/* Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 20px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: scroll; /* Enable scroll if needed */
  background-color: rgb(0, 0, 0); /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 60%;
  top: 10px;
  height: auto;
}
/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
  <head> 
    <!-- jquery -->
    <script src="https://ajax.googlea.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>    
  </head>
  <body>
    <div cl="submit_btn" style="padding:10px;">
      <on cl="on" id="btn_sum">
        <span>Submit </span>
      </on>
    </div>
    <div cl="modal" id="myModal">
      <!-- Modal content -->
      <div cl="modal-content">
        <span cl="close">&times;</span>
        <h5>
          Thanks For Submitting Your Details, You Get a cupon Code Shortly
        </h5>
      </div>
    </div>
  </body>
</html>

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(505)
C13多少算严重:在 C#中生成13位唯一随机数(random 13)
上一篇
C++输入多组数据:在 C++程序中访问AppleMagicTrackpad输入数据
下一篇

相关推荐

  • docker游戏服务器:如何使用Docker搭建高性能的游戏服务器

    Docker游戏服务器是一种将游戏服务器部署到容器中的方式,它可以帮助游戏开发者快速、轻松地部署游戏服务器,并且可以更轻松地扩展游戏服务器的容量。…

    2023-04-27 09:55:33
    0 98 32
  • win7玩cf卡顿怎么解决:解决Win7环境下CF游戏卡顿问题

    尝试更新系统:可能是由于系统缺少某些补丁或者更新导致CF卡顿,可以尝试在Windows Update中进行检查更新,并安装最新的补丁和更新。更新显卡驱动:可能是由于显卡驱动过旧或者不兼容导致CF卡顿,可以尝试更新显卡驱动,可以到显卡厂商官网下载最新的驱动进行安装。…

    2023-05-27 11:45:17
    0 96 44
  • cv糖醋排骨是弯的吗弯曲的美味

    cv糖醋排骨不是弯的,它是一种制作方法,通常用来制作排骨。代码:…

    2023-04-01 13:03:36
    0 42 82
  • android 视频编码深入理解MediaCodec API

    Android 视频编码是指将原始视频数据经过压缩编码后,生成新的视频数据,以便减少视频文件的体积,提高传输速度,以及更好地在 Android 设备上播放。…

    2023-01-13 10:58:18
    0 69 35
  • cookie如何使用:如何使用Cookie来改善用户体验

    Cookie是一种存储在客户端的小型文件,用于记录用户的信息,如访问时间、登录状态等。使用Cookie可以更好地为用户提供服务,比如保存用户的登录状态,记录用户的浏览历史记录等。…

    2023-05-07 02:18:11
    0 82 13
  • cv小敢:如何利用CV小敢提升职业技能?

    cv小敢(Computer Vision Tiny-YOLO)是一种轻量级的物体检测算法,它可以在资源受限的设备上运行,如嵌入式设备、智能手机等。它是基于YOLO(You Only Look Once)算法的一个变体,由Joseph Redmon和Ali Farhadi开发,旨在提高深度学习模型的性能,同时减少模型的大小和计算复杂度。…

    2023-02-09 13:08:59
    0 96 97
  • coremail论客邮箱Coremail论客邮箱

    Coremail论客邮箱是一款专业的企业邮箱服务,可以满足企业对安全、可靠性和高效性的要求。它拥有强大的安全性能,可以提供多种安全保护,包括防止邮件被窃取、拦截恶意邮件、防止跨站脚本攻击等。此外,它还支持多种企业级功能,如组织架构管理、收发邮件管理、文件共享管理、联系人管理等,可以帮助企业提高工作效率,提升企业形象。…

    2023-02-25 04:36:55
    0 47 36
  • linux启动tomcat权限不够:解决Linux下Tomcat启动权限不够的问题

    Linux启动Tomcat权限不够的原因可能是Tomcat安装目录的权限设置不正确,或者Tomcat用户没有足够的权限。为了解决这个问题,首先需要检查Tomcat安装目录的权限设置,确保Tomcat用户有足够的权限访问该目录。…

    2023-05-18 10:42:29
    0 13 62

发表评论

登录 后才能评论

评论列表(17条)