我有一个容器框和一个灰色框,该框从右侧隐藏。我也有2个按钮。单击其中之一时,将出现灰色框并推动容器框。再次单击此按钮时,灰色框将通过使用toggleClass移出舞台,并且容器框将向后扩展。看起来像停靠和取消停靠。

这里有一个逻辑:当我单击按钮1时,将出现一个灰色框,其中显示“按钮1”文本。虽然灰色框仍在舞台上,但是如果我单击按钮2,则“按钮2”文本将显示在灰色框上,并且仍保留在舞台上。

问题:当我单击按钮2(如上所述)时,应该用灰色框推动容器,但是当灰色框仍在舞台上时容器会膨胀。问题出在此JS $('.container').toggleClass('col2 col1');



var $grayBox = $('.gray-box');

$('.clickMe').on('click', function() {

  // get text of clicked button and box.
  var myText = $(this).text();
  var boxText = $grayBox.text();

  // "true" if text differs OR box is hidden. otherwise "false".
  var state = myText != boxText || $grayBox.not('.dock');

  // update the box text and state.
  $grayBox.text(myText).toggleClass('dock', state);
  $('.container').toggleClass('col2 col1');
})

.gray-box {
  position: fixed;
  margin-right: -120px;
  top: 10px;
  right: 10px;
  width: 100px;
  height: 100px;
  background-color: gray;
  -webkit-transition: all 0.25s ease-in-out 0s;
  -moz-transition: all 0.25s ease-in-out 0s;
  -o-transition: all 0.25s ease-in-out 0s;
  transition: all 0.25s ease-in-out 0s;
}

.dock {
  margin-right: 0;
}

.container {
  border: 1px solid red;
  height: 400px;
  -webkit-transition: all 0.25s ease-in-out 0s;
  -moz-transition: all 0.25s ease-in-out 0s;
  -o-transition: all 0.25s ease-in-out 0s;
  transition: all 0.25s ease-in-out 0s;
}

.col1 {
  width: 100%;
}

.col2 {
  width: calc(100% - 120px);
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container col1">
  <p>
  </p>
  <button class='clickMe'>Button 1

     </button>
  <p></p>
  <button class='clickMe'>Button 2</button>
</div>

<div class="gray-box">
  My box
</div>





请看我在jsfiddle上的样本

最佳答案

由于$grayBox上的类操作依赖于state,因此它并不总是直接的“切换”。例如,单击一个按钮可能会打开该类,但是单击另一个按钮并不会总是将其关闭。因此,col1 col2的切换可能与$graybox的状态不同步。

我建议使用state变量来更改.container的宽度。另外,我建议不要切换两个类(col1col2),而只切换一个覆盖默认样式的类。

在下面,我将.container的默认宽度设置为100%
切换col2类会将宽度更改为calc(100% - 120px)



var $grayBox = $('.gray-box');
var $container = $('.container');

$('.clickMe').on('click', function() {

  // get text of clicked button and box.
  var myText = $(this).text();
  var boxText = $grayBox.text();

  // "true" if text differs OR box is hidden. otherwise "false".
  var state = myText != boxText || $grayBox.not('.dock');

  // update the box text and state.
  $grayBox.text(myText).toggleClass('dock', state);
  $container.toggleClass('col2', state);

});

.gray-box {
  position: fixed;
  margin-right: -120px;
  top: 10px;
  right: 10px;
  width: 100px;
  height: 100px;
  background-color: gray;
  -webkit-transition: all 0.25s ease-in-out 0s;
  -moz-transition: all 0.25s ease-in-out 0s;
  -o-transition: all 0.25s ease-in-out 0s;
  transition: all 0.25s ease-in-out 0s;
}

.dock {
  margin-right: 0;
}

.container {
  border: 1px solid red;
  height: 400px;
  -webkit-transition: all 0.25s ease-in-out 0s;
  -moz-transition: all 0.25s ease-in-out 0s;
  -o-transition: all 0.25s ease-in-out 0s;
  transition: all 0.25s ease-in-out 0s;
  width: 100%;
}

.col2 {
  width: calc(100% - 120px);
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <p><button class='clickMe'>Button 1</button></p>
  <p><button class='clickMe'>Button 2</button></p>
</div>

<div class="gray-box">My box</div>







编辑

这是使用flexbox layout的实验:



var $sideNav = $('#sideNav');

$('.navToggle').on('click', function() {

  // get text of clicked button and box.
  var btnText = $(this).text();
  var navText = $sideNav.text();

  // "true" if text differs OR box is hidden. otherwise "false".
  var state = btnText != navText || $sideNav.not('.open');

  // update the box text and state.
  $sideNav.text(btnText).toggleClass('open', state);

});

#container {
  display: flex;
}

#buttons {
  border: 1px solid red;
  flex: 1 1 0;
}

#sideNav {
  height: 100px;
  background-color: gray;
  transition: all 0.25s ease-in-out 0s;
  flex: 0 0 0;
  overflow: hidden;
}

#sideNav.open {
  flex-basis: 100px;
  margin-left: 20px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="container">
  <div id="buttons">
    <p><button class='navToggle'>Button 1</button></p>
    <p><button class='navToggle'>Button 2</button></p>
  </div>
  <div id="sideNav">My box</div>
</div>

关于jquery - 两个div停靠和停靠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56192855/

10-13 01:42