美文网首页
input file样式包装

input file样式包装

作者: 詹小云 | 来源:发表于2017-05-26 10:55 被阅读0次

初次使用html5的新标签inputp[type="file"]那时,看见标签样式的那一刻我是沉默的,怎么可以这么丑?所以我就动动小手,准备美化一下。于是写下了以下代码:

<!--html-->
<input type="file" id="style"/>

/*css*/
#style{
    width: 168px;
    height: 38px;
    background: #fff;
    border: 1px #CFCFCF solid;
    border-radius: 5px;
}

摇头晃脑轻松自在地打开chrome,脑袋里已经自动播放样式了,然而看到的样式还是...辣么丑。

丑丑的input

于是我就发挥我的聪明才智Google了一下,发现大多人的做法都是相差无几的,做法如下:
1.在input外边包装一层a标签 ,并设置绝对定位

  1. input的透明度设置为0
  2. 把要出现的2文字写在input旁边并设置相对定位
  3. 把样式写给a标签,但是不能设置宽高,不然点击就会直接点a标签,而input就被覆盖了。
    嘻嘻,写个小demo:
<!--html-->
<a href="javascript:(void)" class="upload-wrap">
    <input id="upload" type="file"/>
    <span class="upload-icon">上传文件</span>
</a>

/*css*/
#upload {
    opacity: 0;
    width: 138px;
    height: 38px;
}
.upload-wrap {
    position: relative;
    display: inline-block;
}
.upload-icon {
    position: absolute;
    top: 7px;
    left: 24px;
    color: black;
}
最终样式

好的,然后我们会发现,选择文件后的路径也被opacity为0了,这是后就要用到我们强大的js了。哈哈哈哈哈好吧,其实也只有几行代码。如下:

var upload = document.getElementById('upload');
upload.onchange = function(){
      console.log(this.value);
}

相关文章

  • input file样式包装

    初次使用html5的新标签inputp[type="file"]那时,看见标签样式的那一刻我是沉默的,怎么可以这么...

  • 上传文件 样式 自定义

    css input[type=file] 样式美化,input上传按钮美化 我们在做input文本上传的时候,ht...

  • tech notes

    前端: 修改input type = 'file'的默认样式 JavaScript 执行机制 WebSocket ...

  • css ::file-selector-button 修改inp

    现在可以用::file-selector-button来修改input file上传文件标签样式 html inp...

  • 美团面经总结

    1.文件上传样式处理 使用label标签点击,触发file类型的input,input可直接display: no...

  • 利用 CSS 穿透覆盖默认样式

    常见发生场景:假如我们需要通过 input,type="file"来上传文件,而这个 input 的默认样式,可以...

  • input file(修改样式)

    html: 点击这里上传文件 css: .a-upload { padding: 4px 1...

  • JS小技巧

    CSS 穿透覆盖默认样式input标签 上传type="file",使用img遮盖,防止img阻隔点击事件img ...

  • HTML input type=file 样式设计

    工作中需要自定义一个上传按钮,看了张鑫旭的文章,才想到当初可以用lable给input type=file设计样式...

  • input type=file修改样式

    效果图 原本的样式 html结构 css样式 jq写法 原生js写法

网友评论

      本文标题:input file样式包装

      本文链接:https://www.haomeiwen.com/subject/cygaxxtx.html