【问题标题】:Creating a Responsive Toggle Button for a <Div>为 <Div> 创建响应式切换按钮
【发布时间】:2017-07-12 13:28:43
【问题描述】:

我正在尝试为我的网站制作一个响应式过滤器小部件。

  1. #filter-widget在屏幕尺寸大于767px时显示,在屏幕尺寸小于767px时隐藏(.col-sm-3)。

  2. Show Fileter 按钮在屏幕尺寸小于 767 像素时显示。一旦用户单击此按钮,#filter-widget 将再次显示。

我的预期结果是这样的:

大于 767px 的屏幕尺寸:

屏幕尺寸小于 767 像素:

我的代码如下:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
  <title>Eat Your Food</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
  <div class="container-fluid">
    <div class="col-xs-12 hidden-sm hidden-md hidden-lg">
      <a href="#filter-widget" class="btn btn-success" data-toggle="collapse">Show Filter v</a>
    </div>
    
    <div id="filter" class="col-sm-3">
      <div id="filter-widget" class="collapse in">
        <form class="form-horizontal">
          <h3><b>Category</b></h3>
          <div class="form-group">
            <div class="checkbox">
              <label><input type="checkbox" value="vegetable" />Vegetable</label>
            </div>
            <div class="checkbox">
              <label><input type="checkbox" value="meat" />Meat</label>
            </div>
            <div class="checkbox">
              <label><input type="checkbox" value="grains" />Grains</label>
            </div>
          </div>
        </form>
      </div>
    </div>

    <!-- Result -->
    <div class="col-sm-9">
        <h3>Filted Result Is Here.</h3>
        <p>ResultResultResultResultResult</p>
    </div>
  </div>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</body>
</html>

我想让 #filter-widget 在小屏幕中默认隐藏,所以我将 &lt;div id="filter" class="col-sm-3"&gt; 更改为 &lt;div id="filter" class="col-sm-3 hidden-xs"&gt;。但是,显示过滤器按钮无法显示 #filter-widget

当屏幕小于 767 像素时,如何修改我的代码以让 显示过滤器 隐藏/显示 #filter-widget

我的代码有问题吗?

非常感谢!

【问题讨论】:

    标签: css twitter-bootstrap grid toggle


    【解决方案1】:

    这是一个适用于您的工作示例。请从您的代码中删除 hidden-

    1、CSS

     #filter-widget{display:block}
     .show-filter-button-class{display:none}
    
     @media screen and (max-width: 767px) {
       #filter-widget{display:none}
       .show-filter-button-class{display:block}
    
     }
    

    2、JS

    <script>
    $('body).on('click','.show-filter-button-class',function{
     $('#filter-widget').show();
    }
    </script>
    

    【讨论】:

    • 感谢您的建议。但是有什么方法可以让我在不编写 JS 的情况下只使用 bootstrap 和 css 吗?
    • @EdChen,你想要一个 click function 来执行然后你使用 js 或 jQuery。据我所知,没有其他方法,我们不能将 CSS 用于点击功能。但如果你只想要 CSS,那么使用 checkbox or radio button 作为按钮,我们可以根据它是否为 checked or not 来设置 div 的样式
    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2017-10-13
    相关资源
    最近更新 更多