如何使用JS按特定顺序运行弹出窗口?

时间:2018-07-12 03:28:20

标签: javascript html css

我试图在弹出窗口中添加下一个按钮。该按钮以随机顺序逐一加载弹出窗口。但是我想按特定顺序(升序)运行弹出窗口。同样,当最后一个弹出窗口打开时,想要自动禁用下一个按钮。如何使用此代码进行操作?请帮助我解决问题。

这是我的代码。

$(document).ready(function() {
   $(".getAssignment").click(function() {
      var $divs = $(".modalDialog");
      if ($divs.length > 0) {
         window.location.href = "#" + $divs[ Math.floor(Math.random() * $divs.length) ].id;
      }
   });
});

<input class="getAssignment" type="button" value="Open Modal">

<div id="openModal" class="modalDialog">
    <div>
    <input class="getAssignment" type="button" value="Next">    
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 1</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal2" class="modalDialog">
    <div>   
   <input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 2</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

https://jsfiddle.net/Sanjeewani/y37zeour/4/

1 个答案:

答案 0 :(得分:0)

您可以在元素上使用data-属性来定义顺序。在进入下一个模态时,检查是否存在更多模态。如果不存在,请禁用下一个按钮。

$(document).ready(function() {
   var currentmodal = 1;
   $(".getAssignment").click(function() {
      var $divs = $(".modalDialog");
      var modal = $("*[data-modalorder="+(currentmodal++)+"]");
      if(!$("*[data-modalorder="+currentmodal+"]").length)
      {
          modal.find("input.getAssignment").prop("disabled",true);
      }
      if ($divs.length > 0 && modal) {
          window.location.href = "#" + $(modal).attr("id");
      }
   });
});
.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}
.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}
.modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}
.close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
}
.close:hover {
    background: #00d9ff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<input class="getAssignment" type="button" value="Open Modal">

<div id="openModal" class="modalDialog" data-modalorder=1>
    <div>
    <input class="getAssignment" type="button" value="Next">	
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 1</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal2" class="modalDialog" data-modalorder="2">
    <div>	
   <input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 2</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>

<div id="openModal3" class="modalDialog" data-modalorder="3">
    <div>	
    	<input class="getAssignment" type="button" value="Next">
   <a href="#close" title="Close" class="close">X</a>
   <h2>Modal Box 3</h2>
        <p>This is a sample modal box that can be created using the powers of CSS3.</p>
        <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
    </div>
</div>