3.5.5 控件状态

每个控件(尤其是input)在使用的过程中,可能都会有很多种状态,比如输入内容的时候有焦点提示,输错的时候有出错提示等。Bootstrap提供了3种状态的样式可供使用,分别是:焦点状态、禁用状态、验证提示状态。

1.焦点状态

焦点状态的实现方式是在选择器: focus上删除默认的outline样式,重新应用一个新的box-shadow样式,从而实现焦点状态下,input出现柔和的阴影边框(注意,该效果必须使用.form-control样式才行)。运行效果如图3-24所示。

3.5.5 控件状态 - 图1 图3-24 焦点状态时的运行效果

该效果实现源码如下:

  1. // 源码1706行
  2. .form-control:focus {
  3. border-color: #66afe9;
  4. /* 作用域得到焦点时的边框颜色*/
  5. outline: 0;
  6. -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);
  7. box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175,
  8. 233, .6);
  9. }

从图3-24中可以看出,radio和checkbox的焦点效果和普通input的不太一样。Bootstrap对file、radio、checkbox的焦点效果做了一些特殊处理,以便更圆形化。源码如下:

  1. // 源码1675行
  2. input[type="file"]:focus,
  3. input[type="radio"]:focus,
  4. input[type="checkbox"]:focus {
  5. outline: thin dotted;
  6. outline: 5px auto -webkit-focus-ring-color;
  7. outline-offset: -2px;
  8. }

2.禁用状态

禁用状态的实现方式主要是完善默认disabled状态的显示状态,使用方式和普通的disabled一样,只需要在禁用元素上使用disabled属性即可。使用方法如下:

  1. <input type="text" placeholder="Disabled input here..." disabled>

其源码主要是设置了不准输入的鼠标样式和背景颜色(灰色)。注意,只有带.form-control样式的控件才会更改背景色。请看下面的源码:

  1. // 源码1725行
  2. .form-control[disabled],
  3. .form-control[readonly],
  4. fieldset[disabled] .form-control {
  5. /* 若form-control控件或fieldset元素被禁用,显示不允许输入手形图标*/
  6. cursor: not-allowed;
  7. background-color: #eee;
  8. opacity: 1;
  9. }

而如果不使用上述样式,直接在普通的元素上使用disabled属性,则只会显示一个不能输入的手形图标。源码如下:

  1. // 源码1780行
  2. input[type="radio"][disabled],
  3. input[type="checkbox"][disabled],
  4. .radio[disabled],
  5. .radio-inline[disabled],
  6. .checkbox[disabled],
  7. .checkbox-inline[disabled],
  8. fieldset[disabled] input[type="radio"],
  9. fieldset[disabled] input[type="checkbox"],
  10. fieldset[disabled] .radio,
  11. fieldset[disabled] .radio-inline,
  12. fieldset[disabled] .checkbox,
  13. fieldset[disabled] .checkbox-inline {
  14. cursor: not-allowed;
  15. }

从上述源码可以看出,fieldset如果使用了disabled属性,则fieldset内部的input、select、textarea或应用了.form-control样式的其他控件也将为禁用状态。让我们来验证一下,示例如下:

  1. <fieldset disabled>
  2. <legend><input></legend>
  3. <input type="text" placeholder="Disabled input">
  4. <select>
  5. <option>不可选择</option>
  6. </select>
  7. <div class="checkbox">
  8. <label><input type="checkbox">记住密码</label>
  9. </div>
  10. <button type="submit" class="btn btn-primary">提交</button>
  11. </fieldset>

运行效果如图3-25所示。我们发现大部分都被禁用了,但是legend内的input却没有被禁用,依然可以输入文本,这是因为这是HTML5的一个限制,即fieldset内的第一个legend元素不受disabled的影响。Bootstrap为了遵守这一规则(其实实现起来也很复杂,要用JavaScript代码进行实现)就继续保持原状了,大家使用过程中要特别注意。

3.5.5 控件状态 - 图2 图3-25 禁用状态时的运行效果

注意

由于IE不支持fieldset下的disabled属性(比如,IE10不支持input和button),所以IE下的开发者需要用JavaScript代码再进行特殊处理。

fieldset上应用disabled时,fieldset内部的a标签在所有浏览器下的单击行为都不能被禁用(比如:<a class="btn btn-default">),需要使用JavaScript代码再进行特殊处理。

3.验证提示状态

