使用自定义的按钮替换默认的<input type='file'>
可以通过让默认的input type = 'file'按钮透明度变为0,并且让它刚好覆盖在自定义的按钮上,来实现此效果:
将它写成一个jQuery插件:
(function($){
$.fn.browseElement = function(){
var input = $("<input type='file' multiple>");
input.css({
"position": "absolute",
"z-index": 2,
"cursor": "pointer",
"-moz-opacity": "0",
"filter": "alpha(opacity: 0)",
"opacity": "0"
});
input.mouseout(function(){
input.detach();
});
$(this).mouseover(function(){
input.css($(this).offset());
input.width($(this).outerWidth());
input.height($(this).outerHeight());
$("body").append(input);
});
return input;
};
})(jQuery);
注意: 使用input.offset($(this).offset),会产生bug,常常出现两倍的左侧距离,还是使用.css方法
然后在页面中自定义一个上传按钮:
#attach {
width:100px;
height:30px;
border:1px solid #00B7FF;
background:#cae2fd;
border-radius:4px;
color:#00B7FF;
font-size:12px;
line-height:30px;
text-align:center;
margin:auto;
padding:0
}
<div id="attach">选择文件</div>
然后对该按钮调用扩展的browseElement方法:
var hiddenInput = $('#attach').browseElement();
hiddenInput.change(function(){
//上传文件时相关处理代码
});
完整代码在:
http://pan.baidu.com/s/1mgwQpew
使用自定义的按钮替换默认的<input type='file'>的更多相关文章
- 自定义样式 实现文件控件input[type='file']
一般我们设计的上传按钮都是和整个页面风格相似的样式,不会使用html原生态的上传按钮,但是怎么既自定义自己的样式,又能使用file控件功能呢? 思路是这样的: 1.定义一个相对定位的DIV,按照整成步 ...
- 原生HTML5 input type=file按钮UI自定义
原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...
- input type="file"在各个浏览器下的默认样式,以及修改自定义样式
一.<input type="file"/>在各个浏览器中的默认样式: 系统 浏览器 样式效果 点击效果 mac google 点击按钮和输入框都可以打开文件夹 mac ...
- 自定义上传按钮 <input type="file" name = "file"/> (将file隐藏在button下)
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 关于去除input type='file'改变组件的默认样式换成自己需要的样式的解决方案
在工作中时常会遇到如需要上传功能的按钮,而不像需要系统默认的样式时候,可以采取以下的解决方案: <img onclick="getElementById('file').click() ...
- html中,文件上传时使用的<input type="file">的样式自定义
Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...
- input[type='file']默认样式
<input type="file" name="" id="" value="" /> 当input的ty ...
- input type= file 如何更改自定义的样式
input { @include wh(24px,22px);//sass 宽高 @include pa(0,0); //绝对定位 top:0:left:0: opacity: 0; //透明度: o ...
- 修改input type=file 标签默认样式的简单方法
<html><head><title></title></head><body><form id="upload ...
随机推荐
- Win10 专业版激活!
从Win8.1(无论盗版正版),只要符合计算机处理器位数(32或64),就可以 利用: QJNXR-YD97Q-K7WH4-RYWQ8-6MT6Y 激活你的永久Win10,且激活后记录当前设备信息,版 ...
- acm2024
/** * C语言合法标识符 */ import java.util.*; public class acm2024 { public static void main(String[] args ...
- PHP MySQL 连接数据库
PHP连接MySQL的小实例 <?php /*时间:2014-09-14 *作者:葛崇 *功能:PHP连接MySQL小实例 * */ /* SQL 脚本.直接贴到命令行运行. DROP ...
- 【#254_DIV2】-A B C
题目链接:http://codeforces.com/contest/445 解题报告: 俄国人今天不知道为什么九点钟就比赛了.仅仅过了两道题,第三题全然没思路,有时间单独去刷第三题吧,看起来非常难 ...
- global语句(python学习手册422页)
# -*- coding: cp936 -*- #python 27 #xiaodeng #global语句(python学习手册422页) #实际上就是一个名为__builtin__的模块,但是必须 ...
- Web应用中的普通java程序获取资源文件
- 如何在MyEclipse中将项目部署Tomcat
如果系统里面还没有配置Tomcat,请参照<Windows下如何配置tomcat环境变量>. 1.新建一个web project项目.如下: 效果如下: 2.编写一个实现Servlet接口 ...
- Linux 调优方案, 修改最大连接数-ulimit
Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限制.下面我把某linux用户的最 ...
- IT忍者神龟之 oracle行转列、列转行
一.行转列 须要将例如以下格式 转换为: 这就是最常见的行转列,主要原理是利用decode函数.聚集函数(sum).结合group by分组实现的 create table test( id varc ...
- .net完整的图文验证
摘自:http://blog.csdn.net/durongjian/article/details/4336380 一.创建ValidaeCode类库工程: 1.创建ValidaeCode类库工程, ...