【问题标题】:<input type="file"> positioning issues in IE11 browser<input type="file"> 在 IE11 浏览器中的定位问题
【发布时间】:2015-12-04 15:12:57
【问题描述】:

#upload-file-container {
  width: 50px;
  height: auto;
  overflow: hidden;
}
#upload-file-container input {
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  border: solid transparent;
  border-width: 0 0 100px 200px;
  opacity: 0.0;
  filter: alpha(opacity=100);
  -o-transform: translate(250px, -50px) scale(1);
  -moz-transform: translate(-300px, 0) scale(4);
  direction: ltr;
  cursor: pointer;
}
.buttonText {
  color: #FFFFFF;
  letter-spacing: normal;
  font-family: Arial, sans-serif;
  font-weight: bold;
  font-variant: normal;
  font-size: 11pt font-style: normal;
  text-decoration: none;
  text-transform: none;
  width: 20px;
}
.buttonSpace {
  height: 23px;
  width: 20;
  background-image: url(/portal/images/clearpixel.gif);
}
.buttonRegHead {
  height: 23px;
  width: 7px;
  padding: 0;
  margin: 0;
  background-image: url(/DIPAPWebAppln/images/button_regular_head.gif);
  background-repeat: no-repeat;
}
.buttonRegBody {
  height: 23px;
  padding: 0;
  margin: 0;
  background-image: url(/DIPAPWebAppln/images/button_regular_body.gif);
  background-repeat: repeat-x;
}
.buttonRegTail {
  height: 23px;
  width: 7px;
  padding: 0;
  margin: 0;
  background-image: url(/DIPAPWebAppln/images/button_regular_tail.gif);
  background-repeat: no-repeat;
}
<html>
<table>
  <tr>
    <td align="left" nowrap class="formLabel">IMAGE1:<font color="red">*</font>
    </td>
    <TD>
      <table border=0 cellspacing=0 cellpadding=0>
        <tr>
          <td class='buttonSpace'>&nbsp;</td>
          <td class='buttonRegHead'>&nbsp;</td>
          <td class='buttonRegBody'>
            <div class="buttonText" id="upload-file-container">Browse
              <input type="file" name="realfile1" id="realfile1" onChange="if(validate())preview(this, '1');return true;" />
            </div>
          </td>
          <td class='buttonRegTail'>&nbsp;</td>
        </tr>
      </table>
    </TD>
  </tr>
</table>

</html>

我有一个包含许多文件输入的 Web 应用程序。我隐藏了所有文件元素并放置了一个按钮。这在 IE8 到 IE10 浏览器中正常工作。但不是在 IE11 中。以下是我的代码片段:

<style>
    #upload-file-container {
        width:50px;
        height: auto;
        overflow: hidden;
    }
    #upload-file-container input {
        position: absolute;
        top: 0; right: 0; margin: 0;
        border: solid transparent;
        border-width: 0 0 100px 200px;
        opacity: 0.0;
        filter: alpha(opacity=0);
        -o-transform: translate(250px, -50px) scale(1);
        -moz-transform: translate(-300px, 0) scale(4); 
        direction: ltr;
        cursor: pointer;
    }
    .buttonText {
        color: #FFFFFF;
        letter-spacing: normal;
        font-family: Arial, sans-serif;
        font-weight: bold;
        font-variant: normal;
        font-size: 11pt
        font-style: normal;
        text-decoration: none;
        text-transform: none;
        width: 20px;
    }
</style>

<table border=0 cellspacing=0 cellpadding=0>
    <tr> 
        <td class='buttonSpace'>&nbsp;</td>
        <td class='buttonRegHead'>&nbsp;</td>
        <td class='buttonRegBody'>
            <div  class="buttonText" id="upload-file-container">    
                <%=DipapDictionary.translate(request,"Browse")%>
                <input type="file" name="realfile1" id="realfile1" onChange="if(validate())preview(this, '1');return true;"/>
            </div>
        <td class='buttonRegTail'>&nbsp;</td>
    </tr>
</table>

在 IE10 浏览器中运行正常。文件输入来自按钮,按钮变为可点击。所以,我可以浏览文件。 但是在 IE11 浏览器中,html 文件元素出现在页面顶部的某个位置,并且浏览按钮不可点击。

【问题讨论】:

  • 你能分享一个工作小提琴吗?
  • @DeepakYadav:添加了一个小代码sn-p。如果您想了解更多信息,请告诉我
  • 您在行后缺少一个分号:font-size: 11pt 不知道它是否是这个问题的原因,但它肯定无济于事。

标签: javascript html css internet-explorer-11


【解决方案1】:

对于 IE11,您只需定义 #upload-file-container input CSS 规则的 CSS widthheight

http://codepen.io/jonathan/pen/LGEJQg/?editors=110

在IE11中打开上面的链接,你会看到浏览文件输入触发器

#upload-file-container input {
    width:100%;
    height:100%;
}

所以你的 CSS 规则看起来像这样:

#upload-file-container input {
    position: absolute;
    top: 0; right: 0; margin: 0;
    border: solid transparent;
    border-width: 0 0 100px 200px;
    opacity: 0.0;
    filter: alpha(opacity=0);
    -o-transform: translate(250px, -50px) scale(1);
    -moz-transform: translate(-300px, 0) scale(4); 
    direction: ltr;
    cursor: pointer;

    width:100%; /* add width */
    height:100%; /* add height */
}

如果您需要使其更合适或更大,确实没有必要在 #upload-file-container input 元素上使用 CSS transform scale()transform translate()

您只需要设置元素的widthheight。特别是对于 IE11,因为它需要 widthheight,因为它们不会被其父元素继承。

