当我使用砌体时,Bootstrap折叠面板相互重叠

时间:2017-03-02 11:05:59

标签: javascript jquery html twitter-bootstrap masonry

我有下一页:

    $(function () {
        var container = document.querySelector('#PanelContainer');
        var msnry = new Masonry(container, {
            itemSelector: '.selector-class',
            isAnimated: true,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            }   
        });
        $(".spoiler-trigger").click(function () {
            $(this).parent().next().collapse('toggle');
        });
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>

<div class="container">
<div id="PanelContainer" class="row">
    <div class="col-md-6 selector-class">
        <div class="panel panel-primary card-0">
          <div class="panel-heading">
            <h3 class="panel-title">Title</h3>
        </div>
        <div class="panel-body">
          <div class="panel panel-default">
            <div class="panel-heading">
              <button type="button" class="btn btn-default btn-xs spoiler-trigger" data-toggle="collapse">Collapse</button>
            </div>
            <div class="panel-collapse collapse out">
              <div class="panel-body">Body</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="col-md-6 selector-class">
        <div class="panel panel-primary card-0">
          <div class="panel-heading">
            <h3 class="panel-title">Title</h3>
        </div>
        <div class="panel-body">
          <div class="panel panel-default">
            <div class="panel-heading">
              <button type="button" class="btn btn-default btn-xs spoiler-trigger" data-toggle="collapse">Collapse</button>
            </div>
            <div class="panel-collapse collapse out">
              <div class="panel-body">Body</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="col-md-6 selector-class">
        <div class="panel panel-primary card-0">
          <div class="panel-heading">
            <h3 class="panel-title">Title</h3>
        </div>
        <div class="panel-body">
          <div class="panel panel-default">
            <div class="panel-heading">
              <button type="button" class="btn btn-default btn-xs spoiler-trigger" data-toggle="collapse">Collapse</button>
            </div>
            <div class="panel-collapse collapse out">
              <div class="panel-body">Body</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="col-md-6 selector-class">
        <div class="panel panel-primary card-0">
          <div class="panel-heading">
            <h3 class="panel-title">Title</h3>
        </div>
        <div class="panel-body">
          <div class="panel panel-default">
            <div class="panel-heading">
              <button type="button" class="btn btn-default btn-xs spoiler-trigger" data-toggle="collapse">Collapse</button>
            </div>
            <div class="panel-collapse collapse out">
              <div class="panel-body">Body</div>
            </div>
          </div>
        </div>
      </div>
    </div>
</div>
</div>

所有看起来都很完美但是当用户尝试展开任何与其他面板重叠的面板时。如何让砌体重新计算并平滑更新面板的位置? 附:我是JS的noobie。

ADDED
我为Bootstrap事件设置了监听器:

$(document).on('hidden.bs.collapse', function () {
    msnry.layout();
});
$(document).on('shown.bs.collapse', function () {
    msnry.layout();
});

我的布局开始更新自己,但是如果它有一个错误的扩展行为。现在,面板扩展了,之后调用了布局更新,但我需要在之前完成。

1 个答案:

答案 0 :(得分:1)

希望这可以提供帮助,我的工作正常,但只有在使用col-XX-[3/6/12]时(由于width:的要求是一个很好的%年龄)

它有点乱,但功能齐全。

值得改变/注意的事情:

为了解决崩溃/扩展时的布局更改,JS有一个setTimeout在更新上运行;在生产中,或者特别是对于较慢的动画扩展/折叠序列,我想有一个更好的解决方案需要找到。但对于不到100毫秒的动画,我认为你可以侥幸逃脱。

在CSS上,如果您使用任何大于xs的引导列,则需要调整css grid-item[-6,-12]以匹配col规则以更改宽度%屏幕尺寸。

大多数情况下,引导列规则优先,所以事情不会太破坏。

[注意:我的.click事件绑定到面板上的文本标题,而不是面板标题div]

&#13;
&#13;
$('.grid').masonry({
  itemSelector: '.grid-item',
  columnWidth: '.grid-sizer',
  percentPosition: true,

});

$('.panel-heading').click(function () {
	setTimeout(function() {
		$('.grid').masonry('layout');
	}, 300);
});
&#13;
.grid-sizer, .grid-item {width: 25%;}

.grid-item--width-6 { width: 50%;}

.grid-item--width-12 { width: 100%;}

/* NOT required to work,it 
helps demo whats going on */
.panel-body {
	height: 200px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>

<link href="css/main.css" rel="stylesheet">



<div class="grid">
  <div class="grid-sizer"></div>

  <div class=" col-xs-12 grid-item grid-item--width-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse0-m">panel 0</a>
        </h4>
      </div>
      <div id="collapse0-m" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
      </div>
    </div>
  </div>


  <div class=" col-xs-6 grid-item grid-item--width-6">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse1-m">panel 1</a>
        </h4>
      </div>
      <div id="collapse1-m" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
      </div>
    </div>
  </div>

  <div class="col-xs-6 grid-item grid-item--width-6">
    <div class="panel panel-default">
     <div class="panel-heading">
       <h4 class="panel-title">
         <a data-toggle="collapse" href="#collapse2-m">panel 2</a>
       </h4>
     </div>
     <div id="collapse2-m" class="panel-collapse collapse">
       <div class="panel-body">Panel Body</div>
     </div>
   </div>
  </div>

  <div class="col-xs-3 grid-item">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse3-m">panel 3</a>
        </h4>
      </div>
      <div id="collapse3-m" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
      </div>
    </div>
  </div>

  <div class="col-xs-3 grid-item">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse4-m">panel 4</a>
        </h4>
      </div>
      <div id="collapse4-m" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
      </div>
    </div>
  </div>

  <div class="col-xs-6 grid-item grid-item--width-6">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse5-m">panel 5</a>
        </h4>
      </div>
      <div id="collapse5-m" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
      </div>
    </div>
  </div>

</div>



    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->

    <script src="js/main.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
&#13;
&#13;
&#13;