zoukankan      html  css  js  c++  java
  • 自定义input[type="file"]的样式

    input[type="file"]的样式在各个浏览器中的表现不尽相同:

    1. chrome:


    2. firefox:


    3. opera:


    4. ie:


    5. edge:


    另外,当我们规定 input[type="file"] 的高度,并把它的行高设置成与其高度相等后,chrome中难看的样式出现了:


    “未选择任何文件”这一行并没有竖直居中。

    似乎在 firefox 中好看一些,嗯,我比较喜欢用 firefox。但是这些浏览器中的表现不一致,我们必须做兼容处理。

    思路:

    1. 自定义与其中一个浏览器表现类似的样式,将其放在下层;

    2. 将浏览器本身的表现出来的样式“隐藏掉”, opacity:  0; 置于上层,这样我们点击的才是 input[type="file"] 响应的事件;

    3. 选择文件或改变文件后,改变显示 file 的值。

    代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    html:
    <form action="" class="activityForm">
      <div class="file">
        <label for="file">文件:</label>
        <div class="userdefined-file">
          <input type="text" name="userdefinedFile" id="userdefinedFile" value="未选择任何文件">
          <button type="button">上传</button>
        </div>
        <input type="file" name="file" id="file">        
      </div>
    </form>
    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
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    css:
    .file {
      positionrelative;
      height40px;
      line-height40px;
    }
    .file label {
      display: inline-block;
    }
    .userdefined-file {
      positionabsolute;
      top0;
      left60px;
      z-index2;
      width300px;
      height40px;
      line-height40px;
      font-size0;  /*应对子元素为 inline-block 引起的外边距*/
    }
    .userdefined-file input[type="text"] {
      display: inline-block;
      vertical-alignmiddle;
      padding-right14px;
      padding-left14px;
      width220px;
      box-sizing: border-box;
      border1px solid #ccc;
      height40px;
      line-height40px;
      font-size14px;
      overflowhidden;
      text-overflow: ellipsis;
      white-spacenowrap;
    }
    .userdefined-file button {
      display: inline-block;
      vertical-alignmiddle;
      width80px;
      text-aligncenter;
      height40px;
      line-height40px;
      font-size14px;
      background-color#f54;
      bordernone;
      color#fff;
      cursorpointer;
    }
    .file input[type="file"] {
      positionabsolute;
      top0;
      left60px;
      z-index3;
      opacity: 0;
      width300px;
      height40px;
      line-height40px;
      cursorpointer;
    }
    1
    2
    3
    4
    js:
    document.getElementById("file").onchange = function() {
        document.getElementById("userdefinedFile").value = document.getElementById("file").value;
    }

    这样处理后,就在各个浏览器中表现一致了:

    1. 未选择任何文件时,在 chrome 中:


    2. 在选择文件后,在 firefox 中:

      

  • 相关阅读:
    shell 操作钉钉机器人实现告警提醒
    谨慎 mongodb 关于数字操作可能导致类型及精度变化
    数据库如何应对保障大促活动
    SQL Server Alwayson架构下 服务器 各虚拟IP漂移监控告警的功能实现 -1(服务器视角)
    通过 Telegraf + InfluxDB + Grafana 快速搭建监控体系的详细步骤
    MySQL数据库Inception工具学习与测试 笔记
    MongoDB 中数据的替换方法实现 --类Replace()函数功能
    MongoDB 中的【加减乘除】运算
    MySQL索引设计需要考虑哪些因素?
    关于SQL Server 数据库归档的一些思考和改进
  • 原文地址:https://www.cnblogs.com/libin-1/p/5911205.html
Copyright © 2011-2022 走看看