简体   繁体   English

如何使用HTML,JS和CSS创建自定义文件上载

[英]How Can I Create a Custom File Upload With Only HTML, JS and CSS

Here is the design of the control I want to create: 这是我想要创建的控件的设计:

样本设计

As you can see, it's easy to create the design except the browse button of upload control. 如您所见,除了上传控件的浏览按钮外,很容易创建设计。 I created the rest. 我创造了其余的。 I've been searching about changing the place of Browse button but could not find anything useful. 我一直在寻找改变浏览按钮的位置,但找不到任何有用的东西。

So, how to do that? 那么,该怎么做? There was an idea like embedding an unvisible upload control over button but any other advice is golden, thanks. 有一个想法,如嵌入一个不可见的上传控制按钮,但任何其他建议是黄金的,谢谢。

It's actually not as complicated as you may think. 它实际上并不像你想象的那么复杂。 Check out this fiddle . 看看这个小提琴 Stylize your button how you will! 为您的按钮设计风格!

HTML HTML

<input type="file" id="uploadme" />
<input type="button" id="clickme" value="Upload Stuff!" />

CSS CSS

input[type=file] { 
    width: 1px; 
    visibility: hidden;
}

JavaScript JavaScript的

$(function(){
    $('#clickme').click(function(){
        $('#uploadme').click();
    });
});

Here's are two excellent articles which shows you how to customize the appearance of file inputs. 这是两篇优秀的文章,向您展示如何自定义文件输入的外观。

jQuery Custom File Upload Input: from the book Designing with Progressive Enhancement by the Filament Group jQuery自定义文件上传输入:来自 Filament Group的“ 使用渐进增强设计”一书

Styling File Inputs with CSS and the DOM by Shaun Inman Shaun Inman 用CSS和DOM设置文件输入样式

another example: 另一个例子:

see this Fiddle 看到这个小提琴

HTML: HTML:

<div class="button-green"><input class="file-upload" type="file">Upload File</div>

CSS: CSS:

.button-green
{
    font-family: "Segoe UI","Segoe","Verdana";
    background: #0f9e0a center top no-repeat;
    background-image: linear-gradient(rgb(15, 158, 10), rgb(24, 105, 21)); 
    overflow: hidden;
    color: white;
    border-radius: 5px;
    width: 82px;  
    position: relative; 
    padding: 8px 10px 8px 10px;
}

.button-green:hover
{
    background: #0a8406 center top no-repeat;
    background-image: linear-gradient(rgb(12, 141, 8), rgb(19, 88, 17));     
}

.file-upload
{
    opacity: 0;
    width: 102px;
    height: 35px;
    position: absolute;
    top: 0px;
    left: 0px;
}

For a solution that does not require JavaScript, you could use a <label> : 对于不需要JavaScript的解决方案,您可以使用<label>

<label for="upload">Upload</label>
<input type="file" id="upload" style="position:absolute; visibility:hidden"/>

This won't work in every browser, namely older ones and mobile Safari. 这不适用于所有浏览器,即较旧的浏览器和移动Safari。 To cover these, use the above mentioned JavaScript solution. 要涵盖这些,请使用上面提到的JavaScript解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM