【问题标题】:jQuery toggle expand divs inside bootstrap container wrapjQuery切换在引导容器包装内扩展div
【发布时间】:2015-04-06 10:33:23
【问题描述】:

Guys 基本上会有一个引导容器div,里面会有4张卡片样式divs,带有切换展开/缩回按钮。当您单击一张卡片的按钮时,div 将一直扩展到容器的宽度。请检查图像。我是 jQuery 的新手,使用我的脚本,我无法实现全宽度。如果有人可以帮助我,那就太好了。这是下面的代码 -

$(document).ready(function() {
  $('#toggle-button').click(function() {
    var toggleWidth = $("#exp_card_1").width() == 600 ? "200px" : "600px";
    $('#exp_card_1').animate({
      width: toggleWidth
    });
  });
});
#exp_card_1 {
  position: relative;
  height: 310px;
  background: #2d3644;
  z-index: 1;
}
#toggle-button {
  position: absolute;
  right: 0;
  top: 43.7%;
  height: 38px;
  width: 38px;
  background: #131f34 url("../images/arrow.png") no-repeat;
  background-position: 5px 9px;
  border-radius: 3px 0 0 3px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="container" style="padding:0; position:relative;">
  <!-- /.row -->
  <div class="row mar-t25">
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_1">
        <div id="toggle-button"></div>
      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div class="stock-card curves2">

      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_3" class="stock-card curves2">

      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_4" class="stock-card curves2">

      </div>
    </div>
  </div>
  <!-- /.row -->
</div>

【问题讨论】:

  • Works for me,你确定宽度真的是600
  • @adeneo - 我只给了 600 只是为了检查切换宽度是否有效。我希望 div 一直扩展到容器。
  • @adeneo - 请查看附件图片。

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

这是你要找的吗?

我建议查看 MDN 以更深入地了解 CSS 道具:https://developer.mozilla.org/en-US/docs/Web/CSS/width

$(document).ready(function() {
  $('#toggle-button').click(function() {
    var toggleWidth = $("#exp_card_1").width() > 0 ? "0%" : "100%";
    $('#exp_card_1').animate({
      width: toggleWidth
    });
  });
});
#exp_card_1 {
  position: relative;
  height: 310px;
  background: #2d3644;
  z-index: 1;
}
#toggle-button {
  position: absolute;
  right: 0;
  top: 43.7%;
  height: 38px;
  width: 38px;
  background: #131f34 url("../images/arrow.png") no-repeat;
  background-position: 5px 9px;
  border-radius: 3px 0 0 3px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="container" style="padding:0; position:relative;">
  <!-- /.row -->
  <div class="row mar-t25">
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_1">
        <div id="toggle-button"></div>
      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div class="stock-card curves2">

      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_3" class="stock-card curves2">

      </div>
    </div>
    <div class="col-xs-12 col-sm-3">
      <div id="exp_card_4" class="stock-card curves2">

      </div>
    </div>
  </div>
  <!-- /.row -->
</div>

【讨论】:

  • 嗨 @Brewer Gorge 我希望 div 一直扩展到容器。
  • 如果你在谈论边角的边距,你可以将 body 的边距设置为 0 来删除它。一般来说,我建议习惯 Chrome DevTools(或 FF Web 检查器)来解决这些样式问题。他们真的会给你即时反馈,这对初学者来说非常有用。
  • 请查看附件图片。
猜你喜欢
  • 1970-01-01
  • 2011-01-20
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多