5-7 按钮(按钮组)
本节编程练习不计算学习进度,请电脑登录imooc.com操作

按钮(按钮组)

单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等。那么在这一节中,我们主要向大家介绍Bootstrap框架为大家提供的按钮组组件。

源码查询:

按钮组也是一个独立的组件,所以可以找到对应的源码文件:

  ☑  LESS版本:对应的源文件为buttons.less

  ☑  Sass版本:对应的源文件为_buttons.scss

  ☑  CSS版本:对应bootstrap.css文件第3131行~第3291行

使用方法:

按钮组和下拉菜单组件一样,需要依赖于button.js插件才能正常运行。不过我们同样可以直接只调用bootstrap.js文件。因为这个文件已集成了button.js插件功能。

对于结构方面,非常的简单。使用一个名为“btn-group”的容器,把多个按钮放到这个容器中。如下所示:

<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-step-forward"></span>
  </button>
</div>

运行效果如下所示:

除了可以使用<button>元素之外,还可以使用其他标签元素,比如<a>标签。唯一要保证的是:不管使用什么标签,“.btn-group”容器里的标签元素需要带有类名“.btn”。

按钮组实现源码如下:

/*查看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的浏览器),但有的小伙伴会问,我们平常制作网页时每个按钮都是带有圆角,而在按钮组中的按钮,除了第一个和最后一个具有边上的圆角之外,其他的按钮没有圆角,它是怎么实现的呢?其实实现方法非常简单:

  1、默认所有按钮都有圆角

  2、除第一个按钮和最后一个按钮(下拉按钮除外),其他的按钮都取消圆角效果

  3、第一个按钮只留左上角和左下角是圆角

  4、最后一个按钮只留右上角和右下角是圆角

对应的源码如下:

/*查看bootstrap.css文件第3174行~第3203行*/

.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;
}

任务

我来试试:完成下面任务

结合上一章中的图标,完成一组按钮组效果,效果图如下:

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>按钮组</title>
  6. <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  7. <link rel="stylesheet" href="style.css">
  8. </head>
  9. <body>
  10.  
  11. <div class="btn-group">
  12. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-step-backward"></span></button>
  13. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-fast-backward"></span></button>
  14. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-backward"></span></button>
  15. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-play"></span></button>
  16. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-pause"></span></button>
  17. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-stop"></span></button>
  18. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-forward "></span></button>
  19. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-fast-forward"></span></button>
  20. <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-step-forward"></span></button>
  21. </div>
  22.  
  23. <script src="http://img1.sycdn.imooc.com//down/53c6484f00013d9300000000.js"></script>
  24. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  25. </body>
  26. </html>
  1. body{margin:30px;padding:30px;}
下一节