【问题标题】:Multiple bootstrap css buttons, hide and expand多个引导 css 按钮,隐藏和展开
【发布时间】:2016-04-11 14:56:47
【问题描述】:

所以在来这里之前我已经环顾四周并进行了相当多的研究,我基本上是在尝试实现引导 css 按钮,当我单击一个 div 时,一个 div 将展开,当我单击另一个时,已经打开的 div 将折叠,新的 div将打开。我有三个按钮。我面临的问题是,当我单击第二个按钮时,div 会在已经打开的第一个 div 下方展开。

目前我还没有包含任何 Javascript(对 JS 来说是新手),并且在我见过的其他示例中也没有看到它(例如在引导网站上:http://v4-alpha.getbootstrap.com/components/collapse/)。

我的代码在这里:

<button class="btn btn-info" type="button" data-toggle="collapse" data-target="#corp">More+</button>
<button class="btn btn-info" type="button" data-toggle="collapse" data-target="#wedding">More+</button>
<button class="btn btn-info" type="button" data-toggle="collapse" data-target="#photo">More+</button>
<div class="sectioncont">
  <div id="corp" class="collapse">
    <h2>Wedding Ceremonies</h2>
    We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
    and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
    laboris nisi ut aliquip ex ea commodo consequat.
  </div>

  <div id="wedding" class="collapse">
    <h2>Wedding Ceremonies</h2>
    We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
    and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
    laboris nisi ut aliquip ex ea commodo consequat.
  </div>

</div>

谢谢,任何帮助将不胜感激。我正在使用默认的二十三个主题(使用子主题)来实现这个代码工作平台。 阿米特

【问题讨论】:

  • 你需要使用 Accordion 而不是 Collapse
  • 我编辑了答案以添加图片。

标签: html css twitter-bootstrap button expand


【解决方案1】:

如果您使用的是引导程序。您需要包含引导程序的 js,例如:

https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js

assets/dist/js/bootstrap.min.js

因为某些引导组件仅在您包含 js 文件时才起作用。

一些组件也有自己特定的js。

在collapes的bootstrap组件页面中,也有说:

Collapse Bootstrap 折叠插件允许您切换内容 你的页面有一点 JavaScript 和一些类。灵活的插件 它利用了少数类(来自所需的转换 插件)以便于切换行为。

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,如何将提到的 bootstrap .js 包含到 wordpress 中?
  • 在 Wordpress 中。我想你不需要添加 js 文件。您可以在 Wordpress 中查找 Boostrap 插件并将其添加到您的网站。试试这个wordpress.org/plugins/bootstrap-3-shortcodes
  • 尝试安装它并没有看到任何变化。它在此处给出的第一个示例中是如何工作的:v4-alpha.getbootstrap.com/components/collapse/#example
  • 您是否也尝试过使用第一个示例的 HTML?
  • 我已尝试将示例与 .collapse 和 .collapsing 一起使用。
【解决方案2】:

你必须使用Accordion 而不是Collapse

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>


<body>
<div class="container">

  <div class="panel-group" id="accordion">
<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title"><button class="btn btn-info" type="button" data-toggle="collapse" data-target="#corp">
      <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">More+</botton></a>
    </h4>
  </div>
  <div id="collapse1" class="panel-collapse collapse in">
    <div class="panel-body">   <h2>Wedding Ceremonies</h2>
We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.<img src="http://lombardidental.com/wp-content/uploads/2014/06/smiley.jpg" class="img-responsive" alt="Cinque Terre"></div>
  </div>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">
      <a data-toggle="collapse" data-parent="#accordion" href="#collapse2"><button class="btn btn-info" type="button" data-toggle="collapse" data-target="#corp">
      <a data-toggle="collapse" data-parent="#accordion" href="#collapse2">More+</botton></a>
    </h4>
  </div>
  <div id="collapse2" class="panel-collapse collapse">
    <div class="panel-body">     <h2>Wedding Ceremonies</h2>
We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.<img src="http://lombardidental.com/wp-content/uploads/2014/06/smiley.jpg" class="img-responsive" alt="Cinque Terre"></div>
  </div>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">
      <a data-toggle="collapse" data-parent="#accordion" href="#collapse3"><button class="btn btn-info" type="button" data-toggle="collapse" data-target="#corp">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse3">More+</botton></a>
    </h4>
  </div>
  <div id="collapse3" class="panel-collapse collapse">
    <div class="panel-body">New Div<img src="http://lombardidental.com/wp-content/uploads/2014/06/smiley.jpg" class="img-responsive" alt="Cinque Terre"></div>
  </div>
</div>
  </div> 
</div>

</body>

你必须使用Accordion 而不是Collapse

这是CODEPEN 的工作示例

编辑:

使用图像处理CODEPEN 示例

【讨论】:

  • 嗨,非常感谢您提供这个,但是当您按下我所看到的每个按钮时,该示例不会打开和关闭 div?我也尝试实现它,但没有发现它有效?
  • 哦,对,是的,这就是我要找的。让我试试这个,看看我是否可以在我的网站样式中实现它。目前,我有三个图像,它们下方带有“更多+”按钮。希望我可以将所有这些融入手风琴风格中。
  • 另外,如何将脚本合并到 wordpress 中?我正在使用插件 Bootstrap 3 Shortcodes 和 Easy Bootstrap Shortcode。我还需要做什么吗?
  • @user2323469 是的,它与 div 一起折叠。所以 div 中的任何东西都会相应地折叠
  • @user2323469 检查我的答案我编辑了它,这里是工作代码笔示例codepen.io/anon/pen/NNydYX
【解决方案3】:

只需对 DOM 稍作修改,使用 jQuery 即可轻松完成。

    <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#corp">More+</button>
    <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#wedding">More+</button>
    <button class="btn btn-info" type="button" data-toggle="collapse" data-target="#photo">More+</button>
    <div class="sectioncont">
        <h2>Wedding Ceremonies</h2>
      <div id="corp" class="collapse">
        We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
        and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
        laboris nisi ut aliquip ex ea commodo consequat.
      </div>

        <h2>Wedding Ceremonies</h2>
      <div id="wedding" class="collapse">
        We work closely with our clients in order to ensure their strategy and message is aligned, from business objectives and brand values, to target audience and event format. We challenge thinking, perception and message; only then do we start developing
        and conceptualizing a bespoke event strategy and creative treatment. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
        laboris nisi ut aliquip ex ea commodo consequat.
      </div>

    </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $('.collapse').hide();
});
$('button').click(function(){
  var target = $(this).attr('data-target');
  $('.collapse').each(function(a,b){
    if('#'+b.id !== target){
      $('#'+b.id).hide();
    }    
  });
  $(target).toggle();
});
   

 </script>

【讨论】:

  • 嗨,非常感谢。当我点击同一个按钮时,如何使 div 也关闭?
  • 我编辑了我的答案以关闭目标(如果它已经打开)。
  • 哇,这真是太完美了。非常感谢。让我看看如何实现它。
  • 嘿,我几乎完成了这个工作,我只是想问一下(对不起,我对此有点陌生)如何将行:
  • 最好将js分离成自己的文件,但可以添加到任何HTML文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多