自定义上传按钮

IT技术 javascript upload
2021-03-14 00:33:11

嗨,我只是想知道如何创建自己的自定义文件上传按钮,因为我能做的最好的是

在此处输入图片说明

我想要实现的是

在此处输入图片说明

如果有任何这样做,我会非常感激,请我能有答案来解释如何使用代码而不是包含允许您下载按钮或类似内容的网站链接的答案,谢谢:)

6个回答

尽管其中一些答案会创建看起来像您希望它工作的东西,但是当它们尝试按照您期望的方式执行时,它们会崩溃。文件输入的样式不好直接,您将无法尝试。然而,有一个技巧。

诀窍是将输入的不透明度设为 0,然后将其下方的背景更改为您想要的按钮样式。

.file_button_container,
.file_button_container input {
     height: 47px;
     width: 263px;
 }

 .file_button_container {
     background: transparent url(http://i.stack.imgur.com/BT5AB.png) left top no-repeat;
 }

 .file_button_container input {
     opacity: 0;
 }
<div class="file_button_container"><input type="file" /></div>

@LeoHernandez 图像 uri 有问题...我无法控制。有时它可以拉动图像,有时则不能。不过这不是代码的问题。
2021-04-17 00:33:11
@natedavisolds 嗨,当我意识到我的 javascript 按钮在 IE 8 和 FF 上不起作用时,我发现了这个。这很好用,但在我选择它后它不显示文件名。有没有办法显示它,以便用户知道他选择了一个文件。浏览器支持 +1。
2021-04-23 00:33:11
我可以获取要显示的文件名或临时图像缩略图吗?
2021-04-26 00:33:11
@streetlight 这在最新的 Mac 版 Chrome 中对我有用,但我必须单击顶部的运行
2021-04-27 00:33:11
很好的解决方案。我想在 IE7 和 IE8 中添加支持,可以通过包含“-ms-filter:”progid:DXImageTransform.Microsoft.Alpha(Opacity=0)”; filter: alpha(opacity=0);”
2021-05-14 00:33:11

我认为您也可以尝试这样做:
创建一个<label for="X"></label>可以使用 CSS 轻松设置样式的附加元素

<div class="container">
  <label for="upload" class="uploadButton">Upload</label>
  <input type="file" name="upload" id="upload">
</div>

喜欢

.container {
  position: relative;
}
.uploadButton {
  height: 25px;
  width: 66px;
  display: block;
  cursor: pointer;
  background: url('http://www.ifunia.com/images/christmas/youtube-upload-button.gif') center center no-repeat;
}
input[type="file"] {
  display: block;
  width: 66px;
  height: 25px;
  clip: rect(0px 0px 0px 0px);
  position: absolute;
  left: 0;
  top: 0;
}
<form>
  <div class="container">
    <label for="upload" class="uploadButton"></label>
    <input type="file" name="upload" id="upload" required />
  </div>
  <hr/>
  <button type="submit">Submit</button>
</form>

using 有一个缺点display: none;:它还会隐藏浏览器的本机验证工具提示。所以你最好使用opacity: 0; z-index: -1;它并将其放置在标签的正后方。参见jsfiddle.net/MyYHx
2021-04-19 00:33:11
这是最好的解决方案,但它应该使用visibility: hiddenor opacity: 0(not display:none)
2021-04-19 00:33:11
我真的更喜欢你的解决方案,因为它在可访问性和自定义方面更好。好主意 !
2021-04-23 00:33:11
你应该因为这个答案而获奖:)
2021-05-14 00:33:11
哇哦!如此简单,又如此美好。怎么没有早点发现?好消息是,您可以添加两个labels,一个用于实际标签,一个用于绘制按钮
2021-05-15 00:33:11

箭头不是你可以“只用代码来做”的东西,圆角在 Firefox 中可以正常工作,但在 ie 使用 css 时不行......如果你只需要使用自定义图像,这很容易:

css:

#my_upload_button{
  background-image:url(path-to-file.png);
  border:none;
}

在 Vue JS 中如果你想把上传图片按钮放在 textarea 内使用相对和绝对位置将相机图标放在 textarea 内使用 bottom-3 right-4 找到合适的位置

<div class="field relative">
        <label>Description</label>
        <textarea class="textarea" v-model="description" type="text" maxlength="10000"> </textarea>
        <label for="upload-file" class="icn icn-camera cursor-pointer absolute bottom-3 right-4">
          <input type="file" id="upload-file" hidden ref="file" @change="getImage($event)" accept="image/**" />
        </label>
  </div>

如果不在 textarea 框内,只需一个自定义上传按钮,然后保留这样的代码

 <label for="upload-file" class="icn icn-camera cursor-pointer">
      <input type="file" id="upload-file" hidden ref="file" @change="getImage($event)" accept="image/**" />
    </label>

将默认上传文件图标更改为相机图标

使用 CSS 按 id 或 class 设置按钮样式。

这可能会有所帮助:http : //speckyboy.com/2009/05/27/22-css-button-styling-tutorials-and-techniques/

标准 CSS 样式技术不适用于输入控件。
2021-04-24 00:33:11