【讨论】:

    【解决方案2】:

    这很奇怪,因为我在 IE 9 和 10 上测试了你的 sn-p,但它在那里也不能正常工作。

    我推测的原因是因为您为 Opera 和 Mozilla 供应商添加了 transform 前缀

    -o-transform: translate(250px, -50px) scale(1);
    -moz-transform: translate(-300px, 0) scale(4); 
    

    但是没有像这样的标准转换:

    transform: translate(-300px, 0) scale(4); 
    

    或者像这样带有 Microsoft 前缀的转换:

    -ms-transform: translate(-300px, 0) scale(4); 
    

    所以当我把它放进去时,它就像你描述的那样工作:在 IE 9 和 10 上运行良好,但在 IE11 上不工作。所以我认为这就是你的实际 css 应该是这样的。

    无论如何,解决问题:显然是translate2d is not working well together with scale on IE11。所以我的建议是删除比例部分,将其简化为transform: translate(-300px, 0);。因此,它再次起作用:

    #upload-file-container {
      width: 50px;
      height: auto;
      overflow: hidden;
    }
    #upload-file-container input {
      position: absolute;
      top: 0;
      right: 0;
      margin: 0;
      border: solid transparent;
      border-width: 0 0 100px 200px;
      opacity: 0.0;
      filter: alpha(opacity=100);
      -o-transform: translate(250px, -50px) scale(1);
      -moz-transform: translate(-300px, 0) scale(4);
      -ms-transform: translate(-300px, 0);
      direction: ltr;
      cursor: pointer;
    }
    .buttonText {
      color: #FFFFFF;
      letter-spacing: normal;
      font-family: Arial, sans-serif;
      font-weight: bold;
      font-variant: normal;
      font-size: 11pt font-style: normal;
      text-decoration: none;
      text-transform: none;
      width: 20px;
    }
    .buttonSpace {
      height: 23px;
      width: 20;
      background-image: url(/portal/images/clearpixel.gif);
    }
    .buttonRegHead {
      height: 23px;
      width: 7px;
      padding: 0;
      margin: 0;
      background-image: url(/DIPAPWebAppln/images/button_regular_head.gif);
      background-repeat: no-repeat;
    }
    .buttonRegBody {
      height: 23px;
      padding: 0;
      margin: 0;
      background-image: url(/DIPAPWebAppln/images/button_regular_body.gif);
      background-repeat: repeat-x;
    }
    .buttonRegTail {
      height: 23px;
      width: 7px;
      padding: 0;
      margin: 0;
      background-image: url(/DIPAPWebAppln/images/button_regular_tail.gif);
      background-repeat: no-repeat;
    }
    <html>
    <table>
      <tr>
        <td align="left" nowrap class="formLabel">IMAGE1:<font color="red">*</font>
        </td>
        <TD>
          <table border=0 cellspacing=0 cellpadding=0>
            <tr>
              <td class='buttonSpace'>&nbsp;</td>
              <td class='buttonRegHead'>&nbsp;</td>
              <td class='buttonRegBody'>
                <div class="buttonText" id="upload-file-container">Browse
                  <input type="file" name="realfile1" id="realfile1" onChange="if(validate())preview(this, '1');return true;" />
                </div>
              </td>
              <td class='buttonRegTail'>&nbsp;</td>
            </tr>
          </table>
        </TD>
      </tr>
    </table>
    
    </html>

    编辑:由于您努力为不同的浏览器提供不同的转换值,我将遵循套件并在我的回答中使用-ms-transform。但我建议在您的 CSS 中也使用标准化的 transform,由您决定。

    【讨论】:

      【解决方案3】:

      考虑使用这种更简单的方法来实现相同的目标:

      <label>
          Click me!
          <input type="file" style="display:none" onchange="alert('do stuff')"/>
      </label>
      

      文件输入是隐藏的,但是点击标签会触发文件对话框。

      不需要javascript。无需绝对定位。

      【讨论】:

        【解决方案4】:

        您可以简单地告诉 IE11 表现得像 IE10,只需将以下元标记放入您的 &lt;head&gt;

        <meta http-equiv="X-UA-Compatible" content="IE=10">
        

        不过,我个人的偏好是,在使用 &lt;input type="file"&gt; 时,使用 'display:none' 隐藏它,并使用 javascript 对其进行操作。虽然安全问题意味着您无法设置文件输入的值,但您仍然可以触发点击事件、订阅 onChange 事件(正如您已经在做的那样)并读取 文件输入的值,或从files 属性中读取。

        【讨论】:

        • 是的;就个人而言,我发现尝试破解每个浏览器的“浏览...”按钮定位从来都不是一个非常可扩展的解决方案。总会有事情搞砸的。
        • @dave 我尝试使用 emulate 标签。但是我的页面中有两个jsps。我的 jsp 包含在另一个页面中。我没有主jsp的代码。因此,如果我将元标记放在我的页面中,它会被主 jsp 页面中的元标记覆盖
        • 这更像是一种临时破解,而不是一个可靠的解决方案:“从 IE11 开始,文档模式已被弃用,不应再使用,除非是临时使用。确保更新依赖于传统功能和文档模式以反映现代标准。” msdn.microsoft.com/en-us/library/bg182625(v=vs.85).aspx
        • 是的,有一次,我的回答实际上说这是一种解决方法。我想这在我发布之前已经被编辑了。另一方面,自 IE5 以来,IE 一直在弃用功能,其中一些功能仍然有效。
        • @NageswaranM,不完全控制页面听起来像是完全隐藏 并在其中放置您自己的 UI 的更好论据。
        【解决方案5】:

        以下内容如何:(添加到 CSS)

        input[type=file]
        {
        margin-right:something;
        margin-left:something;
        left:something;
        right:something;
        top:something;
        bottom:something;
        }
        

        【讨论】:

          猜你喜欢
          • 2017-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多