admin管理员组文章数量:1200781
I'm trying to get a button to expand/collapse horizontally other elements(share buttons) and inline using the bootstrap framework
I'm failing at two things:
- The button doesn't expand the other elements inline and after the actual + button
- When it collapse back the elements in it breaks the line and stack on top of each other
I prepared a fiddle:
/
Here is my html code:
<button type="button" class="btn btn-primary btn-sm"
data-toggle="collapse" data-target="#demo">+</button>
<div id="demo" class="collapse in width" style="background-color:yellow;">
<div style="padding: 0; overflow:hidden;">
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
</div>
</div>
And my css:
#demo.width {
height: auto;
-webkit-transition: width 0.35s ease;
-moz-transition: width 0.35s ease;
-o-transition: width 0.35s ease;
transition: width 0.35s ease;
}
I'm trying to get a button to expand/collapse horizontally other elements(share buttons) and inline using the bootstrap framework
I'm failing at two things:
- The button doesn't expand the other elements inline and after the actual + button
- When it collapse back the elements in it breaks the line and stack on top of each other
I prepared a fiddle:
http://jsfiddle.net/g7qCZ/
Here is my html code:
<button type="button" class="btn btn-primary btn-sm"
data-toggle="collapse" data-target="#demo">+</button>
<div id="demo" class="collapse in width" style="background-color:yellow;">
<div style="padding: 0; overflow:hidden;">
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
<button class="btn btn-default btn-xs" type="button">
<span class="glyphicon glyphicon-star"></span> Star
</button>
</div>
</div>
And my css:
#demo.width {
height: auto;
-webkit-transition: width 0.35s ease;
-moz-transition: width 0.35s ease;
-o-transition: width 0.35s ease;
transition: width 0.35s ease;
}
Share
Improve this question
edited Jul 18, 2014 at 19:15
KyleMit♦
29.8k72 gold badges506 silver badges697 bronze badges
asked Jul 18, 2014 at 19:08
user2513846user2513846
1,2362 gold badges17 silver badges41 bronze badges
3 Answers
Reset to default 191) The button doesn't expand the other elements inline and after the actual + button
That's because the 'other elements' are nested inside of a div
element. Div is a block level element with the default of display: block
. You have to override this to display: inline-block
if you would like the div contents to appear on the same line as your plus button.
2) When it collapse back the elements in it breaks the line and stack on top of each other
Overflowing is the last resort for a browser. It would rather not when there are better alternatives and typically wrapping is a better alternative. So the browser will first try to wrap the elements. Only once it can't anymore will it use utilize overflow: hidden
to hide the elements. In order to tell the browser not to wrap, you can use white-space: nowrap;
What to do instead:
Bootstrap collapse is best suited for collapsing vertically. In order to collapse horizontally, you'll have to do a little bit of work. You can use jQuery animate to toggle the width property. However, this will decrement the width in JavaScript which is less performant than straight CSS3 transitions.
Here's another approach. Build a data toggle that just toggles the class in
. Then style the control to have zero width by default, and make it full width when in
is applied:
<button type="button" class="btn btn-primary btn-sm"
data-toggle="toggle" data-target="#demo">+</button>
$("[data-toggle='toggle']").click(function() {
var selector = $(this).data("target");
$(selector).toggleClass('in');
});
#demo {
width: 0px;
}
#demo.in {
width: 220px;
}
Then you can set up the rest of the styles on your demo div:
#demo {
-webkit-transition: width 2s ease;
-moz-transition: width 2s ease;
-o-transition: width 2s ease;
transition: width 2s ease;
display: inline-block;
overflow: hidden;
white-space: nowrap;
background: yellow;
vertical-align: middle;
line-height: 30px;
height: 30px;
}
Here's a working demo in fiddle
The answer by @KyleMit works well. I think you should indeed use the white-space: nowrap;
and display: inline-block
(or float:left
) to solve your issues.
You can apply both properties mentioned above on your #demo
:
#demo {
display: inline-block;
white-space: nowrap;
}
For everything else you can use only Bootstrap code and do not need jQuery animate. Some fixes for Bootstrap are required, see https://github.com/twbs/bootstrap/pull/15104, Bootstrap 3.0 Collapse Horizontally - Bounces in Chrome.
Demo: http://jsfiddle.net/1agh7kaf/1/
Since 5.1 Bootstrap supports .collapse-horizontal
本文标签: javascriptButton toggle horizontal with bootstrapStack Overflow
版权声明:本文标题:javascript - Button toggle horizontal with bootstrap - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738549674a2097120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论