1、按钮(按钮组)
单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等。那么在这一节中,我们主要向大家介绍Bootstrap框架为大家提供的按钮组组件。
源码查询:
按钮组也是一个独立的组件,所以可以找到对应的源码文件:
☑ LESS版本:对应的源文件为buttons.less
☑ Sass版本:对应的源文件为_buttons.scss
☑ CSS版本:对应bootstrap.css文件第3131行~第3291行
使用方法:
按钮组和下拉菜单组件一样,需要依赖于button.js插件才能正常运行。不过我们同样可以直接只调用bootstrap.js文件。因为这个文件已集成了button.js插件功能。
对于结构方面,非常的简单。使用一个名为“btn-group”的容器,把多个按钮放到这个容器中。如下所示:
1 2 3 4 5 6 7 8 9 10 11 | < div class = "btn-group" > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-step-backward" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-fast-backward" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-backward" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-play" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-pause" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-stop" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-forward " ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-fast-forward" ></ span ></ button > < button type = "button" class = "btn btn-default" >< span class = "glyphicon glyphicon-step-forward" ></ span ></ button > </ div > |
运行效果如下所示:
除了可以使用<button>元素之外,还可以使用其他标签元素,比如<a>标签。唯一要保证的是:不管使用什么标签,“.btn-group”容器里的标签元素需要带有类名“.btn”。
按钮组实现源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /查看bootstrap.css文件第 3131 行~第 3161 行/ .btn-group, .btn-group-vertical { position : relative ; display : inline- block ; vertical-align : middle ; } .btn-group > .btn, .btn-group-vertical > .btn { position : relative ; float : left ; } .btn-group > .btn:hover, .btn-group-vertical > .btn:hover, .btn-group > .btn:focus, .btn-group-vertical > .btn:focus, .btn-group > .btn:active, .btn-group-vertical > .btn:active, .btn-group > .btn.active, .btn-group-vertical > .btn.active { z-index : 2 ; } .btn-group > .btn:focus, .btn-group-vertical > .btn:focus { outline : none ; } .btn-group .btn + .btn, .btn-group .btn + .btn-group, .btn-group .btn-group + .btn, .btn-group .btn-group + .btn-group { margin-left : -1px ; } |
从效果图上我们可以看出,按钮组四个角都是圆角(支持CSS3的浏览器),但有的小伙伴会问,我们平常制作网页时每个按钮都是带有圆角,而在按钮组中的按钮,除了第一个和最后一个具有边上的圆角之外,其他的按钮没有圆角,它是怎么实现的呢?其实实现方法非常简单:
对应的源码如下:
/查看bootstrap.css文件第3174行~第3203行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | .btn-group > .btn:not(:first-child):not(:last-child):not(.dropdown-toggle) { border-radius: 0 ; } .btn-group > .btn:first-child { margin-left : 0 ; } .btn-group > .btn:first-child:not(:last-child):not(.dropdown-toggle) { border-top-right-radius: 0 ; border-bottom-right-radius: 0 ; } .btn-group > .btn:last-child:not(:first-child), .btn-group > .dropdown-toggle:not(:first-child) { border-top-left-radius: 0 ; border-bottom-left-radius: 0 ; } .btn-group > .btn-group { float : left ; } .btn-group > .btn-group:not(:first-child):not(:last-child) > .btn { border-radius: 0 ; } .btn-group > .btn-group:first-child> .btn:last-child, .btn-group > .btn-group:first-child> .dropdown-toggle { border-top-right-radius: 0 ; border-bottom-right-radius: 0 ; } .btn-group > .btn-group:last-child> .btn:first-child { border-top-left-radius: 0 ; border-bottom-left-radius: 0 ; } |
2、按钮(按钮工具栏)
在富文本编辑器中,将按钮组分组排列在一起,比如说复制、剪切和粘贴一组;左对齐、中间对齐、右对齐和两端对齐一组,如下图所示:
那么Bootstrap框架按钮工具栏也提供了这样的制作方法,你只需要将按钮组“btn-group”按组放在一个大的容器“btn-toolbar”中,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < div class = "btn-toolbar" > < div class = "btn-group" > … </ div > < div class = "btn-group" > … </ div > < div class = "btn-group" > … </ div > < div class = "btn-group" > … </ div > </ div > |
实现原理主要是让容器的多个分组“btn-group”元素进行浮动,并且组与组之前保持5px的左外距。代码如下:
/源码请查阅bootstrap.css文件第3162行~第3173行/
1 2 3 4 5 6 7 8 9 10 11 12 | .btn-toolbar { margin-left : -5px ; } .btn-toolbar .btn-group, .btn-toolbar .input-group { float : left ; } .btn-toolbar > .btn, .btn-toolbar > .btn-group, .btn-toolbar > .input-group { margin-left : 5px ; } |
注意在”btn-toolbar”上清除浮动。
/源码请查阅bootstrap.css文件第5062行/
1 2 3 4 5 6 7 8 | .btn-toolbar:before, .btn-toolbar:after{ display : table; content : " " ; } .btn-toolbar:after{ clear : both ; } |
运行效果如下:
按钮组大小设置
在介绍按钮一节中,我们知道按钮是通过btn-lg、btn-sm和btn-xs三个类名来调整padding、font-size、line-height和border-radius属性值来改变按钮大小。那么按钮组的大小,我们也可以通过类似的方法:
☑ .btn-group-lg:——大按钮组
☑ .btn-group-sm:——小按钮组
☑ .btn-group-xs:——超小按钮组
只需要在“.btn-group”类名上追加对应的类名,就可以得到不同大小的按钮组。如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < div class = "btn-toolbar" > < div class = "btn-group btn-group-lg" > … </ div > < div class = "btn-group" > … </ div > < div class = "btn-group btn-group-sm" > … </ div > < div class = "btn-group btn-group-xs" > … </ div > </ div > |
实现按钮组大小效果样式代码如下:
/源码请查阅bootstrap.css文件第2320行~第2340行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | .btn-lg, .btn-group-lg> .btn{ padding : 10px 16px ; font-size : 18px ; line-height : 1.33 ; border-radius: 6px ; } .btn-sm, .btn-group-sm> .btn { padding : 5px 10px ; font-size : 12px ; line-height : 1.5 ; border-radius: 3px ; } .btn-xs, .btn-group-xs> .btn{ padding : 1px 5px ; font-size : 12px ; line-height : 1.5 ; border-radius: 3px ; } |
3、按钮(嵌套分组)
很多时候,我们常把下拉菜单和普通的按钮组排列在一起,实现类似于导航菜单的效果。如下所示:
使用的时候,只需要把当初制作下拉菜单的“dropdown”的容器换成“btn-group”,并且和普通的按钮放在同一级。如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < div class = "btn-group" > < button class = "btnbtn-default" type = "button" >首页</ button > < button class = "btnbtn-default" type = "button" >产品展示</ button > < button class = "btnbtn-default" type = "button" >案例分析</ button > < button class = "btnbtn-default" type = "button" >联系我们</ button > < div class = "btn-group" > < button class = "btnbtn-default dropdown-toggle" data-toggle = "dropdown" type = "button" >关于我们< span class = "caret" ></ span ></ button > < ul class = "dropdown-menu" > < li >< a href = "##" >公司简介</ a ></ li > < li >< a href = "##" >企业文化</ a ></ li > < li >< a href = "##" >组织结构</ a ></ li > < li >< a href = "##" >客服服务</ a ></ li > </ ul > </ div > </ div > |
实现的样式代码:
/查看bootstrap.css文件第3192行~第3223行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | .btn-group > .btn-group { float : left ; } .btn-group > .btn-group:not(:first-child):not(:last-child) > .btn { border-radius: 0 ; } .btn-group > .btn-group:first-child> .btn:last-child, .btn-group > .btn-group:first-child> .dropdown-toggle { border-top-right-radius: 0 ; border-bottom-right-radius: 0 ; } .btn-group > .btn-group:last-child> .btn:first-child { border-top-left-radius: 0 ; border-bottom-left-radius: 0 ; } .btn-group .dropdown-toggle:active, .btn-group.open .dropdown-toggle { outline : 0 ; } .btn-group > .btn + .dropdown-toggle { padding-right : 8px ; padding-left : 8px ; } .btn-group > .btn-lg + .dropdown-toggle { padding-right : 12px ; padding-left : 12px ; } .btn-group.open .dropdown-toggle { -webkit-box-shadow: inset 0 3px 5px rgba( 0 , 0 , 0 , . 125 ); box-shadow: inset 0 3px 5px rgba( 0 , 0 , 0 , . 125 ); } .btn-group.open .dropdown-toggle.btn-link { -webkit-box-shadow: none ; box-shadow: none ; } |
4、按钮(垂直分组)
前面看到的示例,按钮组都是水平显示的。但在实际运用当中,总会碰到垂直显示的效果。在Bootstrap框架中也提供了这样的风格。我们只需要把水平分组的“btn-group”类名换成“btn-group-vertical”即可。
运行的效果如下:
实现垂直分组的样式代码:
/请查看bootstrap.css文件第3234行~第3276行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | .btn-group-vertical > .btn, .btn-group-vertical > .btn-group, .btn-group-vertical > .btn-group > .btn { display : block ; float : none ; width : 100% ; max-width : 100% ; } .btn-group-vertical > .btn-group > .btn { float : none ; } .btn-group-vertical > .btn + .btn, .btn-group-vertical > .btn + .btn-group, .btn-group-vertical > .btn-group + .btn, .btn-group-vertical > .btn-group + .btn-group { margin-top : -1px ; margin-left : 0 ; } .btn-group-vertical > .btn:not(:first-child):not(:last-child) { border-radius: 0 ; } .btn-group-vertical > .btn:first-child:not(:last-child) { border-top-right-radius: 4px ; border-bottom-right-radius: 0 ; border-bottom-left-radius: 0 ; } .btn-group-vertical > .btn:last-child:not(:first-child) { border-top-left-radius: 0 ; border-top-right-radius: 0 ; border-bottom-left-radius: 4px ; } .btn-group-vertical > .btn-group:not(:first-child):not(:last-child) > .btn { border-radius: 0 ; } .btn-group-vertical > .btn-group:first-child:not(:last-child) > .btn:last-child, .btn-group-vertical > .btn-group:first-child:not(:last-child) > .dropdown-toggle { border-bottom-right-radius: 0 ; border-bottom-left-radius: 0 ; } .btn-group-vertical > .btn-group:last-child:not(:first-child) > .btn:first-child { border-top-left-radius: 0 ; border-top-right-radius: 0 ; } |
和水平分组按钮不一样的是:
☑ 水平分组按钮第一个按钮左上角和左下角具有圆角以及最后一个按钮右上角和右下角具有圆角
☑ 垂直分组按钮第一个按钮左上角和右上角具有圆角以及最后一个按钮左下角和右下角具有圆角
5、按钮(等分按钮)
等分按钮的效果在移动端上特别的实用。整个按钮组宽度是容器的100%,而按钮组里面的每个按钮平分整个容器宽度。例如,如果你按钮组里面有五个按钮,那么每个按钮是20%的宽度,如果有四个按钮,那么每个按钮是25%宽度,以此类推。
等分按钮也常被称为是自适应分组按钮,其实现方法也非常的简单,只需要在按钮组“btn-group”上追加一个“btn-group-justified”类名,如下所示:
实现原理非常简单,把“btn-group-justified”模拟成表格(display:table),而且把里面的按钮模拟成表格单元格(display:table-cell)。具体样式代码如下:
/源码请查看bootstrap.css文件第3277行~第3291行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | .btn-group-justified { display : table; width : 100% ; table-layout : fixed ; border-collapse : separate ; } .btn-group-justified > .btn, .btn-group-justified > .btn-group { display : table-cell ; float : none ; width : 1% ; } .btn-group-justified > .btn-group .btn { width : 100% ; } |
特别声明:在制作等分按钮组时,请尽量使用<a>标签元素来制作按钮,因为使用<button>标签元素时,使用display:table在部分浏览器下支持并不友好。
6、按钮下拉菜单
按钮下拉菜单仅从外观上看和上一节介绍的下拉菜单效果基本上是一样的。不同的是在普通的下拉菜单的基础上封装了按钮(.btn)样式效果。简单点说就是点击一个按钮,会显示隐藏的下拉菜单。
按钮下拉菜单其实就是普通的下拉菜单,只不过把“<a>”标签元素换成了“<button>”标签元素。唯一不同的是外部容器“div.dropdown”换成了“div.btn-group”。如下所示:
1 2 3 4 5 6 7 8 9 | < div class = "btn-group" > < button class = "btn btn-default dropdown-toggle" data-toggle = "dropdown" type = "button" >按钮下拉菜单< span class = "caret" ></ span ></ button > < ul class = "dropdown-menu" > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > </ ul > </ div > |
实现样式代码如下:
/查看bootstrap.css文件第3204行~第3223行/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | .btn-group .dropdown-toggle:active, .btn-group.open .dropdown-toggle { outline : 0 ; } .btn-group > .btn + .dropdown-toggle { padding-right : 8px ; padding-left : 8px ; } .btn-group > .btn-lg + .dropdown-toggle { padding-right : 12px ; padding-left : 12px ; } .btn-group.open .dropdown-toggle { -webkit-box-shadow: inset 0 3px 5px rgba( 0 , 0 , 0 , . 125 ); box-shadow: inset 0 3px 5px rgba( 0 , 0 , 0 , . 125 ); } .btn-group.open .dropdown-toggle.btn-link { -webkit-box-shadow: none ; box-shadow: none ; } |
运行的效果如下:
7、按钮的向下向上三角形
按钮的向下三角形,我们是通过在<button>标签中添加一个“<span>”标签元素,并且命名为“caret”:
/源码请查看bootstrap.css文件第2994行~第3003行/
1 2 3 4 5 6 7 8 9 10 | .caret { display : inline- block ; width : 0 ; height : 0 ; margin-left : 2px ; vertical-align : middle ; border-top : 4px solid ; border-right : 4px solid transparent ; border-left : 4px solid transparent ; } |
另外在按钮中的三角形“caret”做了一定的样式处理:
/源码查看bootstrap.css文件第3224行~第3233行/
1 2 3 4 5 6 7 8 9 10 | .btn .caret { margin-left : 0 ; } .btn-lg .caret { border-width : 5px 5px 0 ; border-bottom-width : 0 ; } .dropup .btn-lg .caret { border-width : 0 5px 5px ; } |
有的时候我们的下拉菜单会向上弹起,这个时候我们的三角方向需要朝上显示,实现方法:需要在“.btn-group”类上追加“dropup”类名(这也是做向上弹起下拉菜单要用的类名)。
/源码请查看bootstrap.css文件第3109行~第3114行/
1 2 3 4 5 6 | .dropup .caret, .navbar-fixed- bottom .dropdown .caret { content : "" ; border-top : 0 ; border-bottom : 4px solid ; } |
上面代码中可以看出,向上三角与向下三角的区别:其实就是改变了一个border-bottom的值。
下面是向上弹起菜单的例子:
1 2 3 4 5 6 7 8 9 | < div class = "btn-group dropup" > < button class = "btn btn-default dropdown-toggle" data-toggle = "dropdown" type = "button" >按钮下拉菜单< span class = "caret" ></ span ></ button > < ul class = "dropdown-menu" > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > < li >< a href = "##" >按钮下拉菜单项</ a ></ li > </ ul > </ div > |
运行效果如下:
如果大家还想深入学习,可以点击这里进行学习,再为大家附两个精彩的专题:Bootstrap学习教程 Bootstrap实战教程
以上就是关于Bootstrap按钮组工具栏的全部内容,希望对大家的学习有所帮助。
如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区
联系客服