如何在bootstrap中创建动态网格和相应的按钮

时间:2018-03-20 11:28:19

标签: twitter-bootstrap

我想使用bootstrap网格创建一个页面,当大的时候有4列,中等时有3列,小时有2列,超小时有1列。并且顶部的按钮,单击大按钮时将分辨率更改为大,中等按钮单击时将分辨率更改为中小按钮,单击时将分辨率更改为小。怎么做。请帮忙。

<html>
  <head>
    <title> Grid</title>
    <link rel="stylesheet"  href ="bootstrap/css/bootstrap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src = "bootstrap/js/bootstrap.js"></script>
</head>
<body>
    <div class="container" style="background-color:lavender;">
        <h1 style="text-align:center;color:brown">Lorem Ipsum Grid</h1>
        <br><br>

        <div class="row">
            <div class="col-lg-3 col-md-4 col-sm-2 col-xs-1">
                <p>
               // text
              </p>
            </div>
            <div class="col-lg-3 col-md-4 col-sm-2 col-xs-1">
                <p>   // text
              </p>
           </div>
           <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
               <p>
                      // text
               </p>
           </div>
           <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
               <p>
                      // text
               </p>

            </div>
        </div>
        <br><br>
        <div  class="row">
            <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
                <p>
                    // text


                </p>

            </div>

            <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
               <p>
                   // text


               </p>
             </div>
             <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
                 <p>
                       // text
                 </p>
             </div>
             <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
                 <p>
                       // text                     </p>
             </div>
             </div>
             <br><br>
        <div class="row">
            <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
            <p>
                    // text
            </p>
        </div>
        <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
            <p>
                    // text

            </p>
        </div>
        <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
            <p>
                    // text 
            </p>
        </div>
        <div class="col-lg-3  col-md-4  col-sm-2  col-xs-1">
            <p>
                    // text
            </p>
        </div>

        </div>
    </div>

   </body>
  </html>

1 个答案:

答案 0 :(得分:0)

使用jQuery可能吗?希望这是你改变列的意思。希望它可以帮到你。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

    </head>
<body>

    <div class="container" style="background-color:lavender;">
        <h1 style="text-align:center;color:brown">Lorem Ipsum Grid</h1>
        <button type="button" id="btn-small" class="btn btn-success ">Small size</button>
        <button type="button" id="btn-medium" class="btn btn-success ">Medium size</button>
        <button type="button" id="btn-large" class="btn btn-success ">Large size</button>
        <br><br>

        <div class="row">
            <div class="col-change col-lg-3 col-md-4 col-sm-6 col-xs-12">
                <p>
               // text
              </p>
            </div>
            <div class="col-change col-lg-3 col-md-4 col-sm-6 col-xs-12">
                <p>   // text
              </p>
           </div>
           <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
               <p>
                      // text
               </p>
           </div>
           <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
               <p>
                      // text
               </p>

            </div>
        </div>
        <br><br>
        <div  class="row">
            <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
                <p>
                    // text


                </p>

            </div>

            <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
               <p>
                   // text


               </p>
             </div>
             <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
                 <p>
                       // text
                 </p>
             </div>
             <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
                 <p>
                       // text                     </p>
             </div>
             </div>
             <br><br>
        <div class="row">
            <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
            <p>
                    // text
            </p>
        </div>
        <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
            <p>
                    // text

            </p>
        </div>
        <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
            <p>
                    // text 
            </p>
        </div>
        <div class="col-change col-lg-3  col-md-4 col-sm-6 col-xs-12">
            <p>
                    // text
            </p>
        </div>

        </div>
    </div>



    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script>
        $('#btn-small').click(function(){
            console.log('click');
            $('.row .col-change').each(function(){
                $(this).attr("class", "col-change col-xs-12 col-lg-12");

            });
        });
        $('#btn-medium').click(function(){
            console.log('click');
            $('.row .col-change').each(function(){
                $(this).attr("class", "col-change col-xs-4 col-lg-4");

            });
        });
        $('#btn-large').click(function(){
            console.log('click');
            $('.row .col-change').each(function(){
                $(this).attr("class", "col-change col-xs-3 col-lg-3");

            });
        });
    </script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->

  </body>
</html>