独立div js

时间:2019-01-28 15:37:40

标签: javascript jquery html css

请告诉我,当打开一个积木时,第二个落下。如何使这两个块彼此独立? 也就是说,您需要这样做,以便在打开第一个div时,第二个将保留在原位。现在,当打开一个div时,第二个移位。 如何解决?

$(document).ready(function() {
  $('#accordeon .acc-head').on('click', f_acc);
});

function f_acc() {
  //$('#accordeon .ac-body').slideUp(1000);
  $('#accordeon .acc-body').not($(this).next()).slideUp(1000);
  $(this).next().slideToggle(500);
}
#accordeon {
  border: 10px solid #fff;
  width: 30%;
  background: #fff;
  border-radius: 5px;
  display: inline-block;
  margin-right: 20px;
  margin-bottom: 20px;
}

.acc-head {
  cursor: pointer;
}

.acc-body {
  margin-bottom: 5px;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<div id='accordeon'>
  <div class='acc-head'>
    <center><span style='font-size: 20px;'>Стол №".$row['stol']."</span></center>
  </div>
  <div class='acc-body'>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;'>
    <div style='width:100%;height:225px;overflow-y: scroll;margin-bottom:5px;'>
      <div style='width:94%; height:65px;margin-left:10px;' id='ck-button'>
        <label><input type='checkbox' value='1'>
	<span>".$row2['name']." (Модификатор)<br>
		Кол-во: ".$row2['amount']."<br>
		Статус: ".$row2['status']."</span><br>
	</label>
      </div>"; } echo "</div>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;margin-bottom:px;'>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<div id='accordeon'>
  <div class='acc-head'>
    <center><span style='font-size: 20px;'>Стол №".$row['stol']."</span></center>
  </div>
  <div class='acc-body'>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;'>
    <div style='width:100%;height:225px;overflow-y: scroll;margin-bottom:5px;'>
      <div style='width:94%; height:65px;margin-left:10px;' id='ck-button'>
        <label><input type='checkbox' value='1'>
	<span>".$row2['name']." (Модификатор)<br>
		Кол-во: ".$row2['amount']."<br>
		Статус: ".$row2['status']."</span><br>
	</label>
      </div>"; } echo "</div>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;margin-bottom:px;'>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

vertical-align:top添加到#accordeon

$(document).ready(function() {
  $('#accordeon .acc-head').on('click', f_acc);
});

function f_acc() {
  //$('#accordeon .ac-body').slideUp(1000);
  $('#accordeon .acc-body').not($(this).next()).slideUp(1000);
  $(this).next().slideToggle(500);
}
#accordeon {
  border: 10px solid #fff;
  width: 30%;
  background: #fff;
  border-radius: 5px;
  display: inline-block;
  margin-right: 20px;
  margin-bottom: 20px;
  
  vertical-align:top
}

.acc-head {
  cursor: pointer;
}

.acc-body {
  margin-bottom: 5px;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<div id='accordeon'>
  <div class='acc-head'>
    <center><span style='font-size: 20px;'>Стол №".$row['stol']."</span></center>
  </div>
  <div class='acc-body'>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;'>
    <div style='width:100%;height:225px;overflow-y: scroll;margin-bottom:5px;'>
      <div style='width:94%; height:65px;margin-left:10px;' id='ck-button'>
        <label><input type='checkbox' value='1'>
	<span>".$row2['name']." (Модификатор)<br>
		Кол-во: ".$row2['amount']."<br>
		Статус: ".$row2['status']."</span><br>
	</label>
      </div>"; } echo "</div>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;margin-bottom:px;'>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<div id='accordeon'>
  <div class='acc-head'>
    <center><span style='font-size: 20px;'>Стол №".$row['stol']."</span></center>
  </div>
  <div class='acc-body'>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;'>
    <div style='width:100%;height:225px;overflow-y: scroll;margin-bottom:5px;'>
      <div style='width:94%; height:65px;margin-left:10px;' id='ck-button'>
        <label><input type='checkbox' value='1'>
	<span>".$row2['name']." (Модификатор)<br>
		Кол-во: ".$row2['amount']."<br>
		Статус: ".$row2['status']."</span><br>
	</label>
      </div>"; } echo "</div>
    <hr style='background-color: #e0e0e0; height: 1px;  border: 0px;margin-bottom:px;'>
  </div>
</div>