简体   繁体   English

单击时如何使全宽div出现在网格项下

[英]How to make full width div appear under a grid item on click

I'm trying to get a full width content box to appear underneath a grid item on click... I have it appearing when the grid is 4 wide, but because of the absolute positioning, it covers up items below it. 我试图使一个完整宽度的内容框出现在单击时的网格项下方...当网格为4宽时,它会出现,但是由于绝对位置的原因,它掩盖了其下方的项。

The behavior that I'm after is that it would move the other grid items below it down when a full width content box appears. 我要执行的操作是,当出现全角内容框时,它将向下移动它下面的其他网格项。 I can't have it covering up any of the other grid items. 我无法覆盖其他任何网格项。 Note that the height of the full-width content box would change based on the content length within it. 请注意,全角内容框的高度将根据其中的内容长度而变化。

.track-box, .content {
  padding-top: 50px;
  padding-bottom: 50px;
  margin: 5px;
}

.track-content {
  position: absolute;
  left: 0;
  max-width: 100%;
  padding: 20px;
  margin: 10px;
}

https://codepen.io/Finches/pen/XVgxEb https://codepen.io/Finches/pen/XVgxEb

Any help on how to approach this? 对如何解决这个问题有帮助吗? Thanks. 谢谢。

One solution would be for you to toggle a class on the track-box-container div when the box is clicked. 一种解决方案是让您在单击框时在track-box-container div上切换类。 Then you could just set the height manually when that class is added or taken away. 然后,您可以在添加或删除该类时手动设置高度。

See my codepen 看我的codepen

This is the easiest way to do it without changing a lot of your code. 这是最简单的方法,无需更改大量代码。 The only issue is the height will need to be static instead of flexible. 唯一的问题是高度将需要是静态的而不是柔性的。

Hope this helps. 希望这可以帮助。

I made a solution that uses the dynamic height of the content boxes... this way the items in the grid will move down the proper amount no matter how small or large the content is in the content box and the content will fit in the content box properly. 我提出了一个使用内容框动态高度的解决方案...这样,无论内容框中的内容有多小,网格中的项目都将向下移动适当的数量,并且内容将适合内容正确包装。 This would need refined a bit, but it seems to work fairly well. 这需要进行一些改进,但看起来效果很好。

$('.track-box-container').click(function() {

  //Get height of content
  var height = $(this).find('.track-content').height() + 250;

  //Convert height to string
  var heightStr = height.toString();

  //Toggle height and content box display
  if ($(this).height() == 200) {
      $(this).animate({height: heightStr});
      $(this).find('.track-content').show();
    }
    else if ($(this).height() == heightStr) {
      $(this).find('.track-content').hide();
      $(this).animate({height: "200px"});
    }

});

https://codepen.io/Finches/pen/YYQdxz https://codepen.io/Finches/pen/YYQdxz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM