html中,文件上传时使用的的样式自定义

Web页面中,在须要上传文件时基本都会用到<input type="file">元素,它的默认样式:css

chrome下:html

 

IE下:jquery

 

无论是上面哪一种,样式都比较简单,和不少网页的风格都不太协调。chrome

根据用户的需求,设计风格,改变其显示样式的场合就比较多了。bootstrap

若是,要像下面同样作一个bootstrap风格的上传按钮该如何实现。浏览器

 

搭建上传按钮所需的基本元素ui

        <span class="">
            <span>上传</span>
            <input type="file">
        </span>

效果(chrome):spa

如今看到的分两行显示。设计

外围之因此没有换成div,是由于在IE7-浏览器中,只要不是设成inline,它的宽度全都会撑开到能撑到的宽度。若是设成inline,那元素的宽度就没法调整,因此这里用span而后设成inline-block能解决这样的问题。指针

 

增长样式将两行变成一行

        <span class="fileinput-button"">
            <span>上传</span>
            <input type="file">
        </span>

css:

        .fileinput-button {
            position: relative;
            display: inline-block;
        }

        .fileinput-button input{
            position: absolute;
            right: 0px;
            top: 0px;
        }

效果:

默认是没有浅蓝色边框,只有鼠标去点击后,才会显示,这里显示出来是为了看得清楚。

经过将外围的span设成display:relative,将input设成display:absolute的方式让他们都脱离文档流。

经过将input限定在外围的span中进行绝对定位的方式让原本两行显示的变成一行显示。

实际上这里已经overflow了,真正的宽度是“上传”文字的宽度,修改fileinput-button样式增长overflow: hidden

        .fileinput-button {
            position: relative;
            display: inline-block;
            overflow: hidden;
        }

效果:

颇有意思,能看到上边后右边的蓝色边框了吧,其实就是把左边和下边的溢出部分给隐藏了。

这时候用鼠标去点击“上传”两个字其实是点在input上,可以显示“打开”对话框,由于显示层级上input要比“上传”更靠近用户。

 

注意input定位中的right,为何不用left定位。

当咱们改为left后。

效果(chrome):

效果(IE):

 

在chrome下input元素中的选择按钮露出来,可是不要紧,能够经过后面的设透明的方式把它透明掉。

可是在IE下确是会把输入框露出来,关键是鼠标移到输入框上时,指针会变成输入状态,这个就很无法处理了。

经过right的定位方式把输入框移到左边去的方式,能够在IE下回避出现鼠标指针变成输入态的状况。

 

透明input元素

css:

        .fileinput-button {
            position: relative;
            display: inline-block;
            overflow: hidden;
        }

        .fileinput-button input{
            position: absolute;
            left: 0px;
            top: 0px; opacity: 0; -ms-filter: 'alpha(opacity=0)';
        }

效果:

input彻底不见了踪迹,点击“上传”依然有效。

能够支持IE8+。

 

引入bootstrap,并添加按钮样式

head中增长外部css和js的引用。

    <link rel="stylesheet" href="bootstrap/bootstrap.css">
    <link rel="stylesheet" href="bootstrap/bootstrap-theme.css">
    <script src="bootstrap/jquery-1.10.2.js"></script>
    <script src="bootstrap/bootstrap.js"></script>

增长按钮样式。

        <span class="btn btn-success fileinput-button">
            <span>上传</span>
            <input type="file">
        </span>

效果:

 

解决大小问题

若是为fileinput-button样式增长width:100px,将外围的span设成宽100px,会发现点击下部是没有反应的,缘由就是input是默认大小,没法覆盖下部。

能够经过为input设置一个很大的字号将其撑大的方式来解决覆盖问题,这里就设个200px。

       .fileinput-button input{
            position:absolute;
            right: 0px;
            top:0px;
            opacity: 0;
            -ms-filter: 'alpha(opacity=0)'; font-size: 200px;
        }

这样就能解决覆盖问题。

 

完成。

 

参考jQuery-File-Upload

若是是要兼容IE7-能够参考jQuery-File-Upload中的写法。

 

 

代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" href="bootstrap/bootstrap.css">
    <link rel="stylesheet" href="bootstrap/bootstrap-theme.css">
    <script src="bootstrap/jquery-1.10.2.js"></script>
    <script src="bootstrap/bootstrap.js"></script>
    <style>
        .fileinput-button {
            position: relative;
            display: inline-block;
            overflow: hidden;
        }

        .fileinput-button input{
            position:absolute;
            right: 0px;
            top: 0px;
            opacity: 0;
            -ms-filter: 'alpha(opacity=0)';
            font-size: 200px;
        }
    </style>
</head>
<body style="padding: 10px">
    <div align="center">
        <span class="btn btn-success fileinput-button">
            <span>上传</span>
            <input type="file">
        </span>
    </div>
</body>
</html>
相关文章
相关标签/搜索