在填写表单的时候,经常要提示用户其输入内容是否合法,长度是否够用,再次输入的密码是否和第一次输入的密码一致,或者输入的用户名存在还是不存在等问题,不同的提示可能需要不同的提示状态(如,颜色、边框、提示语等)。Bootstrap提供了.has-warning、.has-error、.has-success三种样式用于分别表示警告(黄色)、错误(红色)、成功(绿色)语境的内容。代码如下:

  1. <div class="form-group has-warning">
  2. <label class="control-label" for="inputWarning">输入长度不够!</label>
  3. <input type="text" class="form-control" id="Text1">
  4. </div>
  5. <div class="form-group has-error">
  6. <label class="control-label" for="inputError">输入不符合要求!</label>
  7. <input type="text" class="form-control" id="Text2">
  8. </div>
  9. <div class="form-group has-success">
  10. <label class="control-label" for="inputSuccess">输入文本符合要求!</label>
  11. <input type="text" class="form-control" id="Text3">
  12. </div>

从示例代码可以看到,使用了form-group、control-label、form-control标签进行分组(关于控件分组,会在后续章节进行解释),在form-group平级的div元素上应用has-*样式,然后在input元素上应用form-control样式,实现效果如图3-26所示。

3.5.5 控件状态 - 图3 图3-26 验证提示的运行效果

has-error和has-success的实现方式相同,只是对文字、边框和阴影设置的颜色不同。has-warning的实现源码如下:

  1. // 源码1866行
  2. .has-warning .help-block,
  3. .has-warning .control-label,
  4. .has-warning .radio,
  5. .has-warning .checkbox,
  6. .has-warning .radio-inline,
  7. .has-warning .checkbox-inline { /* has-warning容器内部的控件文本颜色统一设置*/
  8. color: #8a6d3b; /* 不使用control-label,使用其他的(如help-block)也可以*/
  9. }
  10. .has-warning .form-control {
  11. /* 为has-warning容器内部的form-control控件设置边框和阴影效果*/
  12. border-color: #8a6d3b;
  13. -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  14. box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  15. }
  16. .has-warning .form-control:focus {
  17. /* has-warning容器内部的控件在得到焦点时的效果颜色更深*/
  18. border-color: #66512c;
  19. -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #c0a16b;
  20. box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #c0a16b;
  21. }
  22. .has-warning .input-group-addon {/* addon的背景色和字体颜色也要同步设置*/
  23. color: #8a6d3b;
  24. background-color: #fcf8e3;
  25. border-color: #8a6d3b;
  26. }

注意

上述.input-group-addon样式的效果会在4.5节进行讲述。

有的时候,我们在验证状态时提供所对应状态的小图标,以便能够直观地显示,实现效果如图3-27所示。

3.5.5 控件状态 - 图4 图3-27 小图标提示的运行效果

从运行效果可以看到,小图标肯定是固定在输入框的右边的。要实现这种效果,通常要解决两个问题:首先设置输入框父元素的定位方式为相对定位,然后设置这种小图标的定位方式为绝对定位(并设置right值)。

Bootstrap针对该功能提供了特殊的feedback样式,用于实现该效果。其用法如下:

  1. <div class="form-group has-success has-feedback">
  2. <label class="control-label" for="inputSuccess2">Input with success</label>
  3. <input type="text" class="form-control" id="inputSuccess2" />
  4. <span class="glyphicon glyphicon-ok form-control-feedback"></span>
  5. </div>

该效果运用了两个样式:父容器上的has-feedback样式用于设置定位方式;小图标元素上的form-control-feedback样式用于设置图标的显示大小等。两个样式的源码正好符合我们前面所说的要求,具体源码如下:

  1. // 源码1824行
  2. .has-feedback { position: relative; /* 相对定位,用于设置input元素的父容器的定位方式*/}
  3. .has-feedback .form-control { padding-right: 42.5px;
  4. /* 右内边距的设置,以便可以显示小图标*/ }
  5. .has-feedback .form-control-feedback { /* 设置小图标的显示方式*/
  6. position: absolute;/* 绝对定位*/
  7. top: 25px;
  8. right: 0; /* 右对齐*/
  9. display: block;
  10. width: 34px;
  11. height: 34px;
  12. line-height: 34px;
  13. text-align: center;
  14. }
  15. .has-success .form-control-feedback { color: #3c763d; /* 验证通过时图标的显示颜色 */}
  16. .has-warning .form-control-feedback { color: #8a6d3b; /* 验证警告时图标的显示颜色 */}
  17. .has-error .form-control-feedback { color: #a94442; /* 验证错误时图标的显示颜色 */}

上述源码的最后3行分别设置了不同验证状态下的小图标的颜色。