1

此网格的移动和平板电脑布局如下:

[标题]

[图片]

[内容]

但是,在中/大屏幕中,我想将其更改为这种布局

在此处输入图像描述

下面的代码片段首先使用移动设备创建,以便可以在大屏幕中重新排序。但内容列不在中/大屏幕的标题下方。内容列应填充剩余的高度。

如何使它达到所需的布局?如果通过引导程序无法实现,我可以使用 flexbox 吗?

.tall {
  height: 150px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.2.0/sandstone/bootstrap.min.css" rel="stylesheet"/>
<div class="container text-center">
  <div class="row"> 
    <div class="col-sm-12">
      <h4>Reorder columns in larger display</h4>
      <div class="row"> 
        <div class="col-xs-12 col-sm-12 col-md-6 col-md-push-6">
          <div class="well">Header</div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6 col-md-pull-6">
          <div class="well tall">Image</div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6 col-md-push-6">
          <div class="well">Content</div>
        </div>
      </div>    
    </div>
  </div>
</div>

4

2 回答 2

1

You cannot do this directly in bootstrap.You need to move content div next to the header div based on the size of the screen. This cannot be done as css cannot move the div around html.

1.One solution is to use jquery to move the div on change in size.See the code sample below.But this might cause performance issues because it will check the criteria whenever the screen is resized.

<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.2.0/sandstone/bootstrap.min.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" type="application/javascript"></script>
<script type="application/javascript">
    function moveDiv(){
        if($(window).width() > 990 ){ // screen size 
            $("#content").insertAfter("#header"); //insert after header div
        }else{
            $("#content").appendTo("#contentbox"); // append it as child to contentbox
        }
    }
    $(document).ready(function () {
        moveDiv();// default check whenever page loads
        $(window).resize(function(){
         moveDiv(); // on resize check
        });
    });
</script>
<div class="container text-center">
<div class="row">
    <div class="col-sm-12">
        <h4>Reorder columns in larger display</h4>
        <div class="row">
            <div id="headerbox" class="col-xs-12 col-sm-12 col-md-6 col-md-push-6">
                <div id="header" class="well">Header</div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-6 col-md-pull-6">
                <div class="well tall">Image</div>
            </div>

            <div id="contentbox" class="col-xs-12 col-sm-12 col-md-6 col-md-push-6">
                <div id="content" class="well">Content</div>
            </div>

        </div>
    </div>
</div>

  1. You can use visible , invisible criteria on div but you should introduce same div twice ,if you need to maintain the structure.This is a quick hack, but the div should be repeated to maintain the structure.(EDIT: Refer post by lamelemon)

  2. This is something new to consider. Try using grid layouts in css which was recently introduced. Whatever you have explained can be done with few lines of code only using css. https://css-tricks.com/snippets/css/complete-guide-grid/ and check out this youtube video https://www.youtube.com/watch?v=3vJE3bVoF-Q for demo of grid

If you are not using using bootstrap you can do this with regular css like I explained in code snippet.But you have to handle compatibility with older browser.

.tall {
  height: 150px;
}

.divborder {
  border: hidden;
  margin: 1em;
  background-color: grey;
}

@supports (display: grid) /*If browser supports grid*/  {

 /*default div is displayed one by one*/
 
 /*for bigger screen sizes*/
  @media all and (min-width: 600px) {
    .box { /* grid container*/
      display: grid;
      grid-template-columns: 1fr 1fr; /*Specify number of columns 1fr is one fraction of screen*/
      grid-template-rows: 1fr 1fr; /*Specify number of rows*/
    }
    .header {
      grid-row: 1/2; /*start from 1 and end at 2 */
      grid-column: 2/-1; /*start from 2 and end of box (left to right edge)*/
    }
    .image {
      grid-row: 1/-1; /*top to bottom of row*/
      grid-column: 1/2
    }
    .content {
      grid-row: 2/-1;
      grid-column: 2/-1;
    }
  }
}
/*Code for backward compatability*/
<h4>Reorder columns in larger display</h4>
<div class="box">
  <div id="header" class="header divborder">Header</div>
  <div class="image tall divborder">Image</div>
  <div id="content" class="content well divborder">Content</div>
</div>

于 2017-08-01T21:51:28.170 回答
0

我不认为你可以在没有空格的情况下使用 bootstrap 来实现这一点,除非你的内容填充它,但是你可以做的,因为它只是一个图像,在两个位置加载图像并根据屏幕大小隐藏和显示.visible-*.hidden-*

.tall {height: 150px;}
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.2.0/sandstone/bootstrap.min.css" rel="stylesheet"/>
<div class="container text-center">
  <div class="row"> 
    <div class="col-sm-12">
      <h4>Reorder columns in larger display</h4>
      <div class="row"> 
         <div class="col-xs-12 col-sm-12 col-md-6 hidden-sm hidden-xs">
          <div class="well tall">Image</div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6">
          <div class="well">Header</div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6 visible-sm visible-xs">
          <div class="well tall">Image</div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-6">
          <div class="well">Content</div>
        </div>
      </div>    
    </div>
  </div>
</div>

于 2017-08-01T19:38:42.213 回答