如何在弹出窗口中添加下一个上一个按钮?

时间:2018-07-12 07:29:22

标签: javascript html css

我试图在弹出框中添加下一个按钮。现在,它以特定顺序运行弹出窗口。同样,当最后一个弹出窗口打开时,该按钮将自动禁用。但是有一个问题当我删除一些模态时,代码不起作用。我想按此顺序运行弹出窗口,当我删除某些弹出窗口时,这些弹出窗口要正确运行。另外,我想创建上一个按钮。我该怎么做?请帮助我解决问题。

这是我使用的代码。

$(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");
      }
   });
});

$(document).ready(function() {
       var currentmodal = 1;
       $(".getAssignment2").click(function() {
          var $divs = $(".modalDialog");
          var modal = $("*[data-modalorder="+(currentmodal--)+"]");
          if(!$("*[data-modalorder="+currentmodal+"]").length)
          {
              modal.find("input.getAssignment2").prop("disabled",true);
          }
          if ($divs.length > 0 && modal) {
              window.location.href = "#" + $(modal).attr("id");
          }
       });
    });

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

<div id="openModal" class="modalDialog" data-modalorder=1>
    <div>
    <input class="getAssignment2" type="button" value="Previous">
    <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="getAssignment2" type="button" value="Previous">
   <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/q1tm8ck2/38/

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您想要的。但是我尝试重写一下您的js。

$(document).ready(function() {
	var data=[];
  currentModal = 0;
  
  $('.modalDialog').each(function(){
    data.push({
      id: $(this).attr('id'),
      order: $(this).data('modalorder')
    });
  })
    
	$('#openModalBtn').click(function(){
  	currentModal = 0;
    window.location.href = "#" + data[currentModal].id;
    $('#'+data[currentModal].id).find('.getAssignment2').prop('disabled', true);
  })
  
  //prev
  $('.getAssignment2').click(function(){
  	if (currentModal>0) {
    	currentModal--;
      window.location.href = "#" + data[currentModal].id;
    } else {
      
    	window.location.href = '#'
    }
  })
  
  //next
  $('.getAssignment').click(function(){
  	if (currentModal<data.length - 1) {
    	currentModal++;
      if (currentModal===data.length - 1) $('#'+data[currentModal].id).find('.getAssignment').prop('disabled', true);
      window.location.href = "#" + data[currentModal].id;
    } else {
    	window.location.href = '#'
    }
  })
  
})
.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;
}
.getAssignment{
  cursor:pointer;
}
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <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>


<body>

<input type="button" id="openModalBtn" value="Open Modal">

<div id="openModal1" class="modalDialog" data-modalorder=1>
    <div>
    <input class="getAssignment2" type="button" value="Previous">
    <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="getAssignment2" type="button" value="Previous">
   <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="getAssignment2" type="button" value="Previous">
    	<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>

</body>