• Bootstrap-表单


    基础表单

    表单中常见的元素主要包括:文本输入框、下拉选择框、单选按钮、复选按钮、文本域和按钮等。其中每个控件所起的作用都各不相同,而且不同的浏览器对表单控件渲染的风格都各有不同。

    表单源码查询

    Bootstrap框架的表单,其源码占据了大量的代码,同样的,根据不同的Bootstrap版本,你可以轻松获取相应的源码:

       ☑   LESS版本:对应源文件 forms.less

       ☑ ​  Sass版本:对应源文件 _forms.scss

    在Bootstrap框架中,通过定制了一个类名`form-control`,也就是说,如果这几个元素使用了类名“form-control”,将会实现一些设计上的定制效果。

    1、宽度变成了100%

    2、设置了一个浅灰色(#ccc)的边框

    3、具有4px的圆角

    4、设置阴影效果,并且元素得到焦点之时,阴影和边框效果会有所变化

    5、设置了placeholder的颜色为#999

    水平表单

    Bootstrap框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右)

    在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:
    1、在<form>元素是使用类名“form-horizontal”。
    2、配合Bootstrap框架的网格系统

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>水平表单</title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    </head>
    <body>
    <form class="form-horizontal" role="form">
      <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">邮箱</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="inputEmail3" placeholder="请输入您的邮箱地址">
        </div>
      </div>
      <div class="form-group">
        <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" id="inputPassword3" placeholder="请输入您的邮箱密码">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <div class="checkbox">
            <label>
              <input type="checkbox"> 记住密码
            </label>
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default">进入邮箱</button>
        </div>
      </div>
    </form>
    </body>
    </html>

     有时候我们需要将表单的控件都在一行内显示

    在Bootstrap框架中实现这样的表单效果是轻而易举的,你只需要在<form>元素中添加类名“form-inline”即可

    表单控件

    input:

    单行输入框,常见的文本输入框,也就是input的type属性值为text。在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootstrap框架都是通过input[type=“?”](其中?号代表type类型,比如说text类型,对应的是input[type=“text”])的形式来定义样式的。

    为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”

    select:

    <form role="form">
      <div class="form-group">
        <select class="form-control"> 
          <option>1</option> 
          <option>2</option> 
          <option>3</option> 
          <option>4</option> 
          <option>5</option> 
          </select>
      </div>

    多行选择设置multiple属性的值为multiple

    textarea,checkbox,radio:

     <div class="form-group">
        <textarea class="form-control" rows="3"></textarea>
      </div>
    <div class="checkbox">
        <label>
          <input type="checkbox" value="">
          记住密码
        </label>
      </div>
      <div class="radio">
        <label>
          <input type="radio" name="optionsRadios" id="optionsRadios1" value="love" checked>
          喜欢
        </label>
      </div>
        <div class="radio">
        <label>
          <input type="radio" name="optionsRadios" id="optionsRadios2" value="hate">
          不喜欢
        </label>

    为了布局的需要,将复选框和单选按钮需要水平排列。Bootstrap框架也做了这方面的考虑:
    1、如果checkbox需要水平排列,只需要在label标签上添加类名“checkbox-inline”
    2、如果radio需要水平排列,只需要在label标签上添加类名“radio-inline”

    <label class="radio-inline">
    <input type="radio" value="option3" name="sex">中性
    </label>

    button:

    按钮也是表单重要控件之一,制作按钮通常使用下面代码来实现:

      ☑  input[type=“submit”]

      ☑  input[type=“button”]

      ☑  input[type=“reset”]

      ☑  <button>

    在Bootstrap框架中的按钮都是采用<button>来实现

    button多标签支持:

    <button class="btn btn-default" type="button">button标签按钮</button> 
    <input type="submit" class="btn btn-default" value="input标签按钮"/>
    
    <span class="btn btn-default">span标签按钮</span>  
    <div class="btn btn-default">div标签按钮</div>
    <a href="#" class="btn btn-default">a标签</a>

    表单控件大小:

    <div class="form-group">
        <label class="control-label">控件变大</label>
        <input class="form-control input-lg" type="text" placeholder="添加.input-lg,控件变大">
      </div>
      <div class="form-group">
        <label class="control-label">正常大小</label>
        <input class="form-control" type="text" placeholder="正常大小">
      </div>  
      <div class="form-group">
        <label class="control-label">控件变小</label>
        <input class="form-control input-sm" type="text" placeholder="添加.input-sm,控件变小">
      </div> 

    表单控件状态(焦点,禁用)

    要让控件在焦点状态下有上面样式效果,需要给控件添加类名“form-control”

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>表单控件状态——禁用状态</title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    </head>
    <body>
    <h3>示例1</h3>
    <form role="form" class="form-horizontal">
      <div class="form-group">
        <div class="col-xs-6">
          <input class="form-control input-lg" type="text" placeholder="不是焦点状态下效果">
        </div>
        <div class="col-xs-6">
          <input class="form-control input-lg" id="disabledInput" type="text" placeholder="表单已被禁用,不可输入" disabled>
        </div>
      </div> 
    </form> 
    <br>
    <br>
    <br>
    <h3>示例2</h3>   
    <form role="form">
      <fieldset disabled>
        <div class="form-group">
          <label for="disabledTextInput">禁用的输入框</label>
          <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入">
        </div>
        <div class="form-group">
          <label for="disabledSelect">禁用的下拉框</label>
          <select id="disabledSelect" class="form-control">
            <option>不可选择</option>
          </select>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox"> 无法选择
          </label>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
      </fieldset>
    </form> 
    <br>
    <br>
    <br>
    <h3>示例3</h3>
    <form role="form">
      <fieldset disabled>
        <legend><input type="text" class="form-control" placeholder="显然我颜色变灰了,但是我没被禁用,不信?单击试一下" /></legend>
        <div class="form-group">
          <label for="disabledTextInput">禁用的输入框</label>
          <input type="text" id="disabledTextInput" class="form-control" placeholder="禁止输入">
        </div>
        <div class="form-group">
          <label for="disabledSelect">禁用的下拉框</label>
          <select id="disabledSelect" class="form-control">
            <option>不可选择</option>
          </select>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox"> 无法选择
          </label>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
      </fieldset>
    </form>  
    </body>
    </html>

    表单控件状态(验证状态)

    在制作表单时,不免要做表单验证。同样也需要提供验证状态样式,在Bootstrap框架中同样提供这几种效果。
    1、.has-warning:警告状态(黄色)
    2、.has-error:错误状态(红色)
    3、.has-success:成功状态(绿色)
    使用的时候只需要在form-group容器上对应添加状态类名。

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>表单控件状态——验证状态</title>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
    <h3>示例1</h3>
    <form role="form">
      <div class="form-group has-success">
        <label class="control-label" for="inputSuccess1">成功状态</label>
        <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
      </div>
      <div class="form-group has-warning">
        <label class="control-label" for="inputWarning1">警告状态</label>
        <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态">
      </div>
      <div class="form-group has-error">
        <label class="control-label" for="inputError1">错误状态</label>
        <input type="text" class="form-control" id="inputError1" placeholder="错误状态">
      </div>
    </form>  
    <br>
    <br>
    <br>
    <h3>示例2</h3>   
    <form role="form">
      <div class="form-group has-success has-feedback">
        <label class="control-label" for="inputSuccess1">成功状态</label>
        <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
        <span class="glyphicon glyphicon-ok form-control-feedback"></span>
      </div>
      <div class="form-group has-warning has-feedback">
        <label class="control-label" for="inputWarning1">警告状态</label>
        <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态">
        <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
      </div>
      <div class="form-group has-error has-feedback">
        <label class="control-label" for="inputError1">错误状态</label>
        <input type="text" class="form-control" id="inputError1" placeholder="错误状态">
        <span class="glyphicon glyphicon-remove form-control-feedback"></span>  
      </div>
    
    </form>
    </body>
    </html>

    在表单验证的时候,不同的状态会提供不同的 icon,比如成功是一个对号(√),错误是一个叉号(×)等。在Bootstrap框中也提供了这样的效果。如果你想让表单在对应的状态下显示 icon 出来,只需要在对应的状态下添加类名“has-feedback”。请注意,此类名要与“has-error”、“has-warning”和“has-success”在一起

    表单提示信息

    平常在制作表单验证时,要提供不同的提示信息。在Bootstrap框架中也提供了这样的效果。使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部

    <form role="form">
      <div class="form-group has-success has-feedback">
        <label class="control-label" for="inputSuccess1">成功状态</label>
        <input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >
        <span class="help-block">你输入的信息是正确的</span>
        <span class="glyphicon glyphicon-ok form-control-feedback"></span>
      </div>
      <div class="form-group has-warning has-feedback">
        <label class="control-label" for="inputWarning1">警告状态</label>
        <input type="text" class="form-control" id="inputWarning1" placeholder="警告状态">
        <span class="help-block">请输入正确信息</span>
        <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
      </div>
      <div class="form-group has-error has-feedback">
        <label class="control-label" for="inputError1">错误状态</label>
        <input type="text" class="form-control" id="inputError1" placeholder="错误状态">
        
        <span class="glyphicon glyphicon-remove form-control-feedback"></span>  
      </div>
    </form>   

     lg,sm,xs:大型,小型,超小型

    图像

    图像在网页制作中也是常要用到的元素,在Bootstrap框架中对于图像的样式风格提供以下几种风格:

    1、img-responsive:响应式图片,主要针对于响应式设计
    2、img-rounded:圆角图片
    3、img-circle:圆形图片
    4、img-thumbnail:缩略图片

    <div class="container">
      <div class="row">
        <div class="col-sm-4">
          <img   alt="140x140" src="http://placehold.it/140x140">
            <div>默认图片</div>
        </div>
        <div class="col-sm-4">
          <img  class="img-rounded" alt="140x140" src="http://placehold.it/140x140"> 
            <div>圆角图片</div>
        </div>
        <div class="col-sm-4">
          <img  class="img-circle" alt="140x140" src="http://placehold.it/140x140">
            <div>圆形图片</div>
        </div>
          <div class="row">
            <div class="col-sm-6">
              <img  class="img-thumbnail" alt="140x140" src="http://placehold.it/140x140"> 
                <div>缩略图</div>
            </div>
            <div class="col-sm-6">
              <img  class="img-responsive" alt="140x140" src="http://placehold.it/140x140" /> 
              <div>响应式图片</div>
            </div>
          </div>
      </div>

    图标

        <span class="glyphicon glyphicon-search"></span>
        <span class="glyphicon glyphicon-asterisk"></span>
        <span class="glyphicon glyphicon-plus"></span>
        <span class="glyphicon glyphicon-cloud"></span>
  • 相关阅读:
    mvp在flutter中的应用
    Flutter 网络请求库http
    Flutter Dart中的异步
    阿里云 RDS 数据库又发 CPU 近 100% 的“芯脏病”团队
    上周热点回顾(10.14-10.20) 团队
    云上的芯脏病:奇怪的阿里云 RDS 数据库突发 CPU 近 100% 问题团队
    【故障公告】docker swarm 集群问题引发的故障团队
    上周热点回顾(10.7-10.13)团队
    上周热点回顾(9.30-10.6) 团队
    上周热点回顾(9.23-9.29) 团队
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/6571845.html
Copyright © 2020-2023  润